七.单向链表的:增,删,改,查

七.单向链表:增,删,改,查

该链表拥有一个头节点,头节点中的数据域用来存储链表的节点个数,指针域指向第一个数据节点。以下是代码。

链表结构体定义:

struct ListNode 
{
    int num;
    struct ListNode* next;
};
typedef struct ListNode Node;  //重命名为Node

创建头节点

Node *head = (Node *)malloc(sizeof(Node));  //创建头节点

增加节点函数

:在链表尾部增加一个节点

int AddNode(Node *head)      //增加节点
{
    Node *temp = (Node*)malloc(sizeof(Node));  //创建新节点并分配空间
    if(temp==NULL) //内存分配失败
    {
        printf("内存分配失败\n");
        return -1;
    }
    head->num++;            //节点数目加1
    while(head->next)       //先遍历到链表尾节点
    {
        head = head->next;
    }
    printf("输入节点数据: ");
    scanf("%d",&temp->num); //新节点存储数据
    head->next = temp;      //指向新节点          
    temp->next = NULL;      //新节点指空
}

创建链表

:利用AddNode()函数批量创建节点

int CreatList(Node *head,int len) //创建指定长度的链表
{
    int i = 0;
    for(i=0;i<len;i++)
    {
        AddNode(head);
    }
    return 0;
}

删除指定节点

:删除第num个节点

int RemoveNode(Node *head,int num)  //删除指定节点
{
    int i = 0,t_num;
    Node *temp = NULL;
    t_num = head->num;  //取得节点个数
    head->num --;  //节点数减1
    head = head->next; 
    for(i=0;i<num-2;i++)
    {
        head = head->next;
    }
    if(num < t_num)     //不是最后一个节点
    {
        temp = head -> next;
        head->next = head->next->next;//当前节点的上一个节点的指针域指向当前节点的下一个节点的指针域
        free(temp);    //释放当前节点
        temp = NULL;
    }
    else if(num == t_num)//是最后一个节点
    {
        free(head->next);
        head->next = NULL;
    }
    printf("已删除节点%d\n",num);
}

修改指定节点的值

:修改第num个节点的值

int SetNode(Node *head,int num) //修改指定节点
{
    int i = 0;
    int temp = 0;
    if(num>head->num)
    {
        printf("该节点不存在\n");
        return -1;
    }
    head = head->next;
    for (i=0;i<num-1;i++) //遍历到指定节点
    {
        head = head->next;
    }
    printf("请输入数值: ");
    scanf("%d",&temp);
    head->num = temp;
    printf("节点%d的值被修改为: %d\n",num,head->num);
}

插入节点

:在第before个节点后面插入一个新节点

int InsertNode(Node *head,int before) //在指定的节点后插入一个节点 
{
    Node* temp = (Node *)malloc(sizeof(Node));
    if(temp==NULL) //内存分配失败
    {
        printf("内存分配失败\n");
        return -1;
    }
    int i = 0 ;
    head->num++; //节点数加一
    head = head->next;
    for (i=0;i<before-1;i++)
    {
        head = head->next;
    }
    printf("请输入节点数据: ");
    scanf("%d",&temp->num);
    temp->next = head->next;
    head->next=temp;
    printf("在节点%d后 插入完毕\n",before);
}

打印指定节点的值

:打印第num个节点的数据

int PrintNode(Node *head,int num) //打印指定节点的值
{
    int i = 0;
    if(num> head->num)
    {
        printf("该节点不存在\n");
        return -1;
    }
    head = head->next;
    for(i=0;i<num-1;i++) //遍历到指定节点
    {
        head = head ->next;
    }
    printf("第%d个节点的值为%d\n",num,head->num);
}

打印整个链表

:打印所有节点的数据

int PrintList(Node *head) //打印链表
{   
    printf("节点数%d:  ",head->num);
    head = head->next;
    while(head) //若head指针为空,则打印完毕
    {
        printf("%d  ",head->num);
        head = head->next;
    }
    printf("\n");
}

删除整个表

:删除所有节点,包括头结点

int RemoveList(Node *head) //删除表
{
    int i = 0;
    int len = head->num;
    for(i=len;i>0;i--)  //只能从后往前删
        RemoveNode(head,i);
    free(head); //释放头结点空间
    head = NULL;
    printf("链表删除完毕\n");
}

