【问题标题】:Implementation of Stacks using Linked List. Where am I going wrong?使用链表实现堆栈。我哪里错了?
【发布时间】:2014-12-04 19:17:31
【问题描述】:

这是我用 C 编写的代码:

#include<stdio.h>
struct Node
{
    int info;
    struct Node *next;
};
void init_Node(struct Node *n)
{
    n->next = NULL;
}
struct Node *front = NULL;
void display()
{
    struct Node *rear = front;
    if(rear == NULL)
        printf("List is empty!\n");
    else
    {
        printf("[%i]-->",rear->info);
        rear = rear->next;
    }
    printf("NULL");
    printf("\n");
}
void addEnd(int x)
{
    struct Node *n = malloc(sizeof(struct Node));
    struct Node *rear = front;
    n->info = x;
    if(front == NULL)
        front = n;
    else
    {
        while(rear->next != NULL)
            rear = rear->next;
        rear->next = n;
        rear = n;
    }
    display();
}
void deleteEnd()
{
    struct Node *rear = front;
    if(front == NULL)
        printf("Stack is Empty!");
    else
    {
        while(rear->next->next != NULL)
        {
            rear = rear->next;
        }
        printf("Popped : %i\n", rear->next->info);
        rear->next = NULL;
        display();
    }
}
int main()
{
    struct Node *n = malloc(sizeof(struct Node));
    init_Node(n);
    clrscr();
    addEnd(23);
    addEnd(45);
    addEnd(8);
    addEnd(57);
    deleteEnd();
    addEnd(98);
    deleteEnd();
    getch();
    return 0;
}

以下输出是使用类在 C++ 中完成实现时的输出。 程序的输出应该是这样的 -

但我的代码的输出是这样的 -

编辑 添加 while() 循环并添加 n->next = NULL;输出结果是:

我哪里错了?

【问题讨论】:

  • display() 不包含循环,因此不能显示多个节点。
  • 尝试使用单链表结构,该结构根据定义为队列作为堆栈。没有做任何调试。可能是其他东西。
  • 我不明白为什么void addBegin(int); void addEnd(int); 这两个原型在main() 的主体中声明,而addBegin() 甚至没有实现或调用。
  • void addBegin(int)void addEnd(int) 是什么?
  • n-&gt;info = x; --> n-&gt;info = x;n-&gt;next=NULL; 新节点的next必须初始化为NULL。

标签: c linked-list stack singly-linked-list


【解决方案1】:

你需要做两处改变:

void display()
{
    struct Node *rear = front;
    if(rear == NULL)
        printf("List is empty!\n");
    else
    {
        // CHANGE 1: You need a while loop here
        while(rear != NULL) {
            printf("[%i]-->",rear->info);
            rear = rear->next;
        }
    }
    printf("NULL");
    printf("\n");
}

void addEnd(int x)
{
    struct Node *n = malloc(sizeof(struct Node));
    struct Node *rear = front;
    n->info = x;
    // CHANGE 2: You need to clear n->next to NULL.
    n->next = NULL;
    if(front == NULL)
        front = n;
    else
    {
        while(rear->next != NULL)
            rear = rear->next;
        rear->next = n;
        rear = n;
    }
    display();
}

【讨论】:

    【解决方案2】:

    在您的函数中,addEnd() 您需要将下一个指针初始化为零。所以,就在 n->info = x; 行之后添加一行 n->next = 0;

    【讨论】:

    • @BLUEPIXY 就在你前面。
    • 做到了,但前 2 次迭代仍然是正确的,但之后我猜输出是垃圾,不明白为什么!
    • @WeatherVane,是的。刚注意到。我赞成他的评论。
    • @Sidsec9 也在 addEnd() 我认为行后面 = n;在 else 的底部是一个错误。尝试删除该行。
    猜你喜欢
    • 2011-07-29
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 2011-11-09
    • 2013-10-05
    • 2020-09-12
    • 2013-03-17
    相关资源
    最近更新 更多