单链表的初始化,创建,插入,删除和反转
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef int Elemtype; 5 typedef struct Node 6 { 7 Elemtype data; 8 struct Node *next; 9 }Node, *LinkedList; 10 11 //单链表的初始化 12 LinkedList LinkedListInit() 13 { 14 Node *L; 15 L = (Node*)malloc(sizeof(Node)); 16 if (L == NULL) 17 { 18 printf("申请内存空间失败 "); 19 } 20 L->next = NULL; 21 return L; 22 } 23 24 //单链表的创建一:头插法建立单链表 25 LinkedList LinkedListCreatH() 26 { 27 Node *L; 28 L = (Node *)malloc(sizeof(Node)); 29 L->next = NULL; 30 31 Elemtype x; 32 while (scanf("%d", &x) != EOF) 33 { 34 Node *p; 35 p = (Node *)malloc(sizeof(Node)); 36 p->data = x; 37 p->next = L->next; 38 L->next = p; 39 } 40 return L; 41 } 42 43 //单链表的创建二:尾插法建立单链表 44 LinkedList LinkedListCreatT() 45 { 46 Node *L; 47 L = (Node *)malloc(sizeof(Node)); 48 L->next = NULL; 49 50 Node *r; 51 r = L; 52 Elemtype x; 53 while (scanf("%d", &x) != EOF) 54 { 55 Node *p; 56 p = (Node *)malloc(sizeof(Node)); 57 p->data = x; 58 //p->next = NULL; 59 r->next = p; 60 r = p; 61 } 62 r->next = NULL; 63 return L; 64 } 65 66 //单链表的插入,在链表的第i个位置插入x的元素 67 //要在第i个位置插入,就得先找到第(i-1)个位置,插在它后面 68 LinkedList LinkedListInsert(LinkedList L, int i, Elemtype x) 69 { 70 Node *pre; 71 pre = L; 72 int tempi = 0; 73 for (tempi = 1; tempi < i; tempi++) 74 pre = pre->next; 75 Node *p; 76 p = (Node *)malloc(sizeof(Node)); 77 p->data = x; 78 p->next = pre->next; 79 pre->next = p; 80 return L; 81 } 82 83 //单链表的删除,在链表中删除第一个值为x的元素 84 LinkedList LinkedListDelete(LinkedList L, Elemtype x) 85 { 86 Node *pre, *p; 87 p = L->next; 88 while (p->data != x) 89 { 90 pre = p; 91 p = p->next; 92 } 93 pre->next = p->next; 94 free(p); 95 return L; 96 } 97 98 //单链表的反转 99 LinkedList LinkedListReverse(LinkedList L) 100 { 101 102 Node *rhead = NULL; 103 Node *prev = NULL; 104 Node *p = L->next;//如果原链表的头是一个结点,结点的内容为任意值,p要指向头的下一个结点才是链表的第一个值 105 //Node *p = L;//如果原链表的头是一个指针,p直接等于Lj就可以了,L指的就是链表的第一个值 106 Node *pnext = NULL; 107 while (p != NULL) 108 { 109 pnext = p->next; 110 if (pnext == NULL) 111 rhead = p; 112 p->next = prev; 113 prev = p; 114 p = pnext; 115 } 116 free(L); 117 return rhead; 118 } 119 120 int main() 121 { 122 LinkedList list, start; 123 124 //单链表的创建一:头插法建立单链表 125 printf("请输入单链表的数据:"); 126 list = LinkedListCreatH(); 127 for (start = list->next; start != NULL; start = start->next) 128 printf("%d", start->data); 129 printf(" "); 130 131 //单链表的创建二:尾插法建立单链表 132 printf("请输入单链表的数据:"); 133 list = LinkedListCreatT(); 134 for (start = list->next; start != NULL; start = start->next) 135 printf("%d", start->data); 136 printf(" "); 137 138 //单链表的插入,在链表的第i个位置插入x的元素 139 int i,x; 140 printf("请输入插入数据的位置:"); 141 scanf("%d", &i); 142 printf("请输入插入数据的值:"); 143 scanf("%d", &x); 144 LinkedListInsert(list, i, x); 145 for (start = list->next; start != NULL; start = start->next) 146 printf("%d", start->data); 147 printf(" "); 148 149 //单链表的删除,在链表中删除第一个值为x的元素 150 printf("请输入要删除的元素的值:"); 151 scanf("%d", &x); 152 LinkedListDelete(list, x); 153 for (start = list->next; start != NULL; start = start->next) 154 printf("%d", start->data); 155 printf(" "); 156 157 //单链表的反转 158 Node *rhead; 159 rhead=LinkedListReverse(list); 160 for (start = rhead; start != NULL; start = start->next) 161 printf("%d", start->data); 162 printf(" "); 163 164 system("pause"); 165 return 0; 166 } 167 //注意:结束输入的时候连续输入三个ctrl+z
运行结果:
注意:在VS2015环境下,需要输入三个ctrl+z才能结束while中的scanf;在VC++6.0中一个ctrl+z就可以结束。
——如有不对的地方,非常欢迎给予指导!
——【感谢】部分资料来源于http://blog.csdn.net/m_zhurunfeng/article/details/54809821
——【感谢】部分资料来源于《剑指OFFER》