欢迎来到天天文库
浏览记录
ID:61905079
大小:32.50 KB
页数:9页
时间:2021-03-26
《实验0-线性表-二叉树的基本操作1.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、;elseif((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-1]=L.a[j];L.len--;}}//置空线性表voidSetnull(sequenlist&L){L.len=0;}//在线性表中查找x元素的位置intLocate(sequenlistL,elemtypex){intj=1;while((j8、9、(i>L.len))returnNULL;e10、lsereturnL.a[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;}retur11、np;}Node*creat(void)//尾插法{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;}//单12、链表head中查找第i个位置上的元素Node*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-13、>next=s;}p=Locate(head,y);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<<"thenodeisnote14、xist!"<next=p->next;deletep;}}//输出单链表voidprint(Node*head){Node*p;p=head->next;while(p->next!=NULL){cout<data<<"->";p=p->next;}cout<data;}1.顺序结构队列的基本操作#include
2、;elseif((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-1]=L.a[j];L.len--;}}//置空线性表voidSetnull(sequenlist&L){L.len=0;}//在线性表中查找x元素的位置intLocate(sequenlistL,elemtypex){intj=1;while((j8、9、(i>L.len))returnNULL;e10、lsereturnL.a[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;}retur11、np;}Node*creat(void)//尾插法{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;}//单12、链表head中查找第i个位置上的元素Node*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-13、>next=s;}p=Locate(head,y);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<<"thenodeisnote14、xist!"<next=p->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-1]=L.a[j];L.len--;}}//置空线性表voidSetnull(sequenlist&L){L.len=0;}//在线性表中查找x元素的位置intLocate(sequenlistL,elemtypex){intj=1;while((j8、9、(i>L.len))returnNULL;e10、lsereturnL.a[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;}retur11、np;}Node*creat(void)//尾插法{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;}//单12、链表head中查找第i个位置上的元素Node*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-13、>next=s;}p=Locate(head,y);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<<"thenodeisnote14、xist!"<next=p->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;e
10、lsereturnL.a[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;}retur
11、np;}Node*creat(void)//尾插法{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;}//单
12、链表head中查找第i个位置上的元素Node*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-
13、>next=s;}p=Locate(head,y);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<<"thenodeisnote
14、xist!"<next=p->next;deletep;}}//输出单链表voidprint(Node*head){Node*p;p=head->next;while(p->next!=NULL){cout<data<<"->";p=p->next;}cout<data;}1.顺序结构队列的基本操作#include
此文档下载收益归作者所有