《数据结构【实验报告】.doc》由会员分享,可在线阅读,更多相关《数据结构【实验报告】.doc(16页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、数据结构实验报告 学号:1.1线性表【数组】#include #define maxsize 10struct Studentchar num10;char sex;float score;class seqlistprivate:struct Student stumaxsize;int length;public:seqlist(struct Student a,int n);seqlist()void insert(int i,struct Student x);void Delete(int i);void print();seqlist:seqlist(struct Student
2、a,int n)/含参构造函数if(nmaxsize)couterrorendl;for(int i=0;in;i+) stui=ai;length=n;void seqlist:insert(int i,struct Student x)/插入函数if(ilength+1)couterror=i;j-)stuj=stuj-1;stui-1=x;length+;void seqlist:Delete(int i)/删除函数if(ilength+1)couterrorendl;for(int j=i;j=length+1;j+)stuj-1=stuj;length-;void seqlist:p
3、rint()for(int i=0;ilength;i+)coutstui.numtstui.sextstui.scoretendl;coutendl;void main()struct Student b=张三,M,82,李四,M,86,王二,F,88;struct Student c=孙大,F,89;seqlist s(b,3);s.print();s.insert(2,c);s.print();s.Delete(3);s.print();1.2线性表【链表】#include #include struct studentchar name8;char num10;float maths;
4、student *next;class STBpublic:STB();/初始化一个空链表/*STB(student b,int n);/头插法*/STB(student b,int n);/尾插法void Insert(student x,int i);/插入算法student Delete(int i);/删除算法void print();private:struct student *first; ;STB:STB()first=new student;first-next=NULL;/*STB:STB(student b,int n)/头插法first=new student;firs
5、t-next=NULL;struct student *s; for(int i=0;iname,bi.name); strcpy(s-num,bi.num); s-maths=bi.maths; s-next=first-next; first-next=s; */STB:STB(student b,int n)/尾插法first=new student; first-next=NULL;struct student *r;r=first;struct student *s;for(int i=0;iname,bi.name); strcpy(s-num,bi.num); s-maths=b
6、i.maths; r-next=s; r=s;r-next=NULL;void STB:Insert(student x,int i)/插入算法struct student *p=first;int count= 0;while(p!=NULL&countnext;count+;student *s;s=new student;strcpy(s-name,x.name);strcpy(s-num,x.num);s-maths=x.maths;s-next=p-next;p-next=s;student STB:Delete(int i)/删除算法struct student *p=first;
7、int count =0;while(p!=NULL&countnext;count+;struct student *q;struct student y;q=p-next;strcpy(y.name,q-name);strcpy(y.num,q-num);y.maths=q-maths;p-next=q-next;delete q;return y;void STB:print()struct student *p=first-next;while(p !=NULL)coutnametnumtmathsnext;coutendl;int main() student b3=张三,87,李四
8、,96,王二,99; struct student c =孙大,38;STB t(b,3);t.print();t.Insert( c,2);t.print();t.Delete(3);t.print();return 0;2.1队列【数组】#include #define maxsize 10struct Studentchar num10;char sex;float score;class seqlistprivate:struct Student stumaxsize;int length;public:seqlist(struct Student a,int n);seqlist()
9、void insert(int i,struct Student x);void Delete(int i);void print();seqlist:seqlist(struct Student a,int n)/含参构造函数if(nmaxsize)couterrorendl;for(int i=0;in;i+) stui=ai;length=n;void seqlist:insert(int i,struct Student x)/插入函数if(ilength+1)couterror=i;j-)stuj=stuj-1;stui-1=x;length+;void seqlist:Delete
10、(int i)/删除函数if(ilength+1)couterrorendl;for(int j=i;j=length+1;j+)stuj-1=stuj;length-;void seqlist:print()for(int i=0;ilength;i+)coutstui.numtstui.sextstui.scoretendl;coutendl;void main()struct Student b=张三,M,82,李四,M,86,王二,F,88;struct Student c=孙大,F,89;seqlist s(b,3);s.print();s.insert(2,c);s.print()
11、;s.Delete(3);s.print();2.2队列【链表】#include struct studentint num;char sex;struct student *next;class link_queueprivate:struct student *front ,*rear;public:link_queue();link_queue()void Enqueue(struct student x);void Outqueue();void Empty();link_queue:link_queue()struct student *s;s=new struct student;
12、s-next=NULL;front=rear=s;void link_queue:Enqueue(struct student x)struct student *p;p=new struct student;p-num=x.num;p-sex=x.sex;p-next=NULL;rear-next=p;void link_queue:Outqueue()if(rear=front)couterrornext;x.num=p-num;x.sex=p-sex;front-next=p-next;delete p;coutx.numtx.sexendl;void link_queue:Empty(
13、)if(front=rear)cout空endl;elsecout不空endl;void main()struct student a=15789,M;link_queue s;s.Enqueue(a);s.Outqueue();s.Empty();3.1栈【数组】#include #define N 10struct studentint num;float score;class seq_stackprivate:int top;struct student AN;public:seq_stack();void push(struct student a);void pop();int E
14、mpty();int Full();seq_stack:seq_stack()/构造函数top=-1;void seq_stack:push(struct student a)/入栈if(top=N-1)couterror;for(int i=0;i=3;i+)A+top=ai;void seq_stack:pop()/出栈struct student x;int top=3;while(top!=-1)x=Atop-;coutx.numtx.scoretendl;int seq_stack:Empty()/判断栈空if(top=-1)cout空endl;elsecout不空endl;retu
15、rn 0;int seq_stack:Full()/判断栈满int i;if(top=N-1)i=1;elsei=0;return i;void main()struct student b=5536,88,5537,86,5538,92,5539,91;seq_stack s;s.push(b);s.pop();s.Empty();s.Full();3.2栈【链表】#include #define N 10struct studentint num;float score;class seq_stackprivate:int top;struct student AN;public:seq_
16、stack();void push(struct student a);void pop();int Empty();int Full();seq_stack:seq_stack()/构造函数top=-1;void seq_stack:push(struct student a)/入栈if(top=N-1)couterror;for(int i=0;i=3;i+)A+top=ai;void seq_stack:pop()/出栈struct student x;int top=3;while(top!=-1)x=Atop-;coutx.numtx.scoretendl;int seq_stack
17、:Empty()/判断栈空if(top=-1)cout空endl;elsecout不空endl;return 0;int seq_stack:Full()/判断栈满int i;if(top=N-1)i=1;elsei=0;return i;void main()struct student b=5536,88,5537,86,5538,92,5539,91;seq_stack s;s.push(b);s.pop();s.Empty();s.Full();4.0二叉树#include #define N 10struct studentint num;float score;class seq_
18、stackprivate:int top;struct student AN;public:seq_stack();void push(struct student a);void pop();int Empty();int Full();seq_stack:seq_stack()/构造函数top=-1;void seq_stack:push(struct student a)/入栈if(top=N-1)couterror;for(int i=0;i=3;i+)A+top=ai;void seq_stack:pop()/出栈struct student x;int top=3;while(to
19、p!=-1)x=Atop-;coutx.numtx.scoretendl;int seq_stack:Empty()/判断栈空if(top=-1)cout空endl;elsecout不空endl;return 0;int seq_stack:Full()/判断栈满int i;if(top=N-1)i=1;elsei=0;return i;void main()struct student b=5536,88,5537,86,5538,92,5539,91;seq_stack s;s.push(b);s.pop();s.Empty();s.Full();结论;数组和链表主要的区别就在于数据空间内存的不同。数组需要的存储空间要预先分配,因此要注意分配的空间的大小,存储空间位置连续。但插入删除效率较低。链表不需要预先分配存储空间,存储空间动态分配。插入删除操作方便。存储空间位置不连续。从实现角度数组的实现比较简单,链表需要基于指针。所以,主要操作是查找的运用数组的手段,而频繁插入删除运算的选择链表来进行操作。
限制150内