链表

  1. 数组: 元素类型相同,连续存储,静态分配

    • 使用数组远远不能达到我们的要求

      • 因为数组必须实现确定大小,不能实现动态申请、释放

    • 使用malloc 动态内存分配也无法实现

      • malloc申请的空间,不能实现局部申请、释放

  2. 一种很强大也很重要的数据结构 —— 链表

    • 定义:链表是一种物理存储上非连续,数据元素的逻辑顺序通过链表中的指针链接次序,实现的一种

    • 特点:

      • 链表由一系列节点(链表中每一个元素称为节点)组成,节点在运行是动态生成(malloc)

      • 包括两个部分

        • 一个是存储数据元素的数据域

        • 另一个是存储下一个节点地址的指针域

    • 链表的构成

      • 链表由一个个节点构成,每个节点一般采用结构体的形式组织,列如

        • typedef struct student

        • {

        • int num;

        • char name[20];

        • struct student *next;

        • }STU;

      • 链表节点分为两个域

        • 数据域:存放各种实际的数据 如: num、score等

        • 指针域:存放下一节点的首地址,如:next等

      • 链表的创建

        • 举例

          • typedef struct stu{
            
            int num;
            
            char name[18];
            
            struct stu *next;
            
            }STU;
            
            void link_print(STU*head)
            
            {
            
            while(head!=NULL)
            
            {
            
            printf("%s %d",head->name,head->num);
            
            head =head->next;
            
            }
            
            }
            
            int main(){
            
            STU *p, *p2, * pb;
            
            p = malloc(sizeof(STU));
            
            p->num = 101;
            
            strcpy(p->name,"cc");
            
            p->next = NULL;
            
            p2 = malloc(sizeof(STU));
            
            p2->num = 102;
            
            strcpy(p2->name,"xy");
            
            p->next = p2;
            
            p2->next = NULL:
            
            pb = p;
            
            //while(pb!=NULL)
            
            //{
            
            // printf("%s %d",pb->name,pb->num);
            
            // pb = pb->next;
            
            //}
            
            }
      • 链表的遍历、链表的释放、链表节点的查找、链表节点的删除、链表中插入一个节点、链表排序、链表逆序 有代码解释

      • 双向链表、双向链表的创建、双向链表节点的删除、双向链表插入节点

    • 链表基本操作
      #include <stdio.h>
      #include <stdlib.h>
      
      /* run this program using the console pauser or add your own getch, system("pause") or input loop */
      typedef struct stu{
          int num;
          char name[20];
          struct stu *next;
      }STU;
      
      struct stu*create()   //链表的创建 
      {
          STU*head;
          STU*p;
          STU*p1;
          head = (struct stu*)malloc(sizeof(STU));
          head->next = NULL;
          if(head==NULL)
          {
            return;
          }
          p = head;
          if(head!=NULL)
          {
              while(1)
              {
              printf("请输入学生的学号和姓名\n");
                 scanf("%d%s",&(p->num),p->name);
                 if((p->num)>0)
                 {
              p1= (struct stu*)malloc(sizeof(STU));
              p->next = p1;
              p =p1;
              }else{
                  p = head;
                  return p;
              }    
            }
          }
      }
      void print(STU*head)  //链表的遍历 
      {
          STU*p = head;
          while(p!=NULL)
          {
              printf("学号为:%d\t姓名为%s\n",p->num,p->name);
              p=p->next;
          }
          
      }
      void sort(STU*head)  //链表的查询 
      {
          STU*p = head;
          int n;
          printf("请输入要查找的学号\n");
          scanf("%d",&n);
          while(p!=NULL)
          {
              if((p->num)==n)
              {
                  printf("你所查找的学号为%d\t名字为%s\n",p->num,p->name);
              }
              p = p->next;
          }
       } 
      
      //链表插入 
      struct stu* cha(STU*head)
      {
          STU*p =head;
          STU*p1;
          STU*p2;
          p1 = (struct stu*)malloc(sizeof(STU));
          printf("请输入你要插入的学号和姓名\n");
          scanf("%d%s",&(p1->num),p1->name);
        while(p1->num >p->num&&p->next!=NULL)
        {
              p2 = p;
              p=p->next;
        }
        if(p1->num<=p->num)
        {
            if(p==head)
            {
             p->next = p1;
             p1->next = NULL; 
           } 
           else{
               p2->next = p1;
               p1->next = p;
           }
           if(p->next ==NULL)
           {
               p->next = p1;
               p1->next=NULL;
           }
        }
        
       return head;
         
      }
      
      //链表的删除
       STU*del(STU*head)
       {
         STU*pf;
         STU*pd;
         int count;
         pd = head;
         pf = head;
         printf("请输入你要删除的学号\n");
         scanf("%d",&count);
         while(count!=pd->num&&pd->next!=NULL)
         {
               pf = pd;
               pd = pd->next;
         }
         if(count==pd->num)
         {
         if(pd ==head)
         {
                pf = pd->next;
                free(pd);
         }
         else{
               pf->next = pd->next;
               free(pd);
         }
        }else{
            printf("不好意思!没有你想要删除的学号\n");
        }
      
         
         return pf;
          
       } 
      
      int main(int argc, char *argv[]) {
          STU*head;
          int n;
          printf("1为链表创建并插入\n");
          printf("2为链表的遍历\n");
          printf("3为链表的查询\n");
          printf("4为链表的插入\n");
          printf("5为链表的删除\n");
          printf("其他数字都为退出学生管理系统\n");
          while(1)
          {
              printf("请输入你的选择\n");
              scanf("%d",&n);
              if(n==1)
              {
              head=create();
              }
              else if(n==2)
              {
                  print(head);    
              }
              else if(n==3)
              {
               sort(head);
              }
              else if(n==4)
              {
                  head=cha(head);    
              }else if(n==5)
              {
                  head = del(head);
              }
              else{
                   printf("欢迎使用学生管理系统!\n");
                   exit(0); 
              } 
          }
          return 0;
      }

       

posted @ 2019-10-02 22:42  冬瓜先生  阅读(194)  评论(0)    收藏  举报