单向链表的相关操作

来源:互联网 发布:linux mint哪个版本 编辑:程序博客网 时间:2024/06/04 23:29

链表的相关操作,在没学软件技术基础之前,代码依旧写于2017年2月。

#include <iostream>using namespace std;typedef struct linklist {int data;struct linklist* next;}LINK;LINK* creat(int element,LINK *first);LINK* search (int j,LINK * head);LINK * bubblesort(LINK *head,int length);LINK * selectionsort(LINK *head,int length);LINK * insertionsort(LINK *head,int length);LINK * swap_diy(LINK *head,int i,int j);LINK* destroy(LINK *head);void print (LINK* head);int length (LINK* head);int main(){LINK *head;head = new(LINK);head->next = NULL;while (1){int a;cout <<"1.创建链表"<<endl;cout <<"2.查找链表"<<endl;cout <<"3.插入元素"<<endl;cout <<"4.删除元素"<<endl;cout <<"5.排序"<<endl;cout <<"6.销毁"<<endl;cout <<"7.打印"<<endl;cin >> a;switch(a){case (1):{cout <<"1.前插法创建链表"<<endl;cout <<"2.后插法创建链表"<<endl;cout <<"3.创建有序链表"<<endl;int b;cin >> b;switch (b){case 1:{int i,j;cout << "请输入创建元素数量";cin >> i;for (int k=1;k<=i;k++){cout << "请输入第"<<k<<"个元素的数据域";cin >>j;creat (j,head);}print(head);}break;case 2:{int i,j;LINK *p=head;;cout << "请输入创建元素数量";cin >> i;for (int k=1;k<=i;k++){cout << "请输入第"<<k<<"个元素的数据域";cin >>j;creat (j,p);p=p->next;}print(head);}break;case 3:{int i,j;LINK *p=head;cout << "请输入创建元素数量";cin >> i;for (int k=1;k<=i;k++){p=head;cout << "请输入第"<<k<<"个元素的数据域";cin >>j;while (p->next!=NULL){if (j < p->next->data)break;p = p->next;}creat (j,p);}print(head);}break;}}break;case (2):{cout << "输入所查找的链表元素位置"<<endl;int a;cin >> a;LINK*b = search(a,head);if (b != NULL)cout << "该节点数据域为"<<b->data<<endl;else cout <<"查询失败"<<endl;}break;case (3):{cout << "在第几个元素之后插入"<<endl;int a,c;cin >> a;cout << "输入这个元素的数据域为"<<endl;cin >>c;LINK*b = search (a,head);LINK*d = creat(c,b);if (b != NULL)print(head);else cout <<"插入失败"<<endl;}break;case (4):{   cout << "删除第几个元素?"<<endl;int a;cin >> a;LINK*b = search (a-1,head);if (b != NULL){LINK *s=b->next;b->next = s->next;delete(s);print (head);}else cout <<"删除失败"<<endl;}break;case (5):{cout << "排序算法:"<<endl;cout << "1.冒泡排序"<<endl;cout << "2.选择排序"<<endl;cout << "3.插入排序"<<endl;int i = length(head);int a;cin>>a;switch (a){case 1:{bubblesort(head,i);print(head);   }break;case 2:{selectionsort(head,i);print(head);   }break;case 3:{insertionsort(head,i);print(head);   }break;}break;}case (6):{destroy(head);}break;case (7):{print (head);}}}}LINK* creat(int element,LINK *first){LINK *ele;ele = new(LINK);ele -> data = element;ele -> next = first -> next;first -> next = ele;return first;}LINK* search (int j,LINK * head){LINK *tem;tem = head;int i=0;while (tem -> next !=NULL && i < j){tem = tem->next;i++;}if (tem != NULL && i==j)return tem;else return NULL;}void print (LINK* head) { int j=0; LINK *p; p = head->next; while (p != NULL) { cout << p ->data << " "; p = p -> next; j++; } cout << endl;}int length (LINK* head) { int j=0; LINK *p; p = head->next; while (p != NULL) { p = p -> next; j++; } return j;}LINK * bubblesort(LINK *head,int length){int i,j,tem;for (i=1;i <= length;i++)for (j=1;j <= length-i;j++)if (search(j,head) -> data > search(j+1,head) -> data){tem = search(j,head) -> data;search(j,head) -> data = search(j+1,head) -> data;search(j+1,head) -> data = tem;}return head; }LINK * selectionsort(LINK *head,int length){for (int i = 1;i <= length;i++){int tep = i;for (int j = i;j <= length;j++)if (search(j,head) -> data < search(tep,head) -> data)tep = j;int swap = search(i,head) -> data;search(i,head) -> data = search(tep,head) -> data;search(tep,head) -> data = swap;}return head;}LINK * insertionsort(LINK *head,int length){for (int i = 2;i <= length;i++)for (int j = 1;j < i;j++){if (search(j,head) -> data > search(i,head) -> data){LINK *previous_i = search(i-1,head);LINK *previous_j =  search(j-1,head);LINK *adress_i = search(i,head);LINK *adress_j = search(j,head);previous_i -> next = adress_i->next;previous_j -> next = adress_i;adress_i -> next = adress_j;break;}}return head;}LINK* destroy(LINK *head){LINK *s,*p;s = head->next;p = s;while (s != NULL){s =s -> next;delete(p);p=s;}head->next = NULL;return head;}

只是想留一个记录,之后会放出更好的版本的代码的。

0 0
原创粉丝点击