单链表的基本操作(创建,删除,增加,反向)

xiaoxiao2021-02-28  105

代码自己真正敲才发现自己掌握的一点都不好。 实现了单链表的基本操作 #include "stdio.h" struct student {   long num;   float score;   struct student *next; };                                    //结构体 1,创建单链表 struct student * create_link() {  struct student *p=NULL,*head=NULL,*p1=NULL;  int n,i=1;                                                      printf("please input the numble of nodes you want to create:");  scanf("%d",&n);                                                                        //n是输入的链表的个数  while(i<=n)  {    p=(struct student*)malloc(sizeof(struct student));                  //分配空间    scanf("%ld %f",&p->num,&p->score);    if(head==NULL)    {      head=p;      p1=p;      i++;      continue;          //退出本次循环    }    p1->next=p;       //关键,是所有结点连接的关键    p1=p1->next;    i++;  }  p1->next=NULL;  //最后让最后的节点指向空                                                                 return head; } 2,插入结点(在第i个结点后面插入) struct student *insert_alink(struct student * head,int i,long n,float s) {   struct student *p,*p1,*pre;   p=(struct student*)malloc(sizeof(struct student));   p->num=n;   p->score=s;   int j=1;   if(i<1)    //判断i<0的情况                  {       printf("the potition is error");       return head;    }   if(head==NULL)   //i不小于零,就将插入的结点作为首结点,而不管i的大小(其实也可以直接退出,找不到该结点)   {     head=p;     return head;   }   pre=head;   p1=pre->next;   while(j<i)   { pre=p1;      if(pre==NULL)        break;               p1=p1->next;      j++;   }   if(pre!=NULL)   {     p->next=p1;     pre->next=p;   }   else     printf("the potition is error");   return head; } 3,两个链表合并(其实也是插入链表的问题,将head1插入到head的第i个位置后面) struct student * two_links(struct student * head,struct student *head1,int i) { int j=1;   struct student *p=head,*p1=head1,*pre,*pre1;     if(head==NULL || head1==NULL)         {        printf("there are at least one  linked list is null");        return head;      }   while(j<=i)  //使pre指向带插入结点,p是pre的后继结点,链表插在pre和p之间   {    pre=p;     if(pre==NULL)    //若是pre为空,则说明i的值太大超出了head链表的结点个数      { printf("the potision is errir\n");        return head;      }    p=p->next;    j++;   }  while(p1!=NULL)  //pre是倒数是最后一个不为空的结点,p1为空   { pre1=p1;     p1=p1->next;   }  if(i==0)   {     pre1->next=head;     return head1;   }    //如果i==0,则插在表头  //否则  pre1->next=p;  pre->next=head1;   return head; } 4,计算链表结点个数 int size_stu(struct student * head) { int i;   struct student * p=head;   if(head==NULL)     return 0;   while(p!=NULL)   {     p=p->next;     i++;   }  return i; } 5,删除 struct student * delete_alink(struct student * head,int i) {  struct student * p1=head,*pre;    if(head==NULL || i<1)     {      printf("the head is invalid or the position is error\n");      return head;     }    int j=1;   if(i==1)     {       head=head->next;       return head;     }    while(j<i)    { pre=p1;      p1=p1->next;      j++;    }    if(p1!=NULL)    {     pre->next=p1->next;     free(p1);    }    else     printf("the potition is error\n");   return head; }                        6,逆序 struct student * nixun(struct student * head) {   struct student *p1=NULL,*p2=NULL,*p3=NULL;   p1=head;   p2=head->next;   p3=p2->next;   while(p2!=NULL)   {        p1->next=p3;        p2->next=head;        head=p2;        p2=p3;        if(p2==NULL)          break;        p3=p3->next;   }  return head; }
转载请注明原文地址: https://www.6miu.com/read-66215.html

最新回复(0)