链表非降序列插入元素
2016-04-16 16:11
239 查看
#include <iostream> #include <stdio.h> #include <stdlib.h> using namespace std; struct node { int date; node *next; }; node *insert_(int k,node *head) { node * temp=(node *)malloc(sizeof(node)); temp->date=k; temp->next=NULL; node *last=head,*it; int flag=0; for(it=head->next;it!=NULL;it=it->next ) { if(temp->date<=it->date) { flag=1; last->next=temp; temp->next=it; break; } else last=it; } if(flag==0) { last->next=temp; } return head; } void shou(node *head) { node *l; l=head->next; while(l!=NULL) { cout<<l->date<<" "; l=l->next; } cout<<endl; } int main() { int n,k; node *head,*l,*p; head=(node*)malloc(sizeof(node)); while(~scanf("%d",&n)) { for(int i=0; i<n; i++) { p=(node*)malloc(sizeof(node)); cin>>p->date; p->next=NULL; if(i==0) head->next=l=p; else { l->next=p; l=p; } } shou(head); cin>>k; head=insert_(k,head); shou(head); } return 0; }
相关文章推荐
- python调试
- 【计算机组成原理】主板与总线
- Matab 灰度图 生成 伪彩色 图像 gray convert to pseudo color
- GitBook安装与使用
- 我的第一篇博客:Ubuntu之路(一)
- 【快速幂】HDU4506小明系列故事——师兄帮帮忙
- php
- Effective Java读书笔记 第一部分(第二章-第六章)
- 数据库字段内容存在'、"、>、<等特殊字符,转义html字符输出
- typedef和define
- 从底层看看HTTP模块的构建之net模块深入理解
- javascript练习题(二)
- PHP表单验证之必填字段--w3school学习笔记
- 使用Ceph作为OpenStack的后端存储
- 第一次写博客
- operator =注意事项
- 20145231第七周学习笔记
- 你了解System.out.println()的真正含义吗?
- 学习进度条
- linux mysql 忘记密码怎么办