求解:线性表(基本线性表运算、线性表的逆置)C++
[size=24px][/size]求各位大神,解决这个问题
{实现基本线性表的创建、求基本线性表的长度、在基本线性表中查找某个数据元素、在某个位置插入一个新数据元素、在某个线性表中删除某个数据元素等操作。
分别以不同存储结构实现线性表的就地逆置。请把完整代码写给我,我是刚入门的菜鸟,再麻烦给点建议,数据结构(C++)怎么学比较好.....谢谢
[解决办法]
#include"stdio.h"#include"malloc.h"#define NULL 0#define L sizeof(struct integer)struct integer /*定义结构体*/{int num;int zhengshu; struct integer *next;};int n; //纪录链表的长度struct integer *creat(void) /*创建链表*/{ struct integer *head; struct integer *p1,*p2; n=0; p1=p2=(struct integer *)malloc(L); scanf("%d,%d",&p1->num,&p1->zhengshu); head=NULL; while(p1->num!=0){n=n+1; if(n==1) head=p1; else p2->next=p1; p2=p1; p1=(struct integer *)malloc(L); scanf("%d,%d",&p1->num,&p1->zhengshu);} p2->next=NULL; return(head);}void print(struct integer *head) /*打印链表中的数据*/{ struct integer *p; printf("Now %d biaohao and zhengshu are :n",n); p=head; if(head!=NULL) do {printf("%d,%5.1dn",p->num,p->zhengshu); p=p->next; }while(p!=NULL);}int count(struct integer *head) /*返回链表的长度*/{int i=0; struct integer *p;p=head;while(p!=NULL){p=p->next;i++;}return i;}void *findnode(struct integer *head,int num) /*查找链表中的第num个数据*/{int j=1;struct integer *p;/*if(head==NULL){printf("n空链表,请先创建!n");return;}*/p=head;if(num>count(head)||num<=0){printf("Input error! Please input againn");}else{while(p!=NULL && jnext;}printf("%d bianhao reprensts %dn",p->num,p->zhengshu);printf("n");}return(head);}struct integer *del(struct integer *head,long num) /*删除链表中的第num个数据*/{struct integer *p1,*p2; if(head==NULL){printf("nList Null!n"); return;} p1=head; while(num!=p1->num && p1->next!=NULL){ p2=p1; p1=p1->next;} if(num==p1->num) { if(p1==head) head=p1->next; else p2->next=p1->next; printf("Delete: %dn",num); n=n-1; } else printf("%d not been fonnd!n",num); return(head);}struct integer *insert(struct integer *head,struct integer *stud) /*插入数据*/{ struct integer *p0,*p1,*p2; p1=head; p0=stud; if(head==NULL){head=p0; p0->next=NULL;} else{while((p0->num>p1->num)&&(p1->next!=NULL)){p2=p1; p1=p1->next;}if(p0->num<=p1->num){ if(head==p1)head=p0; else p2->next=p0; p0->next=p1;} else{ p1->next=p0; p0->next=NULL;}} n=n+1; return(head);}main() /*主功能函数*/{int a,b; struct integer *head,stu; int del_num,fin_num;printf("1 to go on / 0 to exit:n");scanf("%d",&a);while(a!=0){/*struct integer *head,stu; int del_num;*/printf("1 creat 2 print 3 delete 4 insert 5 findonde 0 exitn");/*菜单的实现*/scanf("%d",&b);switch(b){case 1:{/*clrscr();*/printf("Please Input bianhao and data:n");printf("for example 1,90 0,0 to exit:n");head=creat();}break;case 2:{/*clrscr();*/print(head);}break;case 3:{/*clrscr();*/ printf("nInput the deleted biaohao:"); scanf("%d",&del_num); head=del(head,del_num);}break; case 4:{/*clrscr();*/ printf("nInput the inserted biaohao and zhengshu:"); scanf("%d,%d",&stu.num,&stu.zhengshu); head=insert(head,&stu);}break; case 5:{/*clrscr();*/printf("Please Input the bianhao you want to find:");scanf("%d",&fin_num);head=findnode(head,fin_num);}break;case 0:{ return;}break;default:printf("Input error! Please input againn");}} }
[解决办法]
//建立List链表Node *CreateList(){ Node *head = new Node(-1,NULL); int num; Node *tmp = head; cout<<"Input the Node's data:"; cin>>num; while(num != -1) { Node *p = new Node(num,NULL); tmp->next = p; tmp = p; cout<<"Input the Node's data:"; cin>>num; } cout<<"Create List finished!"<<endl; return head; }//寻找链表中最小项Node *FindMin(Node *head){ if(head->next == NULL) { cout<<"This list is NULL"<<endl; return NULL; } else { if(head->next->next == NULL) { cout<<"The Mix Node's data is: "<<head->next->data<<endl; return head->next; } else { Node *p = head->next; Node *q = p->next; while(q->next != NULL) { if((p->data > q->data) && (q->next != NULL)) { p = q; q = q->next; } else { q = q->next; } } cout<<"The Mix Node's data is:"<<p->data<<endl; return p; } }}//合并有序链表Node *UnionList(Node *a,Node *b){ if(a == NULL || b == NULL) { cout<<"Has NULL list!"<<endl; return NULL; } Node *pa = a->next; Node *pb = b->next; a->next = NULL; delete b; Node *tmp1 = a; Node *tmp2; while((pa != NULL) && (pb != NULL)) { if(pa->data < pb->data) { tmp2 = pa; pa = pa->next; tmp1->next = tmp2; tmp1 = tmp2; } else { tmp2 = pb; pb = pb->next; tmp1->next = tmp2; tmp1 = tmp2; } } if((pa == NULL)&&(pb != NULL)) { tmp1->next = pb; } if((pb == NULL)&&(pa != NULL)) { tmp1->next = pa; } return a;}//倒序合并有序链表Node *RUnionList(Node *a,Node *b){ if(a == NULL || b == NULL) { cout<<"Has NULL list!"<<endl; return NULL; } Node *pa = a->next; Node *pb = b->next; a->next = NULL; delete b; Node *tmp1; Node *tmp2; while((pa != NULL) && (pb != NULL)) { if(pa->data > pb->data) { tmp2 = pb; pb = pb->next; tmp2->next = a->next; a->next = tmp2; } else { tmp2 = pa; pa = pa->next; tmp2->next = a->next; a->next = tmp2; } } if((pa == NULL)&&(pb != NULL)) { while(pb) { tmp2 = pb; pb = pb->next; tmp2->next = a->next; a->next = tmp2; } } if((pb == NULL)&&(pa != NULL)) { while(pa) { tmp2 = pa; pa = pa->next; tmp2->next = a->next; a->next = tmp2; } } return a;}//合并有序数组void UnionArray(int a[],int b[],int m,int n,int used){ for(int i = 0;i < n;i++) { for(int j = 0;j < used;j++) { if(b[i] < a[j]) { for(int k = used-1;k >= j;k--) { a[k+1] = a[k]; } used++; a[j] = b[i]; break; } } }}//将数组中所有负数移至非负数前面void ReverseArray(int a[],int len){ int low = 0; int high = len-1; for(int i = 0;i < len/2;i++) { if((a[low] >= 0) && (a[high]) < 0) { int tmp = a[low]; a[low] = a[high]; a[high] = tmp; low++; high--; } else { if((a[low] < 0) && (a[high]) <= 0) { low++; } if((a[low] >= 0) && (a[high]) > 0) { high--; } if((a[low] < 0) && (a[high]) >= 0) { low++; high--; } } }}//寻找链表中最小项并删除之Node *FndANDDELMin(Node *head){ if(head->next == NULL) { cout<<"This list is NULL"<<endl; return NULL; } else { if(head->next->next == NULL) { cout<<"The Mix Node's data is: "<<head->next->data<<endl; Node *p = head->next; delete p; p = NULL; return head; } else { Node *pro = head; Node *p = head->next; Node *q = p->next; while(q->next != NULL) { if(p->data < q->next->data) { q = q->next; } else { p = q->next; pro = q; q = q->next->next; } } cout<<"The Mix is :"<<p->data<<endl; pro->next = p->next; delete p; p = NULL; return head; } }}//寻找链表中最小项并放置最前面Node *FndANDIRTMin(Node *head){ if(head->next == NULL) { cout<<"This list is NULL"<<endl; return NULL; } else { if(head->next->next == NULL) { cout<<"The Mix Node's data is: "<<head->next->data<<endl; Node *p = head->next; delete p; p = NULL; return head; } else { Node *pro = head; Node *p = head->next; Node *q = p->next; while(q->next != NULL) { if(p->data < q->next->data) { q = q->next; } else { p = q->next; pro = q; q = q->next->next; } } cout<<"The Mix is :"<<p->data<<endl; pro->next = p->next; p->next = head->next; head->next = p; return head; } }}//判断b是否为a子序列bool IsSubList(Node *a,Node *b){ if(a == NULL || b == NULL) { return false; } else { Node *p = a->next; Node *q = b->next; while((p != NULL) && (q != NULL)) { if(p->data != q->data) { p = p->next; } else { while(q != NULL) { if(p->data == q->data) { p = p->next; q = q->next; } else { q = b->next; break; } } } } if(q == NULL) { return true; } else { return false; } }}//删除无序表中相同的元素Node *DelSame(Node *head){ Node *pro = head->next; Node *p = head->next; cout<<"!!!!"<<p->data<<endl; Node *q = p->next; while(pro->next != NULL) { while(q != NULL) { if(q->data == p->data) { Node *tmp = q; pro->next = q->next; q = q->next; delete tmp; pro = pro->next; } else { pro = pro->next; q = q->next; } } p = p->next; pro = p; q = p->next; } return head;}//在A有序表中删除有序表A、B、C中共有的元素Node *DelUnoinSam(Node *a,Node *b,Node *c){ Node *pro = a; Node *pa = a->next; Node *pb = b->next; Node *pc = c->next; while(pa != NULL && pb != NULL && pc != NULL) { if((pa->data == pb->data) && (pa->data == pc->data)) { Node *tmp = pa; pro->next = pa->next; pa = pa->next; delete tmp; tmp = NULL; pb = pb->next; pc = pc->next; } else { if((pa->data == pb->data) && (pa->data > pc->data)) { pc = pc->next; } if((pa->data == pb->data) && (pa->data < pc->data)) { pro = pro->next; pa = pa->next; pb = pb->next; } if((pa->data == pc->data) && (pa->data > pb->data)) { pb = pb->next; } if((pa->data == pc->data) && (pa->data < pb->data)) { pro = pro->next; pa = pa->next; pc = pc->next; } if((pa->data > pb->data) && (pa->data < pc->data)) { pro = pro->next; pa = pa->next; pb = pb->next; } if((pa->data < pb->data) && (pa->data < pc->data)) { pro = pro->next; pa = pa->next; } if((pa->data > pb->data) && (pa->data > pc->data)) { pb = pb->next; pc =pc->next; } if((pa->data < pb->data) && (pa->data > pc->data)) { pro = pro->next; pa = pa->next; pc = pc->next; } } } return a;}
[解决办法]
http://blog.csdn.net/hackbuteer1/article/details/6591486
这里有楼主需要的完整代码、。。