c - 堆栈和队列程序中的段错误

标签 c segmentation-fault stack queue

这段代码应该允许用户随意在堆栈和队列之间切换。我现在面临的问题是在我启动程序后,它会像它应该的那样要求 q 或 s。但是一旦你真正输入了你的输入,它就会给出一个段错误。有人知道我能做些什么来解决这个问题吗?

#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");
}

最佳答案

scanf("%s", ch); 会导致您的段错误。更改它 scanf("%c", &ch);。在所有方法中更正此问题。在你的 scanf 调用之后,还要放置这样的代码来吃掉缓冲区中的尾随新行:

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

完整代码:

#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");
}

关于c - 堆栈和队列程序中的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28080756/

相关文章:

C++跳转到其他方法执行

c - 为什么 printf ("%.1f", 1) 输出 0.0

c++ - 一台 Linux 机器上的段错误,但另一台使用 C++ 代码的机器上没有

汇编 (68k) 堆栈操作

c - 初始化 int 变量时出现段错误(核心已转储)

c++ - 可变长度数组的 ICC 段错误

c - 为什么Lua C函数中的参数值显示在堆栈底部?

c - 如何解决vfmadd213ps的 "illegal instruction"?

c - 初始化多个结构时出现“预期错误 ";"”

c - 在 C 中释放局部定义的变量