C语言(链表)

程序=算法+数据  (算法就是处理数据的)
数据结构:
 1、物理结构:数据在内存中的表现形式
    1、顺序结构:数组(在一块空间,且元素之间相邻)
    2、链式结构:元素之间互不相邻,且元素之间有一定的联系。
 2、逻辑结构:表据的联系(映象)
    联系:元素之间的联系(映象)
   逻辑结构如下:
    散列:元素之间没有联系
    线性结构:节点之间的联系1:1,且每一个节点只能一个直接前驱和直接后驱
              (节点元素之间的联系像一条线)
    树形结构:节点之间存在1:N的联系。(同层节点互不相连) 
    图形结构:节点之间存在N:N的联系。
  3、线性的结构:
      1、物理区:线性顺序表,线性链表:
         线性顺序表:将所有的数据存放一块空间,且存在1:1的系            
            struct people
            {
                int id;
                char name[10];
                char telphone[11];
                //下一个学生的手机号
                int next;
            };
            struct people e[48];
        总结:优点:遍历简单  缺点:删除和插入麻烦,长度固定 
            
          链式线性表:所有节点都不在同一块空间,但节点之间通过指针来相互联系
           1、单向链式线性表:
                struct Node
                {
                    data;
                    struct Node* next;//一个联系(单向)
                };
             头节点指针变量:存放链表中第一个节点的地址。
                struct Node* head;
             尾节点:没有直接后驱(指向域为next==NULL)
             操作功能:
              1、插入节点:(头插法,尾插法:新的结点成为尾节点)
                    1、为节点分配空间
                    2、修改节点的联系
      链表栈:代码如下

1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #include<string.h>
  4 #include<stdbool.h>
  5 struct  student
  6 {
  7         int id;
  8         char name[10];
  9         int sc;
 10 
 11 };
 12 struct node
 13 {
 14         struct student msg;
 15         struct node* next;
 16 };
 17 struct stack
 18 {
 19         struct node* top;
 20         bool (*push)(struct stack*,struct student);
 21 
 22 };
 23 bool push(struct stack* ps,struct student data)
 24 {
 25         struct node* pnew=malloc(sizeof(struct node));
 26         if(pnew==NULL)
 27                 return false;
 28         pnew->msg=data;
 29         pnew->next=ps->top;
 30         ps->top=pnew;
 31                 return true;
}
 34 bool pop(struct stack* ps,struct student* data )
 35 {
 36         struct node* pone;
 37         if(ps->top==NULL)
 38                 return false;
 39         else
 40         {
 41                 *data=ps->top->msg;
 42                 pone=ps->top;
 43                  ps->top=ps->top->next;
 44                 free(pone);
 45                 printf("%d %s %d\n",data->id,data->name,data->sc);
 46                 return true;
 47         }
 48 
 49 
 50 
 51 }
 52 bool init(struct stack* ps)
 53 {
 54         ps->top=NULL;
 55         ps->push=push;
 56 }
void main()
 58 {
 59         struct stack s;
 60         init(&s);
 61         struct student s1={1001,"txd",99};
 62         if(s.push(&s,s1)==true)
 63         {
 64                 printf("成功");
 65         }
 66         struct student s2={0,"",-1} ;
 67         pop(&s,&s2);
 68 
 69 
 70 }
 

猜你喜欢

转载自blog.csdn.net/moon_rern/article/details/81322880