首页 > 代码库 > 单链表(建立、插入、删除、排序、逆置、打印)
单链表(建立、插入、删除、排序、逆置、打印)
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <curses.h>using namespace std;typedef struct student{ int data; struct student *next;}node;node * creat(void){ node *head,*p,*s; int x,cycle=1; head=(node*)malloc(sizeof(node)); p=head; while(cycle) { printf("\nplease input the data( 0 means stop ):"); scanf("%d",&x); if(x) { s=(node *)malloc(sizeof(node)); s->data=http://www.mamicode.com/x; printf("\n%d",s->data); p->next=s; p=s; } else cycle=0; //input x=0,then cycle=0,the for cycle is over; } head=head->next; //remove header p->next=NULL;return head;}int length(node *head) //measuring the length of list{ int n=0; node *p; p=head; while(p) { p=p->next; n++; } return n;}int print(node *head) //print the list{ node *p; int n=0,i=1; n=length(head); printf("\nNow.These %d records are: ",n); p=head; if(!head) return -1; while(p) { printf("\n %d ",p->data); p=p->next; }}node * del(node *head,int num) //delete the node whose data =http://www.mamicode.com/= num{ node *p1,*p2; p1=head; while(num!=p1->data && p1->next!=NULL) { p2=p1; p1=p1->next; } if(num == p1->data) { if(p1 == head) { head=p1->next; free(p1); } else p2->next=p1->next; } else printf("\n%d could not been found",num); return head;}node * insert(node *head,int num){ node *p0,*p1,*p2; p1=head; p0=(node *)malloc(sizeof(node)); p0->data=http://www.mamicode.com/num; while(p0->data > p1->data && p1->next != NULL) { p2=p1; p1=p1->next; } if(p0->data <= p1->data) { if(head == p1) { p0->next=p1; head=p0; } else { p0->next=p1; p2->next=p0; } } else { p1->next=p0; p0->next=NULL; } return head;}node * sort(node *head){ node *p,*p2,*p3; int n,temp; n=length(head); if(head==NULL || head->next==NULL) return head; p=head; for(int j=1;j<n;++j) { p=head; for(int i=0;i<n-j;++i) { if(p->data > p->next->data) { temp = p->data; p->data = http://www.mamicode.com/p->next->data; p->next->data =http://www.mamicode.com/ temp; } p = p->next; } } return head;}node * reverse (node *head){ node *p1,*p2,*p3; if(head==NULL || head->next==NULL) return head; p1 = head; p2 = p1->next; while(p2) { p3 = p2->next; p2->next = p1; p1=p2; p2=p3; } head->next = NULL; head=p1; return head;}int main (int argc, char **argv){ node *head; int num,n; while(1) { printf("\n----------------"); printf("\n******Menu******|"); printf("\n0:exit |"); printf("\n1:cerat list |"); printf("\n2:delete a node |"); printf("\n3:insert a node |"); printf("\n4:print list |"); printf("\n5:sort list |"); printf("\n6:reverse list |"); printf("\n----------------"); printf("\n please chose your number listed above: "); scanf("%d",&num); switch(num) { case 0: return 0; case 1: head=creat(); break; case 2: printf("\nchose the number you want delete: "); scanf("%d",&n); head=del(head,n); break; case 3: printf("\nchose the number you want insert: "); scanf("%d",&n); head=insert(head,n); break; case 4: print(head); break; case 5: head=sort(head); break; case 6: head=reverse(head); break; } }}
单链表(建立、插入、删除、排序、逆置、打印)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。