| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 589 人关注过本帖
标题:栈与队列代码交流
只看楼主 加入收藏
josean
Rank: 1
等 级:新手上路
帖 子:142
专家分:0
注 册:2007-4-4
收藏
 问题点数:0 回复次数:1 
栈与队列代码交流
内容:假设以带头结点的循环链表表示队列,并且只设一个指针指向队尾元素结点(注意不设头指针),试编写相应的置空队、入队列和出队列的算法 

#include <stdio.h>

#include <malloc.h>

#define N 100; //该循环队列最多可容纳100个元素

#define L sizeof(struct Q)

int m=5,t; //最先在空的队列里压入5个元素

struct Q

{

int num; //指针及变量的定义

struct Q *next;

};

struct Q *create(void)

{struct Q *head,*p,*last;

int i;

last=(struct Q*)malloc(L); //开辟指针域

printf("input %d records\n",m); //打印输入的元素个数

scanf("%d",&last->num);

head=last;

for(i=2;i<=m;i++) //队列的初始话,压入元素

{ p=(struct Q*)malloc(L);

scanf("%d",&p->num);

last->next=p;

last=last->next;

}

last->next=head;

return last;

}

void printlist(struct Q *head) //对队列进行打印操作

{struct Q *p;

printf("The queue list is :\n");

p=head;

printf("%d\n",p->num);

p=p->next;

while(p!=head)

{

printf("%d\n",p->num);

p=p->next;

}

printf("the stack's length is %d\n",m);

}

struct Q *del(struct Q *last) //对队列进行删除操作

{

struct Q *p,*head; //头节点后移

head=last->next;

last->next=head->next;

m--;

free(head);

return last; //返回尾节点

}

struct Q *ins(last,insert) //队列的插入操作

struct Q *last,*insert;

{

struct Q *head=last->next;

last->next=insert;

last=last->next;

insert->next=head;

m++; //队列中元素个数的计数

return last; //返回尾节点

}

main() //主函数

{struct Q //定义各变量,及指针

*last,*newne;

last=create(); //进行相关的操作

printlist(last->next);

newne=(struct Q *)malloc(L);

printf("please input number you want to insert:\n");

scanf("%d",&t);

newne->num=t;

last=ins(last,newne);

printlist(last->next);

last=del(last);

printlist(last->next);

printf("\ngood bye!\n");

}

搜索更多相关主题的帖子: 队列 代码 交流 
2007-04-20 22:23
josean
Rank: 1
等 级:新手上路
帖 子:142
专家分:0
注 册:2007-4-4
收藏
得分:0 

有点粗糙,希望对大家有帮助!


菩提本无树,明镜亦非台,本来无一物,何处惹尘埃!
2007-04-21 12:13
快速回复:栈与队列代码交流
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041752 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved