Arvin_JIN

单链表基本操作

  1 //单链表基本操作
  2 #include <iostream>
  3 using namespace std;
  4 
  5 //#define ElemType int
  6 typedef int ElemType;
  7 
  8 typedef struct LNode {
  9     ElemType data;
 10     struct LNode *next;   // (递归定义)  指向后继节点
 11 }LinkList;
 12 
 13 //头插法建表
 14 void CreateListF(LinkList *&List, ElemType a[], int n) {
 15     LinkList *s;
 16     List = (LinkList *)malloc(sizeof(LinkList));
 17     List->next = NULL;
 18     for (int i = 0; i < n; i++) {
 19         s = (LinkList*)malloc(sizeof(LinkList));
 20         s->data = a[i];
 21         s->next = List->next;
 22         List->next = s;
 23     }
 24 }
 25 
 26 
 27 //尾插法建表
 28 void CreateListR(LinkList *&List, ElemType a[], int n) {
 29     LinkList *s, *r;
 30     List = (LinkList *)malloc(sizeof(LinkList));
 31     r = List;
 32     List->next = NULL;
 33     for (int i = 0; i < n; i++) {
 34         s = (LinkList *)malloc(sizeof(LinkList));
 35         s->data = a[i];
 36         r->next = s;
 37         r = s;
 38     }
 39     r->next = NULL;
 40 }
 41 
 42 //初始化单链表
 43 void InitList(LinkList *&list) {
 44     list = (LinkList *)malloc(sizeof(LinkList));
 45     list->next = NULL;
 46 }
 47 
 48 //销毁单链表
 49 void DestroyList(LinkList *&list) {
 50     LinkList *p = list, *q = list->next;
 51     while (q != NULL) {
 52         free(p);
 53         p = q;
 54         q = q->next;
 55     }
 56     free(p);
 57 }
 58 
 59 //判断单链表是否为空
 60 bool ListEmpty(LinkList *list) {
 61     return (list->next == NULL);
 62 }
 63 
 64 //求单链表的长度
 65 int ListLength(LinkList *list) {
 66     int n = 0;
 67     LinkList * p = list;
 68     while (p->next) {
 69         n++;
 70         p = p->next;
 71     }
 72     return n;
 73 }
 74 
 75 //输出单链表
 76 void DispList(LinkList *list) {
 77     LinkList *p = list->next;
 78     while (p != NULL) {
 79         cout << p->data << "  ";
 80         p = p->next;
 81     }
 82     cout << endl;
 83 }
 84 
 85 //求单链表中某值元素的位置
 86 int LocateElem(LinkList *list, ElemType e) {
 87     int i = 1;
 88     LinkList *p = list ->next;
 89     while (p != NULL && p->data != e) {
 90         i++;
 91         p = p->next;
 92     }
 93     if (p == NULL)
 94         return (0);
 95     else 
 96         return (i);
 97 }
 98 
 99 
100 
101 //求单链表中某个位置元素的值
102 bool GetElem(LinkList*list, int i, ElemType &e) {
103     LinkList * p = list;
104     int j = 0;
105     while (j < i && p->next != NULL) {
106         j++;
107         p = p->next;
108     }
109     if (p == NULL)
110         return false;
111     else {
112         e = p->data;
113         return true;
114     }
115 }
116 
117 
118 //插入数据元素
119 bool ListInsert(LinkList *&list, int i, ElemType e) {
120     int j = 0;
121     LinkList *p = list, *s;
122     while (j < i - 1 && p != NULL) {
123         j++;
124         p = p->next;
125     }
126     if (p == NULL) 
127         return false;
128     else {
129         s = (LinkList *)malloc(sizeof(LinkList));
130         s->data = e;
131         s->next = p->next;
132         p->next = s;
133         return true;
134     }
135     
136     
137 }
138 
139 //删除数据元素
140 bool ListDelete(LinkList *&list, int i, ElemType &e) {
141     int j = 0;
142     LinkList *p = list, *q;
143     while (j < i - 1 && p != NULL) {
144         j++;
145         p = p->next;
146     }
147     if (p == NULL)
148         return false;
149     else {
150         q = p->next;
151         if (q == NULL)
152             return false;
153         e = q->data;
154         p->next = q->next;
155         free(q);
156         return true;
157     }
158 }
159 
160 
161 int main() {
162     const int n = 7;
163     int a[n];
164     cout << "Input " << n << " numbers:";
165     for (int i = 0; i < n; i++) {
166         cin >> a[i];
167     }
168     LinkList *lista, *listb;
169     InitList(lista);
170     InitList(listb);
171     CreateListF(lista, a, n);
172     cout << "Input " << n << " numbers:";
173     for (int i = 0; i < n; i++) {
174         cin >> a[i];
175     }
176     CreateListR(listb, a, n);
177     cout << "头插法(lista):";
178     DispList(lista);
179     cout << "尾插法(listb):";
180     DispList(listb);
181     DestroyList(listb);
182     cout << endl << endl;
183     
184     int len = ListLength(lista);
185     cout << "Lista的长度" << len << endl;
186     cout << endl << endl;
187     
188     
189     int m;
190     cout << "输入要查询的元素的值(lista):" ;
191     cin >> m;
192     int loca = LocateElem(lista, m);
193     cout << m << "第一次出现在" << loca << "位置" << endl;
194     cout << endl << endl;
195     
196     
197     cout << "输入要查询的元素的位置(lista):";
198     cin >> loca;
199     GetElem(lista, loca, m);
200     cout << "" << loca << "个元素的值为" << m<< endl;
201     cout<< endl << endl;
202     
203     
204     cout << "输入要插入的位置和数值(lista):";
205     cin >> loca >> m;
206     if (ListInsert(lista, loca, m))
207         DispList(lista);
208     else
209         cout << "插入失败!" << endl;
210     cout << endl << endl;
211     
212     cout << "输入要删除的位置(lista):";
213     cin >> loca;
214     if (ListDelete(lista, loca, m))    {
215         cout << "删除的元素值为" << m << endl << "更新后的链表为:";
216         DispList(lista);
217         cout << endl << endl;
218     }
219     else cout << "删除失败!" << endl;
220     DestroyList(lista);
221 }

 

posted on 2018-04-30 21:50  Arvin_JIN  阅读(244)  评论(0编辑  收藏  举报

导航