因此,我一直在尝试制作一个菜单驱动程序,以使用C中的数组来实现出队。 据我所知,普通线性队列和Dequeue之间的主要区别是从两端添加/删除元素的能力。因此,我认为我可以重新利用我编写的Queue的相同代码,并进行一些更改,例如添加函数类似于 insert_front()和 delete_rear()。但是显然我在方法上遇到了麻烦,因为我不为什么,但不仅如此,而且从“队列”代码中获取的代码的编辑部分以及工作部分都无法正常工作。
我附上我的出队和队列代码,希望有人指出我的代码中的错误。
出队代码
#include <stdio.h>
#define SIZE 1>
#include <stdlib.h>
void ip_res() ;//Input Restricted
void op_res(); //Output Restricted
void insert_front(int queue[], int n) ;
void insert_rear(int queue[], int n) ;
void delete_front(int queue[]) ; //Error was accepting int n
void delete_rear(int queue[]) ;
void display(int queue[]) ;
int front = -1;
int rear = -1 ;
int queue[SIZE], n ;
void main()
{
int ch;
while(1)
{
system("CLS") ;
printf("\n***MENU***\n") ;
printf("\n1. INPUT RESTRICTED DEQUEUE") ;
printf("\n2. OUTPUT RESTRICTED DEQUEUE") ;
printf("\n3. DISPLAY");
printf("\n4. EXIT") ;
printf("\n\nEnter your choice: ") ;
scanf("%d", &ch) ;
switch(ch)
{
case 1 :
ip_res(queue,n); //Input-Restricted Dequeue
break ;
case 2 :
op_res(queue,n); //Input-Restricted Dequeue
break ;
case 3 :
display(queue) ;
break ;
default :
printf("INVALID INPUT!!!") ;
system("pause") ;
}
}
}
void ip_res()
{
int ch;
while(1)
{
system("CLS") ;
printf("\n***Menu***") ;
printf("\n 1. Insert from Rear") ;
printf("\n 2. Delete from Front") ;
printf("\n 3. Delete from Rear") ;
printf("\n 4. Display") ;
printf("\n 5. Exit") ;
printf("\nEnter your choice: ") ;
scanf("%d", &ch) ;
switch(ch)
{
case 1:
insert_rear(queue,n) ;
printf("\n");
system("pause");
break ;
case 2:
delete_front(queue) ;
system("pause") ;
break ;
/* case 3:
delete_rear(queue) ;
system("pause") ;
break ;
*/ case 4:
display(queue) ;
system("pause") ;
break ;
case 5:
exit(0) ;
default:
printf("\n Invalid ch") ;
system("pause") ;
break ;
}
}
}
void op_res()
{
int ch;
while(1)
{
system("CLS") ;
printf("\n***Menu***") ;
printf("\n 1. Insert from Front") ;
printf("\n 2. Insert from Rear") ;
printf("\n 3. Delete from Front") ;
printf("\n 4. Display") ;
printf("\n 5. Exit\n") ;
printf("\nEnter your choice: ") ;
scanf("%d", &ch) ;
switch(ch)
{
/* case 1:
insert_front(queue,n) ;
printf("\n");
system("pause");
break ;
*/ case 2:
insert_rear(queue,n);
system("pause") ;
break ;
case 3:
delete_front(queue);
system("pause") ;
break ;
case 4:
display(queue) ;
system("pause") ;
break ;
case 5:
exit(0) ;
default:
printf("\nINVALID CHOICE!!!\n") ;
system("pause") ;
break ;
}
}
}
void insert_rear(int queue[], int n)
{
//Case for inserting an element at Rear End of the queue.
if(front == 0 && rear == SIZE-1)
{
printf("\nOVERFLOW ERROR!!!\nQUEUE IS FULL! \n\n") ;
}
else
{
if (front==-1)
front=0;
rear = rear + 1 ;
queue[rear] = n ;
printf("Insertion Successful!! \n\n") ;
}
system("pause") ;
}
//Case 2 is for deleting an element from front of the queue.
void delete_front(int queue[])
{
int i ;
if(front == -1 && rear == -1)
{
printf("\nUNDERFLOW ERROR!!! \nQUEUE IS EMPTY \n\n") ;
}
else if (front==0 && rear==-1)
{
front=-1;
printf("\nUNDERFLOW ERROR!!! \nQUEUE IS EMPTY \n\n") ;
}
else
{
printf("Deleted element is:%d\n\n", queue[front]) ;
for(i = front ; i<rear ; i++)
{
queue[i] = queue[i+1] ;
}
rear -- ;
}
system("pause");
}
/*/Delete Rear
void delete_rear(int queue[])
{
int i;
if(front == -1 && rear == -1) //WHY NOT || F>R?
{
printf("\nUNDERFLOW ERROR!!! \nQUEUE IS EMPTY \n\n") ;
}
else if (front==0 && rear==-1)
{
front=-1;
printf("\nUNDERFLOW ERROR!!! \nQUEUE IS EMPTY \n\n") ;
}
else
{
printf("Deleted element is:%d\n\n", queue[rear]) ;
for(i = front ; i<rear ; i++)
{
queue[i] = queue[i+1] ;
}
rear ++ ;
}
}
*/
//For Display
void display(int queue[])
{
int i;
if (front == - 1)
printf("Queue is Empty \n");
else
{
for (i = front; i <= rear; i++)
printf("%d\t", queue[i]);
}
printf("\n\n");
system("pause") ;
}
队列代码
#include<stdio.h>
#include <stdlib.h>
#define SIZE 10
void insert(int queue[], int n) ;
void delete(int queue[]) ;
void display(int queue[]) ;
int front = -1;
int rear = -1 ;
void main()
{
int queue[SIZE], n, ch ;
while(1)
{
system("CLS") ;
printf("\n***MENU***\n") ;
printf("\n1. INSERT IN QUEUE") ;
printf("\n2. DELETE FROM QUEUE") ;
printf("\n3. DISPLAY") ;
printf("\n4. EXIT") ;
printf("\n\nEnter your choice: ") ;
scanf("%d", &ch) ;
switch(ch)
{
case 1 :
printf("Enter element to insert: ") ;
scanf("%d", &n) ;
insert(queue, n) ;
break ;
case 2 :
delete(queue) ;
break ;
case 3 :
display(queue) ;
break ;
case 4 :
printf("!!Exit!!") ;
exit(0) ;
default :
printf("INVALID INPUT!!!") ;
system("pause") ;
}
}
}
void insert(int queue[], int n)
{
//Case 1 is for inserting an element in the queue.
if(front == 0 && rear == SIZE-1)
{
printf("\nOVERFLOW ERROR!!!\nQUEUE IS FULL! \n\n") ;
}
else
{
if (front==-1)
front=0;
rear = rear + 1 ;
queue[rear] = n ;
printf("Insertion Successful!! \n\n") ;
// printf("\n\n");
}
system("pause") ;
}
//Case 2 is for deleting an element from the queue.
void delete(int queue[])
{
int i ;
if(front == -1 && rear == -1)
{
printf("\nUNDERFLOW ERROR!!! \nQUEUE IS EMPTY \n\n") ;
}
else if (front==0 && rear==-1)
{
front=-1;
printf("\nUNDERFLOW ERROR!!! \nQUEUE IS EMPTY \n\n") ;
}
else
{
printf("Deleted element is:%d\n\n", queue[front]) ;
for(i = front ; i<rear ; i++)
{
queue[i] = queue[i+1] ;
}
rear -- ;
}
system("pause");
}
//Case 3 is for displaying the elements of the queue.
void display(int queue[])
{
int i;
if (front == - 1)
printf("Queue is Empty \n");
else
{
for (i = front; i <= rear; i++)
printf("%d\t", queue[i]);
}
printf("\n\n");
system("pause") ;
}