1.链表之单链表

linklist.h

 

 1 #ifndef _LINKLIST_H_
 2 #define _LINKLIST_H_
 3 
 4 #include <stdio.h>
 5 #include <stdlib.h>
 6 
 7 typedef struct node
 8 {
 9     int data;
10     struct node *next;
11 }linknode;
12 
13 
14 
15 int linklist_add_tail(linknode *L,int x);    /*尾加法*/
16 int linklist_add_head(linknode *L,int x);   /*头加法*/
17 int linklist_del(linknode *L, int val);     /*删除数据节点*/
18 int linklist_mod(linknode *L, int old_val, int new_val);/*修改节点数据*/
19 int linklist_mod_weizhi(linknode *L,int num,int y);/*按位置修改数据*/
20 int linklist_que(linknode *L, int val);     /*查询目标数据节点*/
21 
22 void linklist_show(linknode *L);            /*显示/遍历输出所有节点数据*/
23 void linklist_free(linknode *L);            /*释放所有数据节点*/
24 
25 
26 #endif //_LINKLIST_H_

linklist.c

  1 #include "linklist.h"
  2 
  3 linknode *linklist_create()/*申请头节点,返回头节点地址*/
  4 //linklist_create为返回一个指针的函数,该指针指向linknode类型数据
  5 {
  6     linknode *l = malloc(sizeof(linknode));
  7     if(NULL == l) return NULL;     //注意:对堆区申请的空间要做判空处理
  8     l->data = 0;    //头节点不存数据
  9     l->next = NULL;
 10     return l;
 11 }
 12 
 13 int linklist_add_tail(linknode *L,int x)/*尾加法*/
 14 {
 15     if(NULL == L) return -1;    //从上层传得到的链表头节点地址要判空
 16 
 17     linknode *n = malloc(sizeof(linknode)); //为要增加的新结点申请地址空间,n为新节点地址
 18     if(NULL == n) return -1;
 19 
 20     n->data = x;    //为新节点赋初值
 21     n->next = NULL;
 22 
 23     linknode *p = L;      //用p指针来代替L来遍历寻找链表尾
 24     while(p->next != NULL)//寻找表尾
 25     {
 26             p = p->next;
 27     }
 28     p->next = n;    //将新节点地址给原来链表尾,即原来链表尾指向新节点
 29     return 0;
 30 }
 31 
 32 //
 33 int linklist_add_head(linknode *L,int x)/*头加法*/
 34 {
 35     if(NULL == L) return -1;    //从上层得到的链表头节点地址要判空
 36     linknode *n = malloc(sizeof(linknode));
 37     if(NULL == n) return -1;
 38 
 39     n->data = x;
 40     n->next = L->next;
 41     L->next = n;
 42 
 43     return 0;
 44 }
 45 
 46 int linklist_del(linknode *L, int val)/*删除数据节点*/
 47 {
 48     if(NULL == L) return -1;    //从上层得到的链表头节点地址要判空
 49     linknode *p1 = L;//定义两个指针配合使用
 50     linknode *p2 = L->next;
 51     while(p2 != NULL)    //从数据节点开头一直遍历寻找
 52     {
 53         if(p2->data == val)
 54         {
 55             p1->next = p2->next;//核心代码
 56             free(p2);
 57             p2 = NULL;
 58             p2 = p1->next;
 59         }
 60         else
 61         {
 62             p1 = p1->next;//继续遍历直到找到要删除的数据
 63             p2 = p2->next;
 64 
 65         }
 66     }
 67         return 0;
 68 }
 69 
 70 int linklist_mod(linknode *L, int old_val, int new_val)/*按数据值修改节点数据*/
 71 {
 72     if(NULL == L) return -1;    //从上层得到的链表头节点地址要判空
 73 
 74     linknode *p = L->next;    //定义数据头节点指针
 75     while(p != NULL)        //遍历寻找
 76     {
 77         if(p->data == old_val)
 78         {    
 79             p->data = new_val;
 80         }
 81         p = p-> next;//遍历
 82     }
 83         return 0;
 84 }
 85 
 86 //
 87 int linklist_mod_weizhi(linknode *L,int num,int y)/*按位置修改数据*/
 88 {
 89     if(NULL == L) return -1;    //从上层得到的链表头节点地址要判空
 90     linknode *p = L->next;
 91     int count = 0;
 92     while(p != NULL) {
 93         count++;
 94         if(count == num) {
 95             p->data = y;
 96             break;
 97         }
 98         p = p->next;
 99     }
100 }
101 
102 int linklist_que(linknode *L, int val)/*查询目标数据节点*/
103 {
104     if(NULL == L) return -1;//从上层得到的链表头节点地址要判空
105 
106     linknode *p = L->next;  //定义数据头节点地址p
107     while(p != NULL)
108     {    
109         if(p->data == val) return 1; //查找到目标数据节点,返回1提供给上层应用
110         p = p->next;    //遍历
111     }
112         return 0;                    //未找到返回0
113 }
114 
115 
116 
117 void linklist_show(linknode *L)/*显示/遍历输出所有节点数据*/
118 {
119     if(NULL == L) return ;    //从上层得到的链表头节点地址要判空
120 
121     linknode *p = L->next;    //定义数据头节点地址p
122     while(p != NULL)
123     {
124         printf("%d ",p->data); //遍历输出节点数据
125         p = p->next;
126     }
127     printf("\n");
128 }
129 
130 
131 void linklist_free(linknode *L)/*释放所有数据节点*/
132 {
133     if(NULL == L) return ;    //从上层得到的链表头节点地址要判空
134 
135     linknode *p = NULL;
136     while(L != NULL) //定义p指针 配合L从头节点开始释放所有节点
137     {
138         p = L;
139         free(p);
140         L = L->next;
141     }
142         L = NULL;
143 }

main.c

 1 #include "linklist.h"
 2 int main()
 3 {
 4     /*增加*/
 5     linknode *LL = linklist_create();
 6     int i = 0;
 7     for(i = 0;i < 5;i++)
 8     {
 9         //linklist_add_tail(LL, i+80);
10         linklist_add_head(LL, i+80);
11     }
12         linklist_show(LL);
13 
14     int a = 0,ret = 0,q = 0;
15     /*删除*/
16     /*while(1) {
17         printf("please input :\n");
18         scanf("%d",&a);
19         if(a == q) break ;
20         linklist_del(LL,a);
21         linklist_show(LL);
22     }*/
23 
24     /*修改*/
25     /*    linklist_mod(LL,84,888);
26         linklist_show(LL);
27     */
28     /*查询*/
29     while(1) {
30         printf("please input :\n");
31         scanf("%d",&a);
32         if(a == q) break ;
33         ret = linklist_que(LL,a);
34         if(ret == 1) 
35                 {printf("find!\n");}
36         else 
37                 {printf("no find!\n");}
38         linklist_show(LL);
39     }
40     linklist_free(LL);
41     return 0;
42 }

makefile

 1 linklist: main.o linklist.o
 2     gcc -g $^ -o $@
 3 main.o:main.c
 4     gcc -g -c $< -o $@
 5 linklist.o:linklist.c
 6     gcc -g -c $< -o $@
 7 
 8 clean:
 9     rm -rf *.o
10     rm -rf linklist

 https://pan.baidu.com/wap/link?surl=1c2EWgKC&realName=1&uid=1512696049029_800&traceid=&ssid=c2aa474ca42f8fbe2626d5926967c4b9.3.1512696196.1.hZVFEzTMk4yT&errno=0&errmsg=Auth%20Login%20Sucess&bduss=&ssnerror=0

posted @ 2017-03-09 21:00  bkycrmn  阅读(119)  评论(0)    收藏  举报