c - 在 C 中使用数组实现 Deque
问题描述
所以我一直在尝试使用 C 中的数组来制作一个菜单驱动的程序来实现 Dequeue。据我所知,普通线性队列和 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") ;
}
解决方案
推荐阅读
- vector - 如何将元素添加到将字符串与字符串向量配对的哈希映射的值中?
- fortran - 在 Fortran FMZM 多精度库中使用 IM_FORM 显示任意长度的格式化字符串
- css - 如何将标签作为 scss mixin 参数传递?
- entity-framework - 实体类型“程序”需要定义主键
- unit-testing - Testing vuex mocked mutation with jest
- linux - 我可以从另一个完全独立的 Perl 脚本中运行吗?
- c# - Receiving un expected data over serial io c#
- grafana - Success failed rate calculation based on true false values
- javascript - title attribute for element with pointer-events: none
- typo3 - TYPO3 v9.5 在页面编辑中没有可见的 URL 段 [slug] 字段