数据结构六个主要算法程序c版.docx
- 文档编号:29134381
- 上传时间:2023-07-20
- 格式:DOCX
- 页数:18
- 大小:16.98KB
数据结构六个主要算法程序c版.docx
《数据结构六个主要算法程序c版.docx》由会员分享,可在线阅读,更多相关《数据结构六个主要算法程序c版.docx(18页珍藏版)》请在冰豆网上搜索。
数据结构六个主要算法程序c版
1.顺序表
#include
#include
typedefstruct
{
intnum;
charname[20];
}Sqstu;
Sqstustu[4];
Sqstustud;
voidcreat();
voidinsert();
voiddel();
voidmain()
{
creat();
printf("请输入要插入学生的信息:
\n");
insert();
printf("请输入要删除学生的学号:
\n");
del();
}
voidcreat()
{
inti;
for(i=0;i<3;i++)
scanf("%d%s",&stu[i].num,stu[i].name);
printf("学生的信息为:
\n");
for(i=0;i<3;i++)
printf("%-10d%s\n",stu[i].num,stu[i].name);
}
voidinsert()
{
inti,j;
scanf("%d%s",&stud.num,stud.name);
for(i=0;stud.num>stu[i].num;i++)
j=i;
for(i=3;i>j;i--)
{
stu[i].num=stu[i-1].num;
strcpy(stu[i].name,stu[i-1].name);
}
stu[j+1].num=stud.num;
strcpy(stu[j+1].name,stud.name);
printf("现在,学生的信息为:
\n");
for(i=0;i<4;i++)
printf("%-10d%s\n",stu[i].num,stu[i].name);
}
voiddel()
{
inti;
scanf("%d",&stud.num);
for(i=0;stud.num!
=stu[i].num;i++)
i=i;
for(i;i<4;i++)
{
stu[i]=stu[i+1];
}
printf("现在,学生的信息为:
\n");
for(i=0;i<3;i++)
printf("%-10d%s\n",stu[i].num,stu[i].name);
}
2.链表
#include
#include
#defineNULL0
#defineLENsizeof(structstudent)
structstudent
{
intnum;
charname[20];
structstudent*next;
};
intn;
structstudent*creat()
{
structstudent*head;
structstudent*p1,*p2;
n=0;
p1=p2=(structstudent*)malloc(LEN);
scanf("%d%s",&p1->num,p1->name);
head=NULL;
while(p1->num!
=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(structstudent*)malloc(LEN);
scanf("%d%s",&p1->num,p1->name);
}
p2->next=NULL;
returnhead;
}
voidprint(structstudent*head)
{
structstudent*p;
printf("\nNow,These%drecordsare:
\n",n);
p=head;
if(head!
=NULL)
do
{
printf("%d,%s\n",p->num,p->name);
p=p->next;
}
while(p!
=NULL);
}
structstudent*insert(structstudent*head,structstudent*stud)
{
structstudent*p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{
head=p0;
p0->next=NULL;
}
else
{
while((p0->num>p1->num)&&(p1->next!
=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
returnhead;
}
structstudent*del(structstudent*head,intnum)
{
structstudent*p1,*p2;
p1=head;
if(head==NULL)
{
printf("\nlistnull!
\n");
returnhead;
}
while((num!
=p1->num)&&(p1->next!
=NULL))
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
printf("delete:
%d\n",num);
n=n-1;
}
else
printf("%dnotbeenfound!
\n",num);
returnhead;
}
voidmain()
{
structstudent*head,*stu;
intdel_num;
printf("inputrecords:
\n");
head=creat();
print(head);
printf("\ninputtheinsertedrecord:
");
stu=(structstudent*)malloc(LEN);
scanf("%d%s",&stu->num,stu->name);
head=insert(head,stu);
print(head);
printf("\ninputthedeletednumber:
");
scanf("%d",&del_num);
head=del(head,del_num);
print(head);
}
3.栈
#include
#defineMaxSize3
typedefstruct
{
intdata[MaxSize];
inttop;
}Stack;
voidInitStack(Stack&S)//置空栈
{
S.top=-1;
}
intpush(Stack&S,intx)//进栈
{
if(S.top==MaxSize-1)
return0;
else
{
S.top=S.top+1;
S.data[S.top]=x;
return1;
}
}
intgettop(StackS,int&x)//取栈顶元素
{
if(S.top==-1)
return0;
else
{
x=S.data[S.top];
return1;
}
}
intpop(Stack&S,int&x)//出栈
{
if(S.top==-1)
return0;
else
{
x=S.data[S.top];
S.top--;
return1;
}
}
intStackEmpty(StackS)//判断栈空
{
if(S.top==-1)
return1;
else
return0;
}
voidmain()
{
StackS;
intx,i=0;
InitStack(S);
printf("现在开始建栈,请输入3个数据!
\n");
do
{
scanf("%d",&x);
push(S,x);
i++;
}while(i<3);
printf("取栈顶元素是:
\n");
printf("%-10d\n",S.data[S.top]);
printf("元素依次出栈:
\n");
i=0;
do
{
pop(S,x);
printf("%-10d",x);
i++;
}while(i<3);
}
4.链队列
#include
#include
#defineNULL0;
typedefstructQNode
{
intdata;
structQNode*next;
}QNode,*QueuePtr;
typedefstruct
{
QueuePtrfront;
QueuePtrrear;
}LinkQueue;
QueuePtrp;
intInitQueue(LinkQueue&Q)//构造空队列
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!
Q.front)
return0;
Q.front->next=NULL;
return1;
}
intEnQueue(LinkQueue&Q,inte)//入队
{
p=(QueuePtr)malloc(sizeof(QNode));
if(!
p)
return0;
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return1;
}
intDeQueue(LinkQueue&Q,int&e)//出队
{
if(Q.front==Q.rear)
return0;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
return1;
}
voidmain()
{
LinkQueueQ;
inte;
InitQueue(Q);
printf("现在开始入队,请输入3个数据!
输入0结束!
\n");
do
{
scanf("%d",&e);
EnQueue(Q,e);
}while(e!
=0);
printf("删除队头元素是:
\n");
DeQueue(Q,e);
printf("%-10d",e);
printf("\n请输入要入队的元素!
输入0结束!
\n");
do
{
scanf("%d",&e);
EnQueue(Q,e);
}while(e!
=0);
}
5.循环队列
#include
#include
#defineMAXSIZE5
typedefstruct
{
intdata[MAXSIZE];
intfront,rear;
intnum;
}Queue;
Queueq;
intx,i;
voidkongdui()
{
q.front=q.rear=0;
}
voidjindui(Queue&q,intx)//进队算法
{
if(q.num==MAXSIZE)
printf("队满!
\n");
q.rear=(q.rear+1)%MAXSIZE;
q.data[q.rear]=x;
q.num++;
}
intchudui(Queue&q,int&x)//出队算法
{
if(q.num==0)
{
printf("对空\n");
return0;
}
q.front=(q.front+1)%MAXSIZE;
x=q.data[q.front];
q.num--;
return1;
}
voidmain()
{
printf("开始入队,请输入6个数据:
\n");
kongdui();
i=0;
while(i<6)
{
scanf("%d",&x);
jindui(q,x);
i++;
}
printf("现在队列元素是:
\n");
i=0;
while(i<6)
{
chudui(q,x);
printf("%-10d",x);
i++;
}
}
6.先序中序递归算法
#include
#include
#defineNULL0
typedefstructnode
{
intdata;
structnode*lchild;
structnode*rchild;
}BiTNode,*BiTree;
voidcreateBinTree(BiTree&T)//创建二叉树
{
inti;
scanf("%d",&i);
if(i==0)
T=NULL;
else
{
T=(BiTree)malloc(sizeof(BiTree));
T->data=i;
createBinTree(T->lchild);//构造左子树
createBinTree(T->rchild);//构造右子树
}
}
voidpreorder(BiTreeT)//先序遍历二叉树
{
if(T)
{
printf("%-10d",T->data);
preorder(T->lchild);
preorder(T->rchild);
}
}
voidinorder(BiTreeT)//中序遍历二叉树
{
if(T)
{
inorder(T->lchild);
printf("%-10d",T->data);
inorder(T->rchild);
}
}
voidmain()
{
BiTreeT;
charch1,ch2;
printf("\n欢迎进入二叉树基本操作测试程序,请选择:
\n");
ch1='y';
while(ch1=='y')
{
printf("\na----------二叉树建立");
printf("\nb----------先序遍历");
printf("\nc----------中序遍历");
printf("\nq----------退出\n");
scanf("\n%c",&ch2);
switch(ch2)
{
case'a':
printf("请输入按先序建立二叉树的节点序列:
\n");
createBinTree(T);
break;
case'b':
printf("该二叉树的先序遍历序列为:
\n");
preorder(T);
break;
case'c':
printf("该二叉树的中序遍历序列为:
\n");
inorder(T);
break;
case'q':
ch1='n';
break;
default:
printf("输入无效,请重新选择需要的操作!
\n");
}
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构 六个 主要 算法 程序