链表升序

来源:互联网 发布:windows消息机制详解 编辑:程序博客网 时间:2024/04/29 06:39

建立一个升序链表并遍历输出

struct LNode{int data;LNode* next;};void Insert(LNode* List, int data){LNode* tmp=new LNode;tmp->data = data;tmp->next = NULL;LNode* pre, *now;now = List->next;pre = List;while (now!=NULL&&now->data<data){pre = now;now = now->next;}tmp->next = now;pre->next = tmp;}void slove(){int x,n;LNode * head = new LNode;while (~scanf("%d",&n)){head->next = NULL;head->data = 0;for (int i = 0; i < n; ++i){scanf("%d", &x);Insert(head, x);}LNode *now = head->next;while (now){if (now->next == NULL)printf("%d", now->data);elseprintf("%d ", now->data);now = now->next;}printf("\n");}}


0 0
原创粉丝点击