数据结构:单链表的创建、遍历、插入、删除、读取操作

include<stdio.h>
#include<stdlib.h>

typedef struct Node{    //节点包含数据和下一个节点的指针
        int data;
        struct Node *next;
}Node;

Node *create_list(Node **head, int n);     //创建链表
Node *create_node();                      //创建单个节点
void insertnode(Node **head,int num, int i);    //链表插入元素
void printnode(Node *head);             //链表遍历打印
void deletenode(Node **head,int num);  //链表删除元素


Node *create_list(Node **head, int n){      //尾插法创建链表
        Node *current=NULL, *previous=NULL;

        *head =  (Node *)malloc(sizeof(Node));    //申请头结点的内存空间
        (*head)->data = n;      //头结点数据域,存储链表的数据的长度

        current = *head;
        for(int i=0;i<n;i++){                //创建n个节点的链表
                previous = current;             //当前节点变成前一个节点
                current = create_node();    //创建一个新的节点给当前节点
                previous->next = current;  //前一个节点的指针赋值为当前节点
        }
        current->next = NULL;        //最后一个节点的指针指向NULL

        return *head;
}

Node *create_node(){                    //创建一个节点
        Node *p;
        p = (Node *)malloc(sizeof(Node));   //分配对应节点大小的内存
        if(p == NULL){
                printf("申请内存失败\n");
                exit(1);
        }
        p->data = rand()%100;                //节点数据为100以内的随机数
        return p;
}


void deletenode(Node **head, int num){
        Node *current = *head;
        Node *previous = NULL;

        while(current != NULL && current->data != num){    //当前指针不指向NULL>并且当前节点的数据不是指定数
                previous = current;          //当前节点变成前一个节点
                current = current->next;    //当前节点变成下一个节点
        }
        if(current == NULL){       //遍历过后当前指针指向NULL
                printf("找不到匹配节点!\n");      //表示没找到匹配节点
        }
        else{        //找到匹配节点
                if(previous != NULL){       //不是第一个节点
                        previous->next = current->next;     //上一个节点接下来的
指针变成下下一个的指针
                }
                else{        //是第一个节点
                        *head = (*head)->next;     //头指针变成下一个节点的指针
                }
                free(current);      //把要删除的节点内存释放
                (*head)->data --;
        }
}

void printnode(struct Node *head){
        struct Node *ptr = head;

        while(ptr != NULL){
                printf("%d ",ptr->data);
                ptr = ptr->next;
        }
        putchar('\n');
}


void insertnode( Node **head, int num, int i ){      //插入的值为num,插入的位置
在第i个后面
        Node *previous = NULL;
        Node *current = (*head)->next;
        Node *new;
        int count=1;

        new = (Node *)malloc(sizeof(struct Node));
        if(new == NULL){
                printf("申请内存失败\n");
                exit(1);
        }
        new->data = num;

        if(current == NULL){      //插入的链表为空表
                *head = new;      //插入的节点变成第一个
                new->next = NULL;      //指向NULL
        }
        else{
                while( (current != NULL) && (count != i) ){   //从头开始遍历,直
到找到第i个节点
                        previous = current;
                        current = current->next;
                        count++;
                }
                if(current == NULL && count>i){
                        printf("插入位置错误\n");
                        exit(EXIT_FAILURE);
                }
                else{
                        previous->next = new;
                        new->next =current;
                        (*head)->data ++;
                }
        }

}

int main(){
        int num;
        int n,e;
        int i;
        Node *head=NULL;

        printf("请输入要创建的单链表长度:");
        scanf("%d",&n);
        head = create_list(&head, n);
        printnode(head);

        while(1){
                printf("请输入要插入的整数:");
                scanf("%d",&num);
                printf("请输入要位置:");
                scanf("%d",&i);
                if(num == -1){
                        break;
                }
                insertnode(&head,num, i);
                printnode(head);
        }

        while(1){
                printf("请输入要删除的整数:");
                scanf("%d",&num);
                if(num == -1){
                        break;
                }
                deletenode(&head,num);
                printnode(head);
        }

        return 0;
}

代码如上,放进去就能跑。。。不过名称命名好像有点乱

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值