搜索
首页 电脑/网络 程序设计 C/C++

线性表的操作

建立一个含26个英文字母的数据元素的线性表并输出该表中各元素的值及查找、插入、删除及顺序表的长度等操作。要求:1)分别用顺序表和链表实现线性表的各种操作; 2)链表的建立要求用逆位序和正位序分别实现;3)采用动态内存空间分配。

全部回答

2013-03-16

215 0
    顺序表:#includeconst int maxsize=1000;struct sequenlist{ char a[maxsize]; int len;};sequenlist setnull(sequenlist L){L。
  len=0;return L;}sequenlist insert(sequenlist L,char x, int i){ int j; if(L。  len>=maxsize-1) coutL。
  len+1)) cout=i;j--) L。a[j+1]=L。a[j]; L。a[i]=x; L。len++; } return L;}sequenlist deletel(sequenlist L,int i){int j;if((iL。
    len)) coutL。len)) return NULL;else return L。a [i];}void main(){sequenlist L;char x,y;int n;L=setnull(L);cout>n;for(int j=1;j>L。
  a[j];L。  len=n;print(L);cout>x>>j;L=insert(L,x,j);print(L);cout>j;L=deletel(L,j);print(L);cout>x;j=locate(L,x);if(j!=0)cout>x;cout>y;L=change(L,x,y);print(L);cout>j;x=get(L,j);coutclass link{public: char data; link *next;};class linklist{protected: link *head;public: link *hcreat() { link *s,*p; char i; cout>i; p=new link; p->next=NULL; while(i!='0') { s=new link; s->data=i; s->next=p->next; p->next=s; cin>>i; } return p; } void print(link *head) { link *p; p=head->next ; while(p->next!=NULL) { coutdata"; p=p->next; } coutdata ; coutnext ; while((p!=NULL)&&(p->data!=x)) p=p->next ; return p; } void deletel(link *head,char x) { link *p,*q; q=head; p=head->next; while((p!=NULL)&&(p->data!=x)) { q=p; p=p->next ; } if(p==NULL)coutnext =p->next ; delete(p); } } void insert(link *head,char x,char y) { link *p,*s; s=new link; s->data =y; if(head->next==NULL) { head->next=s; s->next=NULL; } p=Locate(head,x); if(p!=NULL) coutnext =p->next ; p->next ; } } void change(link *p,char x, char y) { link *q; q=p->next ; while(q!=NULL) { if(q->data==x)q->data=y; q=q->next ; } } int count(link *h) { link *p;int n=0; p=h->next ; while(p!=NULL) { n++; p=p->next ; } return n; }};void main(){int n;char x,y;link *p,*q;linklist a;p=a。
    hcreat (); int(p);cout>y; letel (p,y); int (p);cout>x;cout>y; sert (p,x,y); int (p);cout>x>>y; ange (p,x,y); int (p);cout>x;q=a。
    Locate (p,x);if(q==NULL)cout>i; p=new link; p->next=NULL; while(i!='0') { s=new link; s->data=i; r->next=s; r=s; cin>>i; } r->next=NULL; return p; }。
  

2013-03-16

213 0
    顺序表操作代码如下:#includeconst int maxsize=1000;struct sequenlist{ char a[maxsize]; int len;};sequenlist setnull(sequenlist L){L。
  len=0;return L;}sequenlist insert(sequenlist L,char x, int i){ int j; if(L。  len>=maxsize-1) coutL。
  len+1)) cout=i;j--) L。a[j+1]=L。a[j]; L。a[i]=x; L。len++; } return L;}sequenlist deletel(sequenlist L,int i){int j;if((iL。
    len)) coutL。len)) return NULL;else return L。a [i];}void main(){sequenlist L;char x,y;int n;L=setnull(L);cout>n;for(int j=1;j>L。
  a[j];L。  len=n;print(L);cout>x>>j;L=insert(L,x,j);print(L);cout>j;L=deletel(L,j);print(L);cout>x;j=locate(L,x);if(j!=0)cout>x;cout>y;L=change(L,x,y);print(L);cout>j;x=get(L,j);coutclass link{public: char data; link *next;};class linklist{protected: link *head;public: link *hcreat() { link *s,*p; char i; cout>i; p=new link; p->next=NULL; while(i!='0') { s=new link; s->data=i; s->next=p->next; p->next=s; cin>>i; } return p; } void print(link *head) { link *p; p=head->next ; while(p->next!=NULL) { coutdata"; p=p->next; } coutdata ; coutnext ; while((p!=NULL)&&(p->data!=x)) p=p->next ; return p; } void deletel(link *head,char x) { link *p,*q; q=head; p=head->next; while((p!=NULL)&&(p->data!=x)) { q=p; p=p->next ; } if(p==NULL)coutnext =p->next ; delete(p); } } void insert(link *head,char x,char y) { link *p,*s; s=new link; s->data =y; if(head->next==NULL) { head->next=s; s->next=NULL; } p=Locate(head,x); if(p!=NULL) coutnext =p->next ; p->next ; } } void change(link *p,char x, char y) { link *q; q=p->next ; while(q!=NULL) { if(q->data==x)q->data=y; q=q->next ; } } int count(link *h) { link *p;int n=0; p=h->next ; while(p!=NULL) { n++; p=p->next ; } return n; }};void main(){int n;char x,y;link *p,*q;linklist a;p=a。
    hcreat (); int(p);cout>y; letel (p,y); int (p);cout>x;cout>y; sert (p,x,y); int (p);cout>x>>y; ange (p,x,y); int (p);cout>x;q=a。
    Locate (p,x);if(q==NULL)cout>i; p=r=new link; p->next=NULL; while(i!='0') { s=new link; s->data=i; r->next=s; r=s; cin>>i; } r->next=NULL; return p; }以上程序皆可以在vc 6 上运行。
  

2013-03-12

186 0
不难,但是工作量太多了

类似问题换一批

热点推荐

热度TOP

相关推荐
加载中...

热点搜索 换一换

电脑/网络
C/C++
程序设计
电脑装机
操作系统/系统故障
硬件
笔记本电脑
百度
互联网
反病毒
软件
程序设计
C/C++
数据库
VB
JAVA相关
C#/.NET
VC++
汇编语言
其他编程语言
C/C++
C/C++
举报
举报原因(必选):
取消确定举报