单链表

参考文章:http://blog.csdn.net/hguisu/article/details/7673703

  1 #include "stdafx.h"
  2 #include "stdlib.h"
  3 #include <stdio.h>
  4 
  5 #define TRUE 1
  6 #define FAlSE 0
  7 #define OK 1
  8 #define ERROR 0
  9 #define INFEASIBLE -1
 10 #define OVERFLOW -2
 11 
 12 #define LT(a,b) ((a)<(b))
 13 #define N = 100
 14 
 15 typedef int Status;
 16 typedef int ElemType;
 17 
 18 typedef struct LNode
 19 {
 20     ElemType data;
 21     struct LNode* next;
 22 }LNode,*LinkList;
 23 
 24 ///初始化链表
 25 Status initList(LinkList &L)
 26 {
 27     L = (LinkList)malloc(sizeof(LNode));
 28     if(!L) exit(OVERFLOW);
 29     L->next = NULL;
 30     return OK;
 31 
 32     //需要改变指针的指针,所以参数必须是引用或者是 *L
 33     /*
 34     (*L) = (LNode *)malloc(sizeof(LNode));
 35     (*L) ->next = NULL;
 36     return 1;
 37     */
 38 }
 39 
 40 ///创建链表
 41 void createList(LinkList L,int n)
 42 {
 43     if(!L)    initList(L);
 44     ElemType data;
 45     LinkList p,q = L;
 46     printf("输入加点数据的个数%d:\r\n",n);
 47     for(int i = 0;i<n;i++)
 48     {
 49         p = (LinkList)malloc(sizeof(LNode));
 50         scanf("%d",&data);
 51         p->data = data;
 52         p->next = q->next;
 53         q->next = p;
 54         q = p;
 55     }            
 56 }
 57 
 58 ///在第i位置插入e
 59 Status insertList(LinkList L,ElemType e,int i)
 60 {
 61     LinkList s,p = L;
 62     int j = 0;
 63     while(p&&j<i)
 64     {
 65         p = p->next;
 66         j++;
 67     }
 68     if(!p||j>i) return ERROR;
 69     s = (LinkList) malloc(sizeof(LNode));
 70     s->data = e; 
 71     s->next = p->next;
 72     p->next = s;
 73     return OK;
 74 }
 75 
 76 ///删除第i位置元素,并用e返回其值
 77 Status deleteListElem(LinkList L,int i,ElemType &e)
 78 {
 79     LinkList p,q;
 80     int j = 0;
 81     p = L;
 82     while(p && j<i)
 83     {
 84         p = p->next;
 85         j++;
 86     }
 87     if(!p->next || j>i) return ERROR;
 88     q = p->next;
 89     p->next = q->next;
 90     e = q->data;
 91     free(q);
 92     return OK;
 93 }
 94 
 95 Status freelist(LinkList L)
 96 {
 97     LinkList p = L;
 98     while(L)
 99     {
100         p = L->next;
101         free(L);
102         L = p;
103     }
104     printf("success free");
105     return OK;
106 }
107 
108 void printList(LinkList L)
109 {
110     printf("当前值:\r\n");
111     LinkList p;
112     p = L->next;
113     while(p)
114     {
115         printf("%d\t",p->data);
116         p = p->next;
117     }
118     printf("\r\n");
119 }
120 
121 int _tmain(int argc, _TCHAR* argv[])
122 {
123     LinkList La;
124     ElemType e;
125     int init,i;
126     printf("La:\r\n");
127     initList(La);
128     createList(La,5);
129     printList(La);
130     insertList(La,7,3);
131     printf("La:Insert Elem 7\r\n");
132     printList(La);
133     deleteListElem(La,4,e);
134     printf("La:Delete Elem %d\r\n",e);
135     printList(La);
136 
137     freelist(La);
138     system("pause");
139     return 0;
140 }

 输出结果

posted on 2015-04-28 20:42  Liz-  阅读(136)  评论(0编辑  收藏  举报