1

This code is supposed to allow the user to switch between stack and queue at will. The issue I am facing at the moment is after I start the program it will ask for q or s like it is supposed to. But once you actually put in your input it will give a segmentation fault. Anyone know what I can do to solve this?

#include <stdio.h>
#include <stdlib.h>
struct node{
    int info;
    struct node *ptr;
}*front,*rear,*temp,*front1;
struct stack
{
        int stk[10];
        int top;
} s;

void push();
void pop();
void emptys();
void search();
void queue();
void stack();
void insertq(int data);
void removeq();
void emptyq();
void printq();
void create();
void help();
int count = 0;
void main(){
char ch;
        printf("\n s - Switch to stack mode");
        printf("\n q - Switch to queue mode");
    create();
        while(1)
                {
                printf("\nWhat mode would you like to start in?\n");
                scanf("%s", ch);
                switch (ch){
                        case 's':
                                stack();
                                break;
                        case 'q':
                                queue();
                                break;
                        }
                }
return;
}
void create(){/* Create an empty queue */
    front = rear = NULL;
}
void queue(){
int no, e;
char ch;

    printf("\n s - Switch to stack mode");
        printf("\n i - Insert a new number");
        printf("\n p - Remove front of queue or top of stack");
        printf("\n Q - Exit program");
        printf("\n h - Help");
while(1){
        printf("\n What would you like to do?\n");
        scanf("%s", ch);
        switch (ch)
        {
                case 's':
                        stack();
                        break;
                case 'i':
                        printf("Enter data : ");

                         scanf("%d", &no);
                        insertq(no);
                        break;
                case 'p':
                        removeq();
                        break;
                case 'Q':
                        printf("\nGoodbye");
                        exit;
                case 'h':
                        help();
                        break;
}
}
return;
}
void stack(){
int no, e;
char ch;

    printf("\n q - Switch to queue mode");
        printf("\n i - Insert a new number");
        printf("\n p - Remove front of queue or top of stack");
        printf("\n Q - Exit program");
        printf("\n h - Help");
while(1){
        printf("\n What would you like to do?\n");
        scanf("%s", ch);
        switch (ch)
        {
                case 'q':
                        queue();
                        break;
                case 'i':

                        push();
                        break;
                case 'p':
                        pop();
                        break;
                case 'Q':
                        printf("\nGoodbye");
                        exit;
                case 'h':
                        help();
                        break;
}}
return;
}
void push (){
    int num;

    if (s.top == (9))
    {
        printf ("Error: Overflow\n");
    }
    else
    {
        printf ("Enter the element to be pushed\n");
        scanf ("%d", &num);
        s.top = s.top + 1;
        s.stk[s.top] = num;
    }
}
void pop(){
    int num;
    if (s.top == - 1)
    {
        printf ("Error: Stack Empty\n");

    }
    else
    {
        num = s.stk[s.top];
        printf ("popped element is = %d\n", num);
        s.top = s.top - 1;
    }
}
void insertq(int data){
    if (rear == NULL)
    {
        rear = (struct node *)malloc(1*sizeof(struct node));
        rear->ptr = NULL;
        rear->info = data;
        front = rear;
    }
    else
    {
        temp=(struct node *)malloc(1*sizeof(struct node));
        rear->ptr = temp;
        temp->info = data;
        temp->ptr = NULL;

        rear = temp;
    }
    count++;
}
void removeq(){
    front1 = front;

    if (front1 == NULL)
    {
        printf("\n Error: Trying to display elements from empty queue");
        return;

    }
    else
        if (front1->ptr != NULL)
        {
            front1 = front1->ptr;
            printf("\n Dequed value : %d", front->info);
            free(front);
            front = front1;
        }
        else
        {
            printf("\n Dequed value : %d", front->info);
            free(front);
            front = NULL;
            rear = NULL;
        }
        count--;
}

void printq(){
    front1 = front;

    if ((front1 == NULL) && (rear == NULL))
    {
        printf("Queue is empty");
        return;
    }
    while (front1 != rear)
    {
        printf("%d ", front1->info);
        front1 = front1->ptr;
    }
    if (front1 == rear)
        printf("%d", front1->info);

        printf("%d", front1->info);
}
void help(){
        printf("\n q - switch to queue mode");
        printf("\n s - Switch to stack mode");
        printf("\n i - Insert a new number");
        printf("\n p - Remove front of queue or top of stack");
        printf("\n e - Tells if empty or not");
        printf("\n Q - Exit program");
        printf("\n h - Help");
}
1
  • Try replacing all scanf("%s", ch); with scanf(" %c", &ch); (Notice the space and c in the new format) Commented Jan 22, 2015 at 15:30

2 Answers 2

1

This scanf("%s", ch); leads to your seg fault. Change it scanf("%c", &ch);. Correct this in all methods. Also place the code like this to eat up trailing new line in buffer after your scanf calls:

scanf("%c", &ch);
char c;
while((c = getchar()) != '\n' && c != EOF);

Full code:

#include <stdio.h>
#include <stdlib.h>
struct node{
    int info;
    struct node *ptr;
}*front,*rear,*temp,*front1;
struct stack
{
        int stk[10];
        int top;
} s;

