数据结构第一次作业word版本文档格式.docx

上传人:b****6 文档编号:18565935 上传时间:2022-12-28 格式:DOCX 页数:27 大小:124.04KB
下载 相关 举报
数据结构第一次作业word版本文档格式.docx_第1页
第1页 / 共27页
数据结构第一次作业word版本文档格式.docx_第2页
第2页 / 共27页
数据结构第一次作业word版本文档格式.docx_第3页
第3页 / 共27页
数据结构第一次作业word版本文档格式.docx_第4页
第4页 / 共27页
数据结构第一次作业word版本文档格式.docx_第5页
第5页 / 共27页
点击查看更多>>
下载资源
资源描述

数据结构第一次作业word版本文档格式.docx

《数据结构第一次作业word版本文档格式.docx》由会员分享,可在线阅读,更多相关《数据结构第一次作业word版本文档格式.docx(27页珍藏版)》请在冰豆网上搜索。

数据结构第一次作业word版本文档格式.docx

if(strcmp(n,str1)==0)

{getchar();

return(0);

}

else

return

(1);

linklist*creatlist()

{

inta;

charch[10];

elemptypex;

linklist*head,*r,*p;

p=(linklist*)malloc(sizeof(linklist));

head=p;

p->

next=NULL;

r=p;

while

(1)

{

printf("

\n请输入学号:

\n"

scanf("

%d"

&

x.no);

\n请输入姓名:

%s"

x.name);

getchar();

\n请输入专业\n"

x.Snumber);

\n请输入电话号码\n"

x.Pnumber);

\n请输入QQ\n"

x.QQ);

\n请输入性别\n"

x.sex);

p=(linklist*)malloc(sizeof(linklist));

data=x;

r->

next=p;

r=r->

next;

a=start();

if(a==0)break;

system("

pause"

system("

cls"

}

return(head);

}

voidinsert(linklist*head,intx,elemptypey)

linklist*q,*p,*r;

r=(linklist*)malloc(sizeof(linklist));

r->

data=y;

if(head->

next==NULL)

head->

next=r;

else

q=head;

p=head->

while((p!

=NULL)&

&

(p->

data.no!

=x))

{

q=p;

p=p->

if(p!

=NULL)

q->

}

linklist*del(linklist*head,inti)

intj=0;

linklist*p,*s,*q;

p=head;

j=0;

while((p->

next!

(j<

i-1))

{p=p->

j++;

if(p->

q=p->

next=p->

next->

free(q);

returnNULL;

s=head;

returns;

linklist*locate(linklist*head,intk)

linklist*s;

s=head->

while(s!

if(s->

=k)

s=s->

break;

linklist*get(linklist*head,inti)

intj;

linklist*p;

j=0;

i))

p=p->

j++;

if((i==j)&

(i!

=0))returnp;

voidoutlin(linklist*h)

linklist*p;

p=h->

while(p!

%d,%s,%s,%s,%s,%s\n"

p->

data.no,p->

data.name,p->

data.Snumber,p->

data.Pnumber,p->

data.QQ,p->

data.sex);

\n输出结束\n"

voidMenu_one()

//主菜单

{

\n\n\n\n\n\n\n"

*************************************************\n"

存储学生信息的线性表\n\n"

1、用单链表来创建\n"

2、用顺序表来创建\n"

3、返回\n"

**************************************************\n"

\n\n\n\t\t"

voidMenu_two_2()

//次菜单

用单链表存储学生信息\n\n"

1、录入学生信息(只能录入一次!

)\n"

2、往线性表插入一个信息\n"

3、在线性表中删除一个信息\n"

4、按值检索\n"

5、按序号检索\n"

6、遍历线性表\n"

7、退出\n"

voidmain_switch_2(charj)

inti;

linklist*k;

elemptypem,n;

inta,b;

switch(j){

case'

1'

:

请添加学生信息\n"

head=creatlist();

outlin(head);

}break;

2'

printf("

\n在第几个信息之前插入?

(输入学号)"

\n请输入:

scanf("

m.no);

\n\n\n准备插入的新信息\n\n\n"

n.no);

n.name);

n.Snumber);

n.Pnumber);

n.QQ);

n.sex);

insert(head,m.no,n);

\n\n新的学生信息表\n"

outlin(head);

3'

\n请输入要删除第几个信息:

a);

\n新的信息表\n"

del(head,a);

}break;

case'

4'

\n请输入你要查找的学号:

k=locate(head,a);

k->

data.no,k->

data.name,k->

data.Snumber,k->

data.Pnumber,k->

data.QQ,k->

system("

5'

\n请输入你要查找的序号:

k=get(head,a);

6'

\n学生信息表\n\n"

7'

exit(0);

}

intmain1()

chara[100];

head=(linklist*)malloc(sizeof(linklist));

head->

//生成头结点

while

(1)

Menu_two_2();

\n\t请输入功能编号:

gets(a);

if(a[1]!

='

\0'

\n输入错误\n"

continue;

if(a[0]=='

0'

break;

main_switch_2(a[0]);

return0;

//-------以下为顺序表的函数--------------------------------------------------

usingnamespacestd;

structstudent1

intid;

//学号

charname[30];

//姓名

charsex[2];

//性别

floatgread;

//成绩

};

typedefstructstudent1DataType;

//指定structstudent为DataType

structSeqList

intMAX;

//顺序表中最大元素的个数

intcount;

//存放线性表中元素的个数count<

=MAXLENGTH

DataType*element;

//element[0],element[1],...,element[n-1]存放线性表中的元素

DataType*m;

typedefstructSeqList*MySeqList;

//初始化并创建空顺序表

//功能:

在顺序表中求某元素的下标,没有查找到,则返回-1

intlocateSeqList(MySeqListmySeqList,intid)

for(inti=0;

i<

mySeqList->

count;

++i)

if(mySeqList->

element[i].id==id)//传入一个元素x,查找到后返回下标i

return(i);

return(-1);

intupdateSeqList(MySeqListmySeqList,intid)//学生信息修改

intiRc=locateSeqList(mySeqList,id);

if(iRc==-1)

不存在指定下标!

return(0);

cout<

<

姓名:

cin>

>

mySeqList->

element[iRc].name;

学号:

"

element[iRc].id;

性别:

element[iRc].sex;

电话:

element[iRc].Pnumber;

QQ:

element[iRc].QQ;

成绩:

element[iRc].gread;

return1;

创建空顺序表

MySeqListinitSeqList(intm)

MySeqListmySeqList=(MySeqList)malloc(sizeof(structSeqList));

//分配内存空间

if(mySeqList!

=NULL)

element=(DataType*)malloc(sizeof(DataType)*m);

//为里面的元素分配m个DataType大小的内存空间,相当于初始化了一个长度为m的数组

element)

MAX=m;

//如果创建了元素,MAXLENGTH为最大元素的个数

count=0;

//空表长度为0

return(mySeqList);

free(mySeqList);

//记得要手动释放空间,否则很容易产生内存泄漏

内存空间不足,请关闭一些程序,然后再试!

//存储分配失败,提示空间不足

判断线性表是否为空

intisEmptySeqList(MySeqListmySeqList)

return(mySeqList->

count==0);

顺序表的pos下标后面插入,插入成功返回1,失败返回0

intinsertNextSeqList(MySeqListmySeqList,intpos,DataTypex)

if(pos<

0||pos>

=mySeqList->

count)

++mySeqList->

count>

MAX)

--mySeqList->

表产生了溢出!

for(inti=mySeqList->

count-1;

i!

=pos+1;

--i)

{mySeqList->

element[i]=mySeqList->

element[i-1];

//同样地,把pos+1插入位置及之后的元素均后移一个位置

element[i]=x;

//插入元素x

return

(1);

顺序表的删除(根据下标删除)

intdeleteSeqList(MySeqListmySeqList,intpos)

count)//不存在下标为pos的元素,注意下标范围是从0到count-1

for(inti=pos;

element[i+1];

//被删除元素之后的元素均前移一个位置

//元素个数减1

根据元素值删除,实现顺序表的删除

intdeleteSeqListByValue(MySeqListmySeqList,intid)

intpos=locateSeqList(mySeqList,id);

if(pos==-1)

deleteSeqList(mySeqList,pos);

//输出线性表的元素值

voidprintSeqList(MySeqList&

mySeqList)

++i)//输出线性表的元素值

<

element[i].id<

姓名:

element[i].name<

性别:

element[i].sex;

电话:

element[i].Pnumber<

QQ:

element[i].QQ<

成绩:

element[i].gread;

endl;

cout<

endl;

//根据学生id,输出线性表的元素值

voidprintSeqListById(MySeqList&

mySeqList,intid)

if(id==mySeqList->

element[i].id)

语文:

intmain2()

MySeqListmySeqList=initSeqList(20);

//初始化一个长20的表

L:

用顺序表存储学生信息\n\n"

1、录入学生信息\n"

2、查找学生信息\n"

3、删除学生信息\n"

4、修改学生信息\n"

5、遍历学生信息\n"

6、退出学生系统\n"

inti;

请选择一个操作(1-5):

i;

if(i==1)

count=1;

intiRc=0;

while(true&

count<

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 中医中药

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1