数据的插入与删除

来源:互联网 发布:linux wpa supplicant 编辑:程序博客网 时间:2024/05/16 18:45


在一组数据(数目不超过10000中,插入新数,删除所有与给定数相等的数据。

输入

第一行是未排序的一组非负整数,数目不超过10000。以-1作为结束标志。

第二行是要插入的数。

第三行是要删除的数。

输出

第一行输出自小到大排好序的数。如果没有元素,输出“No elements.”(不包括引号)。

第二行输出插入后自小到大排好序的数,以“,”隔开。

第三行输出删除后自小到大排好序的数,以“,”隔开。如果没有元素,输出“No elements.”(不包括引号)。

样例输入

100 98 79 63 44 99 -1
88
79

样例输出

44,63,79,98,99,100
44,63,79,88,98,99,100
44,63,88,98,99,100


//1014#include <iostream>#include <cstdlib>using namespace std;typedef struct LNode{int data;struct LNode *next;}LNode,*LinkList;int Sort(LinkList &L){if(!L||!L->next){return 0;}LinkList end,p,pre;int temp;pre=L->next;p=pre->next;end=L->next;while(end){end=end->next;}while(p!=end){while(p!=end){if(p->data<pre->data){temp=pre->data;pre->data=p->data;p->data=temp;}pre=pre->next;p=p->next;}end=pre;pre=L->next;p=pre->next;}return 0;}//冒泡 或者找出最大的重新插入int Insert(LinkList &L,int e){LinkList p,pre,q;pre=L;p=L->next;while(p&&p->data<e){p=p->next;pre=pre->next;}q=(LinkList)malloc(sizeof(LNode));q->data=e;q->next=pre->next;pre->next=q;return 0;}int Delete(LinkList &L,int e){LinkList p,pre;int i=0;pre=L;p=L->next;while(p){if(p->data==e){pre->next=pre->next->next;free(p);p=pre->next;++i;}else{pre=pre->next;p=p->next;}}return i;}int Print(const LinkList &L){LinkList p=L->next;if(!p){cout<<"No elements."<<endl;return 0;}while(p->next){cout<<p->data<<",";p=p->next;}cout<<p->data<<endl;return 0;}int main(){LinkList L,p;int i,e,m;L=(LinkList)malloc(sizeof(LNode));L->next=NULL;while(1){cin>>i;if(-1==i){break;}p=(LinkList)malloc(sizeof(LNode));p->data=i;p->next=L->next;L->next=p;}cin>>e;cin>>m;Sort(L);Print(L);Insert(L,e);Print(L);if(Delete(L,m)!=0){Print(L);}else{cout<<"No elements."<<endl;}return 0;}

 PS:切忌将指针与一未定义的指针进行比较 因为结果不知道会发生什么 哎 数据结构都忘了  又要重新看了 算法效率仍然不高


0 0
原创粉丝点击