【问题标题】:pushing data into stack using LinkedList使用链表将数据推入堆栈
【发布时间】:2016-08-17 08:39:16
【问题描述】:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

struct nodeStack{
    char operator;
    struct  nodeStack *next;

};

typedef struct nodeStack node;

node *start=NULL;
node *tail=NULL;
int top=-1;


int isEmpty()
{

    if(top==-1)
        return 1;

}



void push(char c){
    node *tempNode,*tail;

    tempNode=(node*) malloc(sizeof(node));
    if(tempNode==NULL){
        printf("Memory Unvailable\n");
        return;
    }

    tempNode->operator=c;
    if(start==NULL){
        start=tempNode;
        tail=start;
        tempNode->next=NULL;
        top++;
    }
    else{
        tail->next=tempNode;
        tempNode->next=NULL;
        tail=tail->next;
        top++;
    }

}


/*
struct node* pop(){
    if(top==-1){
        printf("stack is empty");
        return;
    }

    else
    {
        node *temp;
        temp=start;
        while(temp->next!=tail){
            temp->next=NULL;
            free(tail);
            tail=temp;
        }
    }

}*/


void displayStack(){
    node *i;
    for(i=start;i!=tail;i=i->next){
        printf("%c -> ",i->operator);
    }
}


int main(){
    int i;
    int flag=1;
    char choice='y';
    printf("pushing data into the stack......");

   while(flag==1){
        char ch;
        printf("enter a character\n");
        scanf(" %c",&ch);
        push(ch);
        printf("want to push more operator (y\n)");
        scanf(" %c",choice);
        if(choice=='y')
            flag=1;
        else
            flag=0;

   }

    displayStack();

   return 0; 
}

当我尝试运行它时,它给了我分段错误。 它只接受一个输入而不接受进一步的输入,同时它给出了分段错误

当我尝试运行它时,它给了我分段错误。 它只接受一个输入而不接受进一步的输入,同时它给出了分段错误

【问题讨论】:

  • 注意:你不需要尾指针。无论如何,不​​是为了堆栈。
  • 您没有使用任何内存分配“开始”,这就是它给出“分段错误”的原因。另外,您已经在 push 方法中声明了“tail”;不需要!
  • @kiner_shah startstart==NULL 时由tempNode 初始化。所以这不是问题的原因
  • 请将else 语句添加到isEmpty 函数中。
  • @FredrickGauss 不错。照原样,如果top != 1,该函数调用UB。那么,OP,您是否希望编译器猜测您要返回的值是什么?您可能想要的只是 return top == -1 将条件转换为布尔返回值。我的意思是,你甚至不在这里使用那个功能,但如果你使用了,它很快就会引起麻烦。

标签: c linked-list stack


【解决方案1】:

看看:

scanf(" %c",choice);

第二个参数应该作为地址传递,因为scanf修改了它。

尝试修改:

scanf("%c", &choice);

我不打算详细解释原因,因为我认为你只是犯了一个错误,因为在前面的一些行中 scanf(" %c",&amp;ch); 你调用了正确的。

注意:我会避免scanf 字符串格式中的空格。

【讨论】:

  • 代码中的错误不止这个!
  • @kiner_shah 当然!即使是格式化的字符串printf("y\n") 也会打印一个新行,我怀疑这不是他想要的确切行为。无论如何,我回答了分段错误的问题,即问题中的重点问题。我想,更多的 cmets 和答案将不胜感激:)
  • 我认为这不是分段错误的原因!主要原因是我猜原始代码中“tail”指针的双重声明! @Biagio 节日。一个在推送功能中,另一个在全球范围内!
  • @kiner_shah 我认为细节很明显,但事实并非如此。我使用 gcc 编译器进行了测试,我可以确保您给出的问题描述的分段错误是:“它只接受一个输入,而不是进一步,同时它给出分段错误”。 事实上,scanf 需要一个指针,传递一个 char 会产生一个 seg。错误。尝试自己并相信
  • 我认为你是对的,根据他提到的问题。但是,在我们修复它之后,我们仍然会因为两个“尾”指针而出现分段错误!试试看,因为我试过了,它确实导致了错误!
【解决方案2】:

这是一个有点修改的代码!工作代码!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>

typedef struct nodeStack{
    char op;
    struct  nodeStack *next;
}node;


node *start = NULL;
node *tail = NULL;
int top=-1;


int isEmpty()
{

    if(top==-1)
       return 1;
    else return 0;

}



void push(char c){
    node *tempNode;

    tempNode=(node*) malloc(sizeof(node));

    tempNode->op=c;
    if(start==NULL){
        //start = (node*) malloc(sizeof(node));
        start=tempNode;
        tail=start;
        tempNode->next=NULL;
        top++;
    }
   else{
        tail->next=tempNode;
        tempNode->next=NULL;
        tail=tail->next;
        top++;
    }

}


/*
 struct node* pop(){
   if(top==-1){
       printf("stack is empty");
        return;
   }

  else
   {
        node *temp;
       temp=start;
       while(temp->next!=tail){
          temp->next=NULL;
          free(tail);
          tail=temp;
      }
  }

 }*/


void displayStack(){
    node *i;
    for(i=start;i!=NULL;i=i->next){
        printf("%c -> ",i->op);
    }
}


int main(){
    int i;
    int flag=1;
    char choice='y', ch;
    printf("pushing data into the stack......");
    start=NULL; tail=NULL;
    while(flag){
        printf("enter a character\n");
        scanf(" %c",&ch);
        push(ch);
        printf("want to push more operator (y/n)");
        scanf(" %c",&choice);
        if(choice=='y')
            flag=1;
        else
            flag=0;

       }

    displayStack();

  return 0; 
}

【讨论】:

  • 我还没有检查它的其余部分,但这仍然在isEmpty() 中缺少返回值,这会导致 UB ......或者如果 OP 曾经使用过它!
  • 非常感谢@kiner_shah 指出小错误。由于我在很长一段时间后编写了 C 代码,所以我错过了这些小东西。谢谢....
  • 不是问题@Nitin! ;-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 2017-06-16
  • 2021-11-02
  • 1970-01-01
相关资源
最近更新 更多