Реализация Deque с использованием массива в C - PullRequest
0 голосов
/ 22 февраля 2019

Итак, я пытался создать программу, управляемую меню, для реализации Dequeue с использованием массивов в 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") ;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...