欢迎来到天天文库
浏览记录
ID:59189096
大小:25.50 KB
页数:9页
时间:2020-10-30
《实验0线性表二叉树的基本操作.doc》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、1.普通线性表顺序存储结构的基本操作#includeusingnamespacestd;#defineelemtypeintstructsequenlist{elemtypea[maxsize];intlen;};//求线性表的长度intLength(sequenlistL){returnL.len;}//在线性表第i个位置插入新元素xvoidInsert(sequenlist&L,elemtypex,inti){if(L.len>=maxsize-1cout<<"overflow"<2、eif((i<1)3、4、(i>L.len+1))cout<<"positionisnotcorrect!"<=i;j--)L.a[j+1]=L.a[j];L.a[i]=x;L.len++;}}//将线性表第i个元素删除voidDelete(sequenlist&L,inti){intj;if((j<1)5、6、(i>L.len))cout<<"positionisnotcorrect!"<7、[j];L.len--;}}//置空线性表voidSetnull(sequenlist&L){L.len=0;}//在线性表中查找x元素的位置intLocate(sequenlistL,elemtypex){intj=1;while((j8、9、(i>L.len))returnNULL;elsereturnL.a10、[i];}1.普通线性表链式存储结构的基本操作#includeusingnamespacestd;#defineelemtypeinttypedefstructnodeNode;structnode{elemtypedata;Node*next;};//建立链表Node*creat(void)//头插法{Node*s,*p;inti;cin>>i;p=NULL;while(i){s=newNode;s->data=x;s->next=p;p=s;cin>>i;}returnp;}Node*creat(v11、oid)//尾插法{Node*s,*r,*p;inti;p=r=newNode;p->next=NULL;cin>>i;while(i){s=newNode;s->data=i;r->next=s;r=s;cin>>i;}returnp;}//单链表head中,查找值为x的结点Node*Locate(Node*head,elemtypex){Node*p;p=head->next;while((p!=NULL)&&(p->data!=x))p=p->next;returnp;}//单链表head中查找第i个位置上的元素No12、de*Get(Node*head,inti){intj;Node*p;j=1;p=head->next;while((jnext;}returnp;}//在单链表head中,在值为y的结点之后插入值为x的结点voidInsert(Node*head,elemtypex,elemtypey){Node*p,*s;s=newNode;s->data=x;s->next=NULL;if(head->next==NULL){head->next=s;}p=Locate(head,y13、);if(p==NULL)cout<<"positionisillegal"<next=p->next;p->next=s;}}//在单链表head中,删除值为x的结点voidDelete(Node*head,elemtypex){Node*p,*q;q=head;p=head->next;while((p!=NULL)&&(p->data!=x){p=p->next;q=p;}if(p==NULL)cout<<"thenodeisnotexist!"<next=p-14、>next;deletep;}}//输出单链表voidprint(Node*head){Node*p;p=head->next;while(p->next!=NULL){cout<data<<"->";p=p->next;}cout<data;}1.顺序结构队列的基本操作#include
2、eif((i<1)
3、
4、(i>L.len+1))cout<<"positionisnotcorrect!"<=i;j--)L.a[j+1]=L.a[j];L.a[i]=x;L.len++;}}//将线性表第i个元素删除voidDelete(sequenlist&L,inti){intj;if((j<1)
5、
6、(i>L.len))cout<<"positionisnotcorrect!"<7、[j];L.len--;}}//置空线性表voidSetnull(sequenlist&L){L.len=0;}//在线性表中查找x元素的位置intLocate(sequenlistL,elemtypex){intj=1;while((j8、9、(i>L.len))returnNULL;elsereturnL.a10、[i];}1.普通线性表链式存储结构的基本操作#includeusingnamespacestd;#defineelemtypeinttypedefstructnodeNode;structnode{elemtypedata;Node*next;};//建立链表Node*creat(void)//头插法{Node*s,*p;inti;cin>>i;p=NULL;while(i){s=newNode;s->data=x;s->next=p;p=s;cin>>i;}returnp;}Node*creat(v11、oid)//尾插法{Node*s,*r,*p;inti;p=r=newNode;p->next=NULL;cin>>i;while(i){s=newNode;s->data=i;r->next=s;r=s;cin>>i;}returnp;}//单链表head中,查找值为x的结点Node*Locate(Node*head,elemtypex){Node*p;p=head->next;while((p!=NULL)&&(p->data!=x))p=p->next;returnp;}//单链表head中查找第i个位置上的元素No12、de*Get(Node*head,inti){intj;Node*p;j=1;p=head->next;while((jnext;}returnp;}//在单链表head中,在值为y的结点之后插入值为x的结点voidInsert(Node*head,elemtypex,elemtypey){Node*p,*s;s=newNode;s->data=x;s->next=NULL;if(head->next==NULL){head->next=s;}p=Locate(head,y13、);if(p==NULL)cout<<"positionisillegal"<next=p->next;p->next=s;}}//在单链表head中,删除值为x的结点voidDelete(Node*head,elemtypex){Node*p,*q;q=head;p=head->next;while((p!=NULL)&&(p->data!=x){p=p->next;q=p;}if(p==NULL)cout<<"thenodeisnotexist!"<next=p-14、>next;deletep;}}//输出单链表voidprint(Node*head){Node*p;p=head->next;while(p->next!=NULL){cout<data<<"->";p=p->next;}cout<data;}1.顺序结构队列的基本操作#include
7、[j];L.len--;}}//置空线性表voidSetnull(sequenlist&L){L.len=0;}//在线性表中查找x元素的位置intLocate(sequenlistL,elemtypex){intj=1;while((j8、9、(i>L.len))returnNULL;elsereturnL.a10、[i];}1.普通线性表链式存储结构的基本操作#includeusingnamespacestd;#defineelemtypeinttypedefstructnodeNode;structnode{elemtypedata;Node*next;};//建立链表Node*creat(void)//头插法{Node*s,*p;inti;cin>>i;p=NULL;while(i){s=newNode;s->data=x;s->next=p;p=s;cin>>i;}returnp;}Node*creat(v11、oid)//尾插法{Node*s,*r,*p;inti;p=r=newNode;p->next=NULL;cin>>i;while(i){s=newNode;s->data=i;r->next=s;r=s;cin>>i;}returnp;}//单链表head中,查找值为x的结点Node*Locate(Node*head,elemtypex){Node*p;p=head->next;while((p!=NULL)&&(p->data!=x))p=p->next;returnp;}//单链表head中查找第i个位置上的元素No12、de*Get(Node*head,inti){intj;Node*p;j=1;p=head->next;while((jnext;}returnp;}//在单链表head中,在值为y的结点之后插入值为x的结点voidInsert(Node*head,elemtypex,elemtypey){Node*p,*s;s=newNode;s->data=x;s->next=NULL;if(head->next==NULL){head->next=s;}p=Locate(head,y13、);if(p==NULL)cout<<"positionisillegal"<next=p->next;p->next=s;}}//在单链表head中,删除值为x的结点voidDelete(Node*head,elemtypex){Node*p,*q;q=head;p=head->next;while((p!=NULL)&&(p->data!=x){p=p->next;q=p;}if(p==NULL)cout<<"thenodeisnotexist!"<next=p-14、>next;deletep;}}//输出单链表voidprint(Node*head){Node*p;p=head->next;while(p->next!=NULL){cout<data<<"->";p=p->next;}cout<data;}1.顺序结构队列的基本操作#include
8、
9、(i>L.len))returnNULL;elsereturnL.a
10、[i];}1.普通线性表链式存储结构的基本操作#includeusingnamespacestd;#defineelemtypeinttypedefstructnodeNode;structnode{elemtypedata;Node*next;};//建立链表Node*creat(void)//头插法{Node*s,*p;inti;cin>>i;p=NULL;while(i){s=newNode;s->data=x;s->next=p;p=s;cin>>i;}returnp;}Node*creat(v
11、oid)//尾插法{Node*s,*r,*p;inti;p=r=newNode;p->next=NULL;cin>>i;while(i){s=newNode;s->data=i;r->next=s;r=s;cin>>i;}returnp;}//单链表head中,查找值为x的结点Node*Locate(Node*head,elemtypex){Node*p;p=head->next;while((p!=NULL)&&(p->data!=x))p=p->next;returnp;}//单链表head中查找第i个位置上的元素No
12、de*Get(Node*head,inti){intj;Node*p;j=1;p=head->next;while((jnext;}returnp;}//在单链表head中,在值为y的结点之后插入值为x的结点voidInsert(Node*head,elemtypex,elemtypey){Node*p,*s;s=newNode;s->data=x;s->next=NULL;if(head->next==NULL){head->next=s;}p=Locate(head,y
13、);if(p==NULL)cout<<"positionisillegal"<next=p->next;p->next=s;}}//在单链表head中,删除值为x的结点voidDelete(Node*head,elemtypex){Node*p,*q;q=head;p=head->next;while((p!=NULL)&&(p->data!=x){p=p->next;q=p;}if(p==NULL)cout<<"thenodeisnotexist!"<next=p-
14、>next;deletep;}}//输出单链表voidprint(Node*head){Node*p;p=head->next;while(p->next!=NULL){cout<data<<"->";p=p->next;}cout<data;}1.顺序结构队列的基本操作#include
此文档下载收益归作者所有