在C中使用数组实现Deque

在C中使用数组实现Deque,c,arrays,data-structures,queue,deque,C,Arrays,Data Structures,Queue,Deque,因此,我一直在尝试制作一个菜单驱动的程序,用于在C中使用数组实现出列。 据我所知,普通线性队列和出列队列的主要区别在于两端添加/删除元素的能力。因此,我认为我可以重新利用我编写的相同队列代码,并进行一些更改,如添加诸如insert_front()和delete_rear()等函数。但显然,我的方法遇到了障碍,因为,我不知道为什么,但我从“队列”代码中提取的代码不仅编辑了,而且工作位也没有工作 我附上我的出列和排队代码,希望有人能指出我代码中的错误 出列代码 #include <stdio.

因此,我一直在尝试制作一个菜单驱动的程序,用于在C中使用数组实现出列。 据我所知,普通线性队列和出列队列的主要区别在于两端添加/删除元素的能力。因此,我认为我可以重新利用我编写的相同队列代码,并进行一些更改,如添加诸如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") ;
    }
#包括
#定义大小1>
#包括
无效ip_res()//输入受限
void op_res()//输出受限
void insert_front(int queue[],int n);
无效插入_后方(int队列[],int n);
void delete_front(int queue[])//接受int n时出错
无效删除后方(int队列[]);
无效显示(整数队列[]);
int front=-1;
int rear=-1;
int队列[SIZE],n;
void main()
{
int-ch;
而(1)
{
系统(“CLS”);
printf(“\n***菜单***\n”);
printf(“\n1.输入限制出列”);
printf(“\n2.输出限制出列”);
printf(“\n3.DISPLAY”);
printf(“\n4.退出”);
printf(“\n\n输入您的选择:”);
scanf(“%d”和“ch”);
开关(ch)
{
案例1:
ip_res(queue,n);//输入受限退出队列
打破
案例2:
op_res(queue,n);//输入限制退出队列
打破
案例3:
显示(队列);
打破
违约:
printf(“无效输入!!!”);
系统(“暂停”);
}
}
}
无效ip_res()
{
int-ch;
而(1)
{
系统(“CLS”);
printf(“\n***菜单***”);
printf(“\n 1.从后面插入”);
printf(“\n 2.从前面删除”);
printf(“\n 3.从后面删除”);
printf(“\n 4.显示”);
printf(“\n 5.退出”);
printf(“\n输入您的选择:”);
scanf(“%d”和“ch”);
开关(ch)
{
案例1:
插入后面(队列,n);
printf(“\n”);
系统(“暂停”);
打破
案例2:
删除前(队列);
系统(“暂停”);
打破
/*案例3:
删除后方(队列);
系统(“暂停”);
打破
*/案例4:
显示(队列);
系统(“暂停”);
打破
案例5:
出口(0);
违约:
printf(“\n无效ch”);
系统(“暂停”);
打破
}
}
}
无效op_res()
{
int-ch;
而(1)
{
系统(“CLS”);
printf(“\n***菜单***”);
printf(“\n 1.从前面插入”);
printf(“\n 2.从后面插入”);
printf(“\n 3.从前面删除”);
printf(“\n 4.显示”);
printf(“\n 5.Exit\n”);
printf(“\n输入您的选择:”);
scanf(“%d”和“ch”);
开关(ch)
{
/*案例1:
插入前面(队列,n);
printf(“\n”);
系统(“暂停”);
打破
*/案例2:
插入后面(队列,n);
系统(“暂停”);
打破
案例3:
删除前(队列);
系统(“暂停”);
打破
案例4:
显示(队列);
系统(“暂停”);
打破
案例5:
出口(0);
违约:
printf(“\n无效选择!!!\n”);
系统(“暂停”);
打破
}
}
}
无效插入_后方(整数队列[],整数n)
{
//用于在队列后端插入元素的案例。
如果(前部==0和后部==1号)
{
printf(“\nOVERFLOW ERROR!!!\n队列已满!\n\n”);
}
其他的
{
如果(前==-1)
正面=0;
后=后+1;
队列[后部]=n;
printf(“插入成功!!\n\n”);
}
系统(“暂停”);
} 
//案例2用于从队列前面删除元素。
void delete_front(整数队列[])
{
int i;
如果(前部==-1和后部==-1)
{
printf(“\nUNDERFLOW ERROR!!!\n队列为空\n\n”);
}
否则如果(前==0和后==-1)
{
正面=-1;
printf(“\nUNDERFLOW ERROR!!!\n队列为空\n\n”);
}
其他的
{
printf(“已删除的元素为:%d\n\n”,队列[front]);
对于(i=前部;iR?
{
printf(“\nUNDERFLOW ERROR!!!\n队列为空\n\n”);
}
否则如果(前==0和后==-1)
{
正面=-1;
printf(“\nUNDERFLOW ERROR!!!\n队列为空\n\n”);
}
其他的
{
printf(“已删除的元素为:%d\n\n”,队列[rear]);

对于(i=front;iOne)来说,奇怪的是循环运行的时间小于t