1:创建一个单链表
2:显示打印一个单链表
3:单链表长度计算
4:查找指定元素,返回该元素在链表中的位置
5:查找指定节点,返回特定位置的节点,并打印节点出的数据
6:插入元素到单链表的指定位置,返回插入后的链表
7:删除节点,给定节点位置,删除该处的数据,返回删除该节点的链表
8:反转链表,将链表从后往前打印输出
9:寻找中间节点,返回中间节点信息,打印此处的节点数据
10:链表排序,对链表中的数据进行从小到大排序,打印显示
typedef struct node { int data; node *next; }node; node *creat() //创建一个链表,以数字0作为结束 { int i = 0; node *p, *q, *head; int x = 0; head = (node*)malloc(sizeof(node)); head->next = NULL; q = head; //最开始尾节点就在首节点 //head->next = NULL; while (1) { printf("please input a data:"); scanf_s("%d", &x); if (x == 0) break; p = (node*)malloc(sizeof(node)); p->data = x; q->next = p; q = p; } q->next = NULL; return head; } void display(node *k) //链表的元素打印 { node *p = k->next; while (p != NULL) { cout << p->data<<"->"; p = p->next; } } int length(node* k) //链表长度获取 { int len = 0; node *p =k->next; while (p != NULL) { len++; p = p->next; } return len; } int search_node(node *k, int x) //查找元素,返回该元素在链表中的位置 { node *p = k->next; int i = 0; while (p!=NULL) { if (p->data == x) return i; p = p->next; if (p == NULL) { cout << "no this element" << endl; return 0; } i++; } return i; } node * search(node*head, int pos) //以节点信息查找,返回寻找位置的节点 { node *p = head->next; if (pos<0) { printf("incorrect position to search node! "); return NULL; } if (pos == 0) { return head; } if (p == NULL) { printf("link is empty! "); return NULL; } while (pos--) { if (p!= NULL) { p = p->next; } else { cout << "error" << endl; break; } } return p; } node *insert_node(node *k, int pos, int data) //插入一个数据到指定节点,返回插入后的链表 { node *p; node *item = NULL; item = (node*)malloc(sizeof(node)); item->data = data; if (pos < 0) cout << "insert invalid" << endl; if (pos == 0) { k->next = item; return k; } p = search(k, pos); if (p != NULL) { item->next = p->next; p->next = item; cout << "insert success" << endl; } else cout << "insert invalid" << endl; return k; } node *delete_node(node *k, int pos)//删除指定位置的节点数据,返回删除后的链表 { node *item = NULL; node *p = k->next; if (p == NULL) { cout << "it's empty" << endl; return NULL; } p = search(k, pos - 1); if (p != NULL&&p->next != NULL) { item = p->next; p->next = item->next; delete item; } return k; } node *reverse(node *k) //反转链表,就地反转法 { if (k == NULL) return k; node *p, *q, *r; p = k->next; q = p->next; while (q != NULL) { p->next = q->next; q->next = k->next; k->next = q; q = p->next; } return k; } node *search_middle(node *k) { int i = 0, j = 0; node *cur = NULL, *mid = NULL; cur = mid = k->next; while (cur != NULL) { if (i / 2 > j) { mid = mid->next; j++; } cur = cur->next; i++; } return mid; } node *sort(node *k) //链表排序,按从小到大排序 { int len = length(k); if (len == 0) { cout << "it's empty" << endl; return k; } node *p = k; int i, j, temp; for (i = 0; i < len; i++) { p=k->next; for (j = 1; j < len -i; j++) { if (p->data > p->next->data) { temp = p->data; p->data = p->next->data; p->next->data = temp; } p = p->next; } } for (i = 0; i < len; i++) { cout << p->data << " "; p = p->next; } }
子函数功能测试:
int main() { node *j,*s; int l = 0,m,pos; int data; j = creat(); display(j); l = length(j); cout << endl<< "length is: " << l << endl; cout << "输入一个带查询元素:"; cin >> m; pos = search_node(j, m); if(pos!=0) cout << "元素" << m << "在链表里的位置" << pos << endl; cout << "输入待查寻节点的位置;"; cin >> m; s = search(j, m); if (s != NULL) cout << endl << "查询节点处的元素值为:" << s->data << endl; cout << "插入节点,输入待插入的节点位置和数据:"; cin >> m>>data; insert_node(j, m, data); cout << endl; display(j); cout << endl<<"删除节点,输入待删除节点的位置:" << endl; cin >> m; s=delete_node(j, m); if (s != NULL) cout << "删除成功" << endl; display(s); cout << endl<< "反转链表" << endl; s = reverse(j); display(s); s = search_middle(j); cout << endl<< "中间节点数据为:" << s->data << endl; cout << "排序链表,从小到大排序" << endl; s = sort(j); return 0; }
运行演示: