C 动态链表的建立,输出,删除,插入

动态链表的建立,输出,删除,插入


#include<stdio.h>

#include<malloc.h>

#include<stdlib.h>

#define NULL 0

#define LEN sizeof(struct student)

struct student

{

    long num;

    float score;

    struct student*next;

};

 int n;/*n为全局变量*/

 

struct student *creat()

{

    struct student*p1,*p2,*head;

    n=0;

    p1=p2=(structstudent *)malloc(LEN);

    scanf("%ld,%f",&p1->num,&p1->score);

    head=NULL;

    while(p1->num!=0)

    {

       n=n+1;

       if(n==1)

              head=p1;

       elsep2->next=p1;

       p2=p1;

       p1=(structstudent *)malloc(LEN);

       scanf("%ld,%f",&p1->num,&p1->score);

    }

    p2->next=NULL;

    return (head);

}

 

 

void print(struct student *head)

{

    struct student*p;

    printf("\nThese  records are:\n");

    p=head;

    if(head!=NULL)

       do

       {

              printf("%ld,%f\n",p->num,p->score );

              p=p->next;

       }while(p!=NULL);

}

 

 

struct student *del(struct student *head,long num)

 {

     struct student *p1,*p2;

 

    if(head==NULL)

     {printf("\nlist null!\n");goto end;}

 

     p1=head;

     

     while(num!=p1->num&&p1->next!=NULL)

     {

        p2=p1; p1=p1->next;

     }

     

     if(num==p1->num)

     {

        if(p1==head)head=p1->next;

        else p2->next=p1->next;

        printf("delete:%ld\n",num);

        n=n-1;

     }

     else printf("%d not beenfound!\n",num);

end:;

     return(head);

 }

 

 struct student*insert(struct student *head,struct student *stud)

 {

     struct student *p0,*p1,*p2;

     p1=head;

     p0=stud;

     if(head==NULL)

     {

        head=p0;p0->next=NULL;

     }

     else

     {

        while((p0->num>p1->num)&&(p1->next!=NULL))

        {

               p2=p1;

               p1=p1->next;

        }

 

        if(p0->num<=p1->num)     //和上面的if矛盾,不对

               {

                      if(head==p1)head=p0;

                      else p2->next=p0;

                      p0->next=p1;

               }

               else

               {p1->next=p0;p0->next=NULL;}

     }

        n=n+1;

        return(head);

 }

                      

 

void main()

{

    struct student*head ,stu;

    long del_num;

    printf("inputrecords:\n");

    head=creat();

    print(head);

 

    printf("\ninputthe deleted number:");

    scanf("%ld",&del_num);

    head=del(head,del_num);

    print(head);

 

    printf("\ninputthe inserted record:");

    scanf("%d,%f",&stu.num,&stu.score);

    head=insert(head,&stu);

    print(head);

}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是 C 语言实现单向链表建立删除插入输出的示例代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct ListNode { int val; // 节点值 struct ListNode* next; // 指向下一个节点的指针 } ListNode; // 创建链表 ListNode* createList(int* arr, int n) { ListNode* head = NULL; ListNode* tail = NULL; for (int i = 0; i < n; i++) { ListNode* node = (ListNode*)malloc(sizeof(ListNode)); node->val = arr[i]; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } return head; } // 删除链表节点 ListNode* deleteNode(ListNode* head, int val) { if (head == NULL) { return NULL; } ListNode* dummy = (ListNode*)malloc(sizeof(ListNode)); dummy->next = head; ListNode* prev = dummy; ListNode* curr = head; while (curr != NULL) { if (curr->val == val) { prev->next = curr->next; free(curr); break; } else { prev = curr; curr = curr->next; } } ListNode* newHead = dummy->next; free(dummy); return newHead; } // 插入链表节点 ListNode* insertNode(ListNode* head, int val, int pos) { ListNode* node = (ListNode*)malloc(sizeof(ListNode)); node->val = val; node->next = NULL; if (head == NULL) { return node; } if (pos == 0) { node->next = head; return node; } ListNode* prev = head; ListNode* curr = head->next; for (int i = 1; i < pos; i++) { if (curr == NULL) { break; } prev = curr; curr = curr->next; } prev->next = node; node->next = curr; return head; } // 输出链表 void printList(ListNode* head) { ListNode* curr = head; while (curr != NULL) { printf("%d ", curr->val); curr = curr->next; } printf("\n"); } // 主函数 int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(int); ListNode* head = createList(arr, n); printList(head); head = deleteNode(head, 3); printList(head); head = insertNode(head, 6, 2); printList(head); return 0; } ``` 在上面的示例代码中,我们定义了链表节点结构体 `ListNode`,其中包含了节点的值 `val` 和指向下一个节点的指针 `next`。 接着,我们实现了链表的创建、删除插入输出操作。其中,创建链表时需要传入一个整型数组 `arr` 和数组的长度 `n`,然后根据数组中的元素创建链表删除操作需要传入要删除的节点的值 `val`,然后遍历链表找到该节点并删除插入操作需要传入要插入的节点的值 `val` 和要插入的位置 `pos`,然后遍历链表找到要插入位置的前一个节点,插入新节点并将原来的节点指针连接到新节点后面。输出操作则是遍历链表输出每个节点的值。 最后,在主函数中我们创建了一个整型数组 `arr`,然后根据数组中的元素创建链表,并依次进行删除插入输出操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值