上述是操作链表的相关函数,为了测试方便这里贴上测试代码:

测试代码

//#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <Windows.h>

struct ListNode 
{
    int num;
    struct ListNode* next;
};
typedef struct ListNode Node;  //重命名为Node



int CreatList(Node *head,int len) //创建指定长度的链表
{
    int i = 0;
    for(i=0;i<len;i++)
    {
        AddNode(head);
    }
    return 0;
}


int AddNode(Node *head)      //增加节点
{
    Node *temp = NULL;
    head->num++;            //节点数目加1
    while(head->next)       //先遍历到链表尾节点
    {
        head = head->next;
    }
    temp = (Node*)malloc(sizeof(Node));
    if(temp==NULL) //内存分配失败
    {
        printf("内存分配失败\n");
        return -1;
    }
    printf("输入节点数据: ");
    scanf("%d",&temp->num); //为节点存储数据
    head->next = temp;      //指向新节点          
    temp->next = NULL;      //新节点尾部指空
}

int SetNode(Node *head,int num) //修改指定节点
{
    int i = 0;
    int temp = 0;
    if(num>head->num)
    {
        printf("该节点不存在\n");
        return -1;
    }
    head = head->next;
    for (i=0;i<num-1;i++)
    {
        head = head->next;
    }
    printf("请输入数值: ");
    scanf("%d",&temp);
    head->num = temp;
    printf("节点%d的值被修改为: %d\n",num,head->num);
}

int RemoveNode(Node *head,int num)  //删除指定节点
{
    int i = 0,t_num;
    Node *temp = NULL;
    t_num = head->num;  //取得节点个数
    head->num --;
    head = head->next;
    for(i=0;i<num-2;i++)
    {
        head = head->next;
    }
    if(num < t_num)     //不是最后一个节点
    {
        temp = head -> next;
        head->next = head->next->next;
        free(temp);
        temp = NULL;
    }
    else if(num == t_num)//是最后一个节点
    {
        free(head->next);
        head->next = NULL;
    }
    printf("已删除节点%d\n",num);
}


int PrintNode(Node *head,int num) //打印指定节点的值
{
    int i = 0;
    if(num> head->num)
    {
        printf("该节点不存在\n");
        return -1;
    }
    head = head->next;
    for(i=0;i<num-1;i++)
    {
        head = head ->next;
    }
    printf("第%d个节点的值为%d\n",num,head->num);
}

//插入节点
int InsertNode(Node *head,int before) //在指定的节点后插入一个节点 
{
    Node* temp = (Node *)malloc(sizeof(Node));
    if(temp==NULL) //内存分配失败
    {
        printf("内存分配失败\n");
        return -1;
    }
    int i = 0 ;
    head->num++; //节点数加一
    head = head->next;
    for (i=0;i<before-1;i++)
    {
        head = head->next;
    }
    printf("请输入节点数据: ");
    scanf("%d",&temp->num);
    temp->next = head->next;
    head->next=temp;
    printf("在节点%d后 插入完毕\n",before);
}

int RemoveList(Node *head) //删除表
{
    int i = 0;
    int len = head->num;
    for(i=len;i>0;i--)  //只能从后往前删
        RemoveNode(head,i);
    free(head);
    head = NULL;
    printf("链表删除完毕\n");
}





int PrintList(Node *head) //打印链表
{   
    printf("节点数%d:  ",head->num);
    head = head->next;
    while(head) 
    {
        printf("%d  ",head->num);
        head = head->next;
    }
    printf("\n");
}


int main(int argc,char *argv[])
{
    Node *head = (Node *)malloc(sizeof(Node));  //创建头节点
    if(head==NULL) //内存分配失败
    {
        printf("内存分配失败\n");
        return -1;
    }
    memset(head,0,sizeof(Node));

    CreatList(head,10);  //创建链表
    PrintList(head);   //打印链表
    AddNode(head);
    AddNode(head);
    SetNode(head,8);
    RemoveNode(head,1);
    RemoveNode(head,2);
    PrintNode(head,6);
    InsertNode(head,5);
    PrintList(head);
    RemoveList(head);

    //printf("Hello World!\r\n");
    system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hanhuo/p/9427143.html