void push();
void pop();
void emptys();
void search();
void queue();
void stack();
void insertq(int data);
void removeq();
void emptyq();
void printq();
void create();
void help();
int count = 0;
int main(){
char ch;
        printf("\n s - Switch to stack mode");
        printf("\n q - Switch to queue mode");
    create();
        while(1)
                {

                printf("\nWhat mode would you like to start in?\n");
                scanf("%c", &ch);
                char c;
                while((c = getchar()) != '\n' && c != EOF);
                switch (ch){
                        case 's':
                                stack();
                                break;
                        case 'q':
                                queue();
                                break;
                        }
                }
return;
}
void create(){/* Create an empty queue */
    front = rear = NULL;
}
void queue(){
int no, e;
char ch;

    printf("\n s - Switch to stack mode");
        printf("\n i - Insert a new number");
        printf("\n p - Remove front of queue or top of stack");
        printf("\n Q - Exit program");
        printf("\n h - Help");
while(1){
        printf("\n What would you like to do?\n");
        scanf("%c", &ch);
        char c;
        while((c = getchar()) != '\n' && c != EOF);
        switch (ch)
        {
                case 's':
                        stack();
                        break;
                case 'i':
                        printf("Enter data : ");

                         scanf("%d", &no);
                        insertq(no);
                        break;
                case 'p':
                        removeq();
                        break;
                case 'Q':
                        printf("\nGoodbye");
                        exit;
                case 'h':
                        help();
                        break;
}
}
return;
}
void stack(){
int no, e;
char ch;

    printf("\n q - Switch to queue mode");
        printf("\n i - Insert a new number");
        printf("\n p - Remove front of queue or top of stack");
        printf("\n Q - Exit program");
        printf("\n h - Help");
while(1){
        printf("\n What would you like to do?\n");
        scanf("%c", &ch);
        char c;
        while((c = getchar()) != '\n' && c != EOF);
        switch (ch)
        {
                case 'q':
                        queue();
                        break;
                case 'i':

                        push();
                        break;
                case 'p':
                        pop();
                        break;
                case 'Q':
                        printf("\nGoodbye");
                        exit;
                case 'h':
                        help();
                        break;
}}
return;
}
void push (){
    int num;

    if (s.top == (9))
    {
        printf ("Error: Overflow\n");
    }
    else
    {
        printf ("Enter the element to be pushed\n");
        scanf ("%d", &num);
        s.top = s.top + 1;
        s.stk[s.top] = num;
    }
}
void pop(){
    int num;
    if (s.top == - 1)
    {
        printf ("Error: Stack Empty\n");

    }
    else
    {
        num = s.stk[s.top];
        printf ("popped element is = %d\n", num);
        s.top = s.top - 1;
    }
}
void insertq(int data){
    if (rear == NULL)
    {
        rear = (struct node *)malloc(1*sizeof(struct node));
        rear->ptr = NULL;
        rear->info = data;
        front = rear;
    }
    else
    {
        temp=(struct node *)malloc(1*sizeof(struct node));
        rear->ptr = temp;
        temp->info = data;
        temp->ptr = NULL;

        rear = temp;
    }
    count++;
}
void removeq(){
    front1 = front;

    if (front1 == NULL)
    {
        printf("\n Error: Trying to display elements from empty queue");
        return;

    }
    else
        if (front1->ptr != NULL)
        {
            front1 = front1->ptr;
            printf("\n Dequed value : %d", front->info);
            free(front);
            front = front1;
        }
        else
        {
            printf("\n Dequed value : %d", front->info);
            free(front);
            front = NULL;
            rear = NULL;
        }
        count--;
}

void printq(){
    front1 = front;

    if ((front1 == NULL) && (rear == NULL))
    {
        printf("Queue is empty");
        return;
    }
    while (front1 != rear)
    {
        printf("%d ", front1->info);
        front1 = front1->ptr;
    }
    if (front1 == rear)
        printf("%d", front1->info);

        printf("%d", front1->info);
}
void help(){
        printf("\n q - switch to queue mode");
        printf("\n s - Switch to stack mode");
        printf("\n i - Insert a new number");
        printf("\n p - Remove front of queue or top of stack");
        printf("\n e - Tells if empty or not");
        printf("\n Q - Exit program");
        printf("\n h - Help");
}
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you, now the problem is that when I am in a mode and choose an option it gives the "what would you like to do" prompt multiple times at once
@Gvalder - you did not read my answer correctly - i am putting your corrected code in edit. Give me some time.
There is a time restriction for selecting answers. I have to wait 10 minutes to accept one, thanks for the fast response.
Note: while(getchar()!='\n'); is an infinite loop should EOF occur.
1) EOF can be signaled from the keyboard. 2) A trailing '\n' after the scanf ("%d", &num); may need to have its '\n' consumed also before a following scanf ("%c", &ch);
|
1

Instead of scanf(%d", &ch) you can use ch = getchar() which will read single character and you can use switch statement to process the input.

while(1)
{
    printf("Enter you choice");
    ch = getchar();
    switch(ch)
    {
        case 's':
        stack();
        break;

        default :
        printf("Invalid input \n");
        help();
        break;
    }
}

In above code default is optional but used for error handling.

Thanks,
Sudhir

2 Comments

After all the edits it will go into stack mode and accept an input once but the second time around no matter what is put in it will take it as an invalid input and loop through again.
Hi Gvalder, You may be facing problem with stream buffers to avoid this give a space in scanf function while accepting input ex : scanf(" %c", &ch); If you see in this scanf there is a space between " and % . Hope this will help you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.