C语言 实现单链表基本功能

 1 #ifndef __NODE_H__
 2 #define __NODE_H__
 3 #include<stdlib.h>
 4 #include <stdio.h>
 5 struct linkNode{
 6     int data;
 7     struct linkNode * next;
 8 };
 9 struct linkHead{
10     int length;
11     short isEmpty;
12     short isFull;
13     int Maxlength;
14     struct linkNode * next;
15 };
16 typedef struct linkNode* p2Node;
17 typedef struct linkHead* p2Head;
18 typedef struct linkHead Head; 
19 
20 extern p2Head createLinkHead();    
21 extern void createLinkNodeByHead(p2Head head,int data);
22 extern void createLinkNodeByTail(p2Head head,int data);
23 extern p2Node getElement(p2Head head,int index);
24 extern int insertList(p2Head head,int position,int data);
25 extern int removeElement(p2Head head,int position);
26 extern int isEmpty(p2Head head);
27 extern int isFull(p2Head head);
28 extern int getLength(p2Head head);
30 extern void setMaxContains(p2Head head,int length);
31 #endif

这里定义了一个头文件,包含结构体的声明和函数的声明!该头文件叫做node.h

下面的node.c引入了该头文件,对上面的函数做了具体的实现!

  1 #include"node.h"
  2 
  3 p2Head createLinkHead()
  4 {
  5     p2Head head;
  6     head= (p2Head)calloc(1,sizeof(Head));
  7     if(NULL == head)
  8     {
  9         printf("error calloc error!\n");return ;
 10     }
 11     head->next=NULL;
 12     head->length=0;
 13     head->isEmpty=1;
 14     head->isFull=0;
 15     return head;
 16 }
 17 //头插式插入链表 
 18  void createLinkNodeByHead(p2Head head,int data)
 19 {    
 20      if(1 == isFull(head))    
 21     {
 22         return ;
 23     }
 24     p2Node node;
 25     node=(p2Node)calloc(1,sizeof(struct linkNode));
 26     if(NULL == node)
 27     {
 28         printf("error calloc error!\n");return ;
 29     }
 30     node->data=data;
 31     
 32     node->next=head->next;
 33     head->next=node;
 34     head->isEmpty=0;
 35     head->length++;
 36 }
37 void createLinkNodeByTail(p2Head head,int data) 38 { 39 if(1==isFull(head)) 40 { 41 return ; 42 } 43 p2Node node; 44 p2Node p=head->next,r; 45 node=(p2Node)calloc(1,sizeof(struct linkNode)); 46 if(NULL == node) 47 { 48 printf("error calloc error!\n");return ; 49 } 50 if(p == NULL) //若出创建的是第一个节点,让head连接它 51 { 52 head->next=node; 53 node->next=NULL; 54 node->data=data; 55 head->length++; 56 head->isEmpty=0; 57 return ; 58 } 59 while(p != NULL) 60 { r=p; //保存p的前一次值; 61 p=p->next; 62 63 } 64 r->next=node; 65 node->next=NULL; 66 node->data=data; 67 head->isEmpty=0; 68 head->length++; 69 } 70 p2Node getElement(p2Head head,int index) //获得第n个真正节点 71 { 72 int k=1; 73 if(head == NULL || index > head->length) 74 { 75 printf("error could not get! "); 76 return ; 77 } 78 p2Node p=head->next;//p指向第一个节点 79 while(k<index) 80 { 81 p=p->next; 82 k++; 83 } 84 return p; 85 } 86 int insertList(p2Head head,int position,int data) 87 { 88 int k=1; 89 p2Node p=head->next; 90 if(1==isFull(head)) 91 { 92 return -1; 93 } 94 p2Node node=(p2Node)calloc(1,sizeof(struct linkNode )); 95 if(NULL == node) 96 { 97 printf("error calloc fault!\n"); 98 return -1; 99 } 100 node->data=data; 101 //插入头部(第一个位置) 102 if(position == 1) 103 { 104 node->next=head->next; 105 head->next=node; 106 head->length++; 107 return 1; 108 }//插入链表中间部位 109 else if(position<=head->length){ 110 while(k<position-1) 111 { 112 p=p->next; 113 k++; 114 } 115 node->next=p->next; 116 p->next=node; 117 head->length++; 118 return 1; 119 }//插进尾部 120 else{ 121 while(p->next!=NULL) 122 { 123 p=p->next; 124 } 125 node->next=p->next; 126 p->next=node; 127 head->length++; 128 return 1; 129 } 130 131 132 } 133 int removeElement(p2Head head,int position) 134 { 135 p2Node p=head->next,r; 136 if(head== NULL || position >head->length || position<=0) 137 { 138 printf("error could not delete!\n"); 139 return -1; 140 } 141 if(position==1) 142 { 143 144 head->next=p->next; 145 head->length--; 146 return 1; 147 } 148 //若是删除链表中间元素 149 else if(position < head->length) 150 { 151 int k=1; 152 while(k<position-1) 153 { 154 p=p->next; 155 k++; 156 } 157 r=p->next; 158 p->next=r->next; 159 free(r); 160 head->length--; 161 return 1; 162 } 163 else//删除尾部 164 { 165 p2Node r; 166 while(p->next!= NULL) 167 { 168 r=p; 169 p=p->next; 170 } 171 r->next=NULL; 172 free(p); 173 head->length--; 174 return 1; 175 } 176 177 } 178 179 int isEmpty(p2Head head) 180 { 181 if(head->length==0) 182 { 183 return 1; 184 } 185 else{ 186 return 0; 187 } 188 189 } 190 int isFull(p2Head head) 191 { 192 if(head->length>=head->Maxlength) 193 { 194 return 1; 195 } 196 else{ 197 return 0; 198 } 199 } 200 int getLength(p2Head head) 201 { 202 return head->length; 203 } 204 220 void setMaxContains(p2Head head,int length) //选择设置链表的最大长度 221 { 222 head->Maxlength= length; 223 }

具体使用:

1.设置该链表的最大长度

2.创建头结点

3.以头插式或尾插方式创建链表真正数据节点

4.指行插,删,查,操作!

 1 #include <stdlib.h>
 2 #include"node.h"
 3 
 4 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 5 
 6 int main(int argc, char *argv[]) {
 7     p2Head head= createLinkHead();
 8     setMaxContains(head,10);
 9     createLinkNodeByTail( head,1);
10     createLinkNodeByTail( head,2);
11     createLinkNodeByTail( head,3);
12     createLinkNodeByTail( head,4);
13     createLinkNodeByTail( head,5);
14     printf("%d\n",head->length);
15     insertList(head,6,10);
16     removeElement(head,6);
17     p2Node node=getElement(head,5);
18     
19     printf("%d\t%d\t",head->length,head->isEmpty);
20     return 0;
21 }

 

posted @ 2017-08-29 13:25  颜小雀  阅读(452)  评论(0编辑  收藏  举报