队列的建立与查询【用C语言编写的完整程序(包括main()函数】
要求:1.定义一个顺序存储结构(整型数组)的存储结构的队列;2.建立队列操作菜单(入队、出队、退出);3.通过键盘输入入队数据;4.每次入队、出队后,显示队内保存的元素注...
要求:1.定义一个顺序存储结构(整型数组)的存储结构的队列;
2.建立队列操作菜单(入队、出队、退出);
3.通过键盘输入入队数据;
4.每次入队、出队后,显示队内保存的元素
注:本人绝不采纳一楼的答案(答非所问)
哪位高手帮忙啊!!!!! 展开
2.建立队列操作菜单(入队、出队、退出);
3.通过键盘输入入队数据;
4.每次入队、出队后,显示队内保存的元素
注:本人绝不采纳一楼的答案(答非所问)
哪位高手帮忙啊!!!!! 展开
3个回答
展开全部
我的理解是 你想用数组模拟 队列?不是的话下面不用看,回复我给我再说一下题意,我重新给你写!
首先输入一个操作,1入队,2出队,3退出
如果是1,再输入一个将要入队列的 数据,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 1000
int queue[LEN], fir, end;
void printQueue()
{
int i = 0;
for(i = fir; i < end; ++ i)
{
printf("%d ", queue[i]);
}
printf("\n");
}
void insertQueue()
{
int value = 0, i = 0;
printf("Enter the data which you want to insert to queue...\n");
scanf("%d", &value);
queue[end ++] = value;
printQueue();
}
void deleteQueue()
{
printf("after delete the top data!\n");
fir ++;
printQueue();
}
void demo()
{
int Number = 0;
while(1)
{
printf("Enter the number:\n");
printf("1.insert...\n");
printf("2.delete...\n");
printf("3.exit!\n");
scanf("%d", &Number);
if(Number < 1 || Number > 4) return;
switch(Number)
{
case 1: insertQueue(); break;
case 2: deleteQueue(); break;
case 3: exit(0);
default: return;
}
}
}
void creatQueue()
{
int i = 0;
fir = 0, end = 0;
for(i = 0; i < LEN; ++ i)
{
queue[i] = 0;
}
}
int main()
{
creatQueue();
demo();
return 0;
}
首先输入一个操作,1入队,2出队,3退出
如果是1,再输入一个将要入队列的 数据,
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN 1000
int queue[LEN], fir, end;
void printQueue()
{
int i = 0;
for(i = fir; i < end; ++ i)
{
printf("%d ", queue[i]);
}
printf("\n");
}
void insertQueue()
{
int value = 0, i = 0;
printf("Enter the data which you want to insert to queue...\n");
scanf("%d", &value);
queue[end ++] = value;
printQueue();
}
void deleteQueue()
{
printf("after delete the top data!\n");
fir ++;
printQueue();
}
void demo()
{
int Number = 0;
while(1)
{
printf("Enter the number:\n");
printf("1.insert...\n");
printf("2.delete...\n");
printf("3.exit!\n");
scanf("%d", &Number);
if(Number < 1 || Number > 4) return;
switch(Number)
{
case 1: insertQueue(); break;
case 2: deleteQueue(); break;
case 3: exit(0);
default: return;
}
}
}
void creatQueue()
{
int i = 0;
fir = 0, end = 0;
for(i = 0; i < LEN; ++ i)
{
queue[i] = 0;
}
}
int main()
{
creatQueue();
demo();
return 0;
}
展开全部
你好,我大学里的作业,共享出来,有问题在找我:
线性表的链式存储结构———单链表
07计科4班 栗华山 0710311206
说明:此程序为动态创建链表,分为5个模块:创建、顺序输出、查询、插入、删除。
编程环境:wintc
#include<stdio.h>
#include<malloc.h>
#define SIZE sizeof(struct node)
/*申明链表*/
struct node
{
int date;
struct node *next;
};
/*创建链表*/
struct node *creat()
{
struct node *head,*p1,*p2;
int many,i,j=1;
char anykey;
head=(struct node *) malloc(SIZE);
head->date=0;
head->next=NULL;
p2=head;
clrscr();
printf("\n\n\n Welcome to creat a linklist!");
printf("\n\n\n The header has created!");
printf("\n\n How many numbers do you want to creat: ");
scanf("%d",&many);
for(i=0;i<many;i++,j++)
{
p1=(struct node *)malloc(SIZE);
scanf("\n\n%d",&p1->date);
p2->next=p1;
p2=p1;
printf("The %d node haved been created\n\n",j);
}
p2->next=NULL;
printf("\n\n\n\n\n Please input anykey to the menu... ");
anykey=getch();
clrscr();
return(head);
}
/*顺序输出链表*/
void print(struct node * head)
{
char anykey;
struct node *p1;
p1=head;
clrscr();
printf("\n\n\n These numbers are :\n ");
if(head==NULL) printf("There is no number!");
else
{
do
{
printf("%d ",p1->date);
p1=p1->next;
}while(p1!=NULL);
}
printf("\n\n\n\n\n Please input anykey to the menu... ");
anykey=getch();
clrscr();
}
/*查看链表*/
void search(struct node *head)
{
int which,i,j,anykey;
struct node *p1;
p1=head;
clrscr();
printf("\n\n Which number do you find: ");
scanf("%d",&which);
for(i=0;i<which;i++)
p1=p1->next;
if(NULL==p1||which>i) printf("i error");
else printf("The %d number is %d",which,p1->date);
printf("\n\n\n\n\n Please input anykey to the menu... ");
anykey=getch();
clrscr();
}
/*向链表插入新元素*/
void insert(struct node *head)
{
struct node *p1,*p2;
int i=0,where,newnumber,anykey;
p1=head;
clrscr();
printf("\n\n\nWhere do you insert: ");
scanf("%d",&where);
while(p1!=NULL&&i<where-1)
{
p1=p1->next;
++i;
}
if(NULL==p1||i>where-1) printf("i error \n");
p2=(struct node *)malloc(SIZE);
printf("\n\nPlease input a number of the new node: ");
scanf("%d",&newnumber);
p2->date=newnumber;
p2->next=p1->next;
p1->next=p2;
printf("\n\nThe new node had beed inserted");
printf("\n\n\n\n\n Please input anykey to the menu... ");
anykey=getch();
clrscr();
}
/*删除链表的元素*/
void deletenode(struct node *head)
{
struct node *p1,*p2;
int i=0,which,anykey;
p1=head;
clrscr();
printf("\n\n\nWhich node do you delete :");
scanf("%d",&which);
while(p1->next&&i<which-1)
{
p1=p1->next;
++i;
}
if(NULL==(p1->next)||i>which-1) printf("i error");
p2=p1->next;
p1->next=p2->next;
free(p2);
printf("\n\nThe %d node had been delete!",which);
printf("\n\n\n\n\n Please input anykey to the menu... ");
anykey=getch();
clrscr();
}
/*主函数*/
void main()
{
int choose;
struct node *head;
for(;;)
{
printf("\n\n\n Welcome to the node world!");
printf("\n\n\n [1]...Creat a linklist");
printf("\n\n [2]...Print the linklist");
printf("\n\n [3]...Search in the linklist");
printf("\n\n [4]...Insert in the linklist");
printf("\n\n [5]...Delete in the linklist");
printf("\n\n\n Where do you want to go <from 1 to 5>... ");
scanf("%d",&choose);
switch(choose)
{
case 1:head=creat();break;
case 2: print(head);break;
case 3:search(head);break;
case 4:insert(head);break;
case 5:deletenode(head);break;
default :return;
}
}
}
2009年3月11日晚
线性表的链式存储结构———单链表
07计科4班 栗华山 0710311206
说明:此程序为动态创建链表,分为5个模块:创建、顺序输出、查询、插入、删除。
编程环境:wintc
#include<stdio.h>
#include<malloc.h>
#define SIZE sizeof(struct node)
/*申明链表*/
struct node
{
int date;
struct node *next;
};
/*创建链表*/
struct node *creat()
{
struct node *head,*p1,*p2;
int many,i,j=1;
char anykey;
head=(struct node *) malloc(SIZE);
head->date=0;
head->next=NULL;
p2=head;
clrscr();
printf("\n\n\n Welcome to creat a linklist!");
printf("\n\n\n The header has created!");
printf("\n\n How many numbers do you want to creat: ");
scanf("%d",&many);
for(i=0;i<many;i++,j++)
{
p1=(struct node *)malloc(SIZE);
scanf("\n\n%d",&p1->date);
p2->next=p1;
p2=p1;
printf("The %d node haved been created\n\n",j);
}
p2->next=NULL;
printf("\n\n\n\n\n Please input anykey to the menu... ");
anykey=getch();
clrscr();
return(head);
}
/*顺序输出链表*/
void print(struct node * head)
{
char anykey;
struct node *p1;
p1=head;
clrscr();
printf("\n\n\n These numbers are :\n ");
if(head==NULL) printf("There is no number!");
else
{
do
{
printf("%d ",p1->date);
p1=p1->next;
}while(p1!=NULL);
}
printf("\n\n\n\n\n Please input anykey to the menu... ");
anykey=getch();
clrscr();
}
/*查看链表*/
void search(struct node *head)
{
int which,i,j,anykey;
struct node *p1;
p1=head;
clrscr();
printf("\n\n Which number do you find: ");
scanf("%d",&which);
for(i=0;i<which;i++)
p1=p1->next;
if(NULL==p1||which>i) printf("i error");
else printf("The %d number is %d",which,p1->date);
printf("\n\n\n\n\n Please input anykey to the menu... ");
anykey=getch();
clrscr();
}
/*向链表插入新元素*/
void insert(struct node *head)
{
struct node *p1,*p2;
int i=0,where,newnumber,anykey;
p1=head;
clrscr();
printf("\n\n\nWhere do you insert: ");
scanf("%d",&where);
while(p1!=NULL&&i<where-1)
{
p1=p1->next;
++i;
}
if(NULL==p1||i>where-1) printf("i error \n");
p2=(struct node *)malloc(SIZE);
printf("\n\nPlease input a number of the new node: ");
scanf("%d",&newnumber);
p2->date=newnumber;
p2->next=p1->next;
p1->next=p2;
printf("\n\nThe new node had beed inserted");
printf("\n\n\n\n\n Please input anykey to the menu... ");
anykey=getch();
clrscr();
}
/*删除链表的元素*/
void deletenode(struct node *head)
{
struct node *p1,*p2;
int i=0,which,anykey;
p1=head;
clrscr();
printf("\n\n\nWhich node do you delete :");
scanf("%d",&which);
while(p1->next&&i<which-1)
{
p1=p1->next;
++i;
}
if(NULL==(p1->next)||i>which-1) printf("i error");
p2=p1->next;
p1->next=p2->next;
free(p2);
printf("\n\nThe %d node had been delete!",which);
printf("\n\n\n\n\n Please input anykey to the menu... ");
anykey=getch();
clrscr();
}
/*主函数*/
void main()
{
int choose;
struct node *head;
for(;;)
{
printf("\n\n\n Welcome to the node world!");
printf("\n\n\n [1]...Creat a linklist");
printf("\n\n [2]...Print the linklist");
printf("\n\n [3]...Search in the linklist");
printf("\n\n [4]...Insert in the linklist");
printf("\n\n [5]...Delete in the linklist");
printf("\n\n\n Where do you want to go <from 1 to 5>... ");
scanf("%d",&choose);
switch(choose)
{
case 1:head=creat();break;
case 2: print(head);break;
case 3:search(head);break;
case 4:insert(head);break;
case 5:deletenode(head);break;
default :return;
}
}
}
2009年3月11日晚
追问
晕,你看题目了没有???
追答
哎,你要用整形数组啊,哥错了,再给你写一个不就得了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
需要这样的信息:
1.用链表行不?
2.直接给你函数,你自己弄菜单?
3.队列数据是什么?
1.用链表行不?
2.直接给你函数,你自己弄菜单?
3.队列数据是什么?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询