王朝网络
分享
 
 
 

偶写的链表、堆栈、队列的集合操作

王朝other·作者佚名  2008-06-01
宽屏版  字体: |||超大  

偶写了一个程序,它的功能是将链表、堆栈、和队列进行集合操作,可以处理一般的插入N个元素,删除N个元素,以及入栈出栈的问题。

--------本程序的最大的特点是能够准确的显示当前的集合操作的各个元素的状态,并且能够将队列的数据以堆栈的格式输出,同样也支持将堆栈的数据以队列的格式显示出来,报错功能也不错,程序的最后将我们开辟的所有的结点空间全部释放掉。

偶觉得,数据结构,说白了就是个哪个先进哪个先出的问题,大家觉得呢??

-----------------偶的QQ:37170732,欢迎喜欢编程的同学加我,非凡是爱好Win32和MFC的同学,大家一起来讨论哦!

-----------------------------下面是程序的代码------------------------------------------

#include <stdio.h>

#include <conio.h>

#define NULL 0

typedef int DataType;

typedef strUCt Node *PNode;

struct Node

{

DataType info;

PNode link;

};

struct LinkType

{

PNode base;

PNode top;

};

typedef struct LinkType *PLinkType;

PLinkType CreatePointer(void)

{ PLinkType pltype;

pltype=(PLinkType)malloc(sizeof(struct LinkType));

if(pltype== NULL) { printf("\nOut of space\n");

pltype=(PLinkType)realloc(sizeof(struct LinkType));}

pltype->base=pltype->top=NULL;

return(pltype);

}

PLinkType CreateHeadNode(PLinkType pltype)

{ PNode paque;

paque=(PNode)malloc(sizeof(struct Node));

if(paque==NULL){

printf("Out of space\n");

paque=(PNode)realloc(sizeof(struct Node));}

else if(paque!= NULL) pltype->base=pltype->top=paque;

pltype->top->link->link=NULL;

return(pltype);

}

PLinkType push_Type(PLinkType pltype,DataType n)

{ PNode p;

int j;

j=0;

printf("Input %d integer:\n",n);

while(j<n) {

pltype->top->link=(PNode)malloc(sizeof(struct Node));

if(pltype->top->link==NULL) {

printf("Out of space");

pltype->top->link=(PNode)realloc(sizeof(struct Node));}

else { while(pltype->top->link!=NULL){

pltype->top->link->link=NULL;

pltype->top=pltype->top->link;

scanf("%d",&pltype->top->info);

j++;}}}

return(pltype);

}

PLinkType print_Type(PLinkType pltype)

{ PNode temp;

temp=pltype->base;

if(temp!=pltype->top){

printf("\n");

while(temp!=pltype->top) {

printf("%d\t",temp->link->info);

temp=temp->link;}}

else printf("empty");

return(pltype);

}

PLinkType pop_Type(PLinkType pltype)

{

while(pltype->base!=pltype->top) {

printf("%d\t",pltype->base->info);

pltype->base=pltype->base->link;}

return(pltype);

}

PLinkType de_Type(PLinkType pltype, DataType j)

{int i;

i=0;

if(pltype->base!=pltype->top){

printf("The pop type list is:\n");

while(pltype->base!=pltype->top &&i<j){

printf("%d\t",pltype->base->link->info);

pltype->base=pltype->base->link;

i++;}

printf("\n%d number(s) has been detyped",i);}

if(pltype->base==pltype->top){

printf("\nAll the type have been detyped");}

return(pltype);

}

PLinkType pop_Stack(PLinkType pltype,DataType j)

{PNode temp;

int i;

i=0;

if(pltype->top!=pltype->base){

printf("The pop stack is:\n");

while(pltype->top!=pltype->base &&i<j){

temp=pltype->base;

if(temp->link!=pltype->top){

while(temp->link != pltype->top) temp=temp->link;

pltype->top->link=pltype->top;

pltype->top=temp;

printf("%d\t",pltype->top->link->info);

i++;}

else{pltype->top->link=pltype->top;

pltype->top=temp;

printf("%d\t",pltype->top->link->info);

i++;}}

printf("\n%d number(s) have been poped\n",i);

return(pltype);}

return(pltype);

}

PLinkType free_all(PLinkType pltype)

{PNode temp;

while(pltype->base!=pltype->top){

temp=pltype->top;

pltype->base=pltype->base->link;

free(temp);}

free(pltype->base);

free(pltype);

printf("All the Nodes and pointer have been freed\n");

}

void main()

{ PLinkType pltype;

PNode pastack;

int j1,j2,j3,j4,j5,k;

int m1,m2,m3,m4,m5,n1,n2,n3,n4,n5;

pltype=CreatePointer();

CreateHeadNode(pltype);

printf("please choose the type of data struct:\n");

printf("1:linklist, 2:linkstack,3:linkqueue,0:to exit\n");

scanf("%d",&k);

while(k!=0){

switch(k){

case 1:{printf("Input the length of linklist:\n");

scanf("%d",&m1);

while(m1<1 ){

printf("The length is illegal,please input again\n");

scanf("%d",&m1);}

push_Type(pltype,m1);

printf("The link list is");

print_Type(pltype);

printf("\nIf you want to enlist or delist,please choose\n");

printf("1: to enlist, 2: to delist, 0:to struct choose\n");

scanf("%d",&m2);

while(m2!=0){

switch(m2){

case 1:{printf("Input the length of the list\n");

scanf("%d",&m3);

while(m3<1 ){

printf("The length is illegal,please input again\n");

scanf("%d",&m3);}

push_Type(pltype,m3);

printf("The link list is:");

print_Type(pltype);} break;

case 2:{if(pltype->base==pltype->top){

printf("The link list is empty\n");}

else{

printf("please input number(s) that you want to delist:\n");

scanf("%d",&m4);

de_Type(pltype,m4);

printf("\nThe&n

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有