【问题标题】:How can I create this linked list with stack in C?如何在 C 中使用堆栈创建此链表?
【发布时间】:2015-05-24 16:36:36
【问题描述】:

我可以创建链表。但我无法用它创建堆栈。 (堆栈不能超过5个,可以为空,如链接所示)。我该怎么做? (C 语言,但允许使用 new int 等 C++ 函数)

【问题讨论】:

  • 什么语言?请向我们展示您目前拥有的代码
  • C 语言,但允许使用像 new int 这样的 C++ 函数 @RobMullins
  • 您是否试图将您的链表复制到堆栈中?您能否发布用于创建堆栈的代码,以及您遇到了什么错误?
  • 链接列表和堆栈将在一起,但如果您的意思是不一样的话。喜欢:Rob 1 2 5 -> HakunaMatata 6 7 9 我无法用堆栈编写代码。 @RobMullins
  • 我不明白,如果您在任何地方使用“新”,它是 c++ ,而不是 c。请了解您实际使用的语言。为了更好地说明,你是如何调用你的编译器的?

标签: c list stack


【解决方案1】:

结构可能是这样的:

struct linkedStack {
  int elements[5];
  int top;
  struct linkedStack *next;
};

然后用top管理栈(开头等于0)...

【讨论】:

    【解决方案2】:

    请发布您的代码,以便我们了解您要做什么。

    这个例子可以帮助你

    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    #include<stdlib.h>
    #include<alloc.h>
    
    void Push(int, node **);
    void Display(node **);
    int Pop(node **);
    int Sempty(node *);
    
    typedef struct stack {
       int data;
       struct stack *next;
    } node;
    
    
    void main() {
       node *top;
       int data, item, choice;
       char ans, ch;
    
       clrscr();
    
       top = NULL;
    
       printf("\nStack Using Linked List : nn");
       do {
          printf("\n\n The main menu");
          printf("\n1.Push \n2.Pop \n3.Display \n4.Exit");
          printf("\n Enter Your Choice");
          scanf("%d", &choice);
    
          switch (choice) {
          case 1:
             printf("\nEnter the data");
             scanf("%d", &data);
             Push(data, &top);
             break;
          case 2:
             if (Sempty(top))
                printf("\nStack underflow!");
             else {
                item = Pop(&top);
                printf("\nThe popped node is%d", item);
             }
             break;
          case 3:
             Display(&top);
             break;
          case 4:
             printf("\nDo You want To Quit?(y/n)");
             ch = getche();
             if (ch == 'y')
                exit(0);
             else
                break;
          }
    
          printf("\nDo you want to continue?");
          ans = getche();
          getch();
          clrscr();
       } while (ans == 'Y' || ans == 'y');
       getch();
    }
    
    void Push(int Item, node **top) {
       node *New;
       node * get_node(int);
       New = get_node(Item);
       New->next = *top;
       *top = New;
    }
    
    node * get_node(int item) {
       node * temp;
       temp = (node *) malloc(sizeof(node));
       if (temp == NULL)
          printf("\nMemory Cannot be allocated");
       temp->data = item;
       temp->next = NULL;
       return (temp);
    }
    
    int Sempty(node *temp) {
       if (temp == NULL)
          return 1;
       else
          return 0;
    }
    
    int Pop(node **top) {
       int item;
       node *temp;
       item = (*top)->data;
       temp = *top;
       *top = (*top)->next;
       free(temp);
       return (item);
    }
    
    void Display(node **head) {
       node *temp;
       temp = *head;
       if (Sempty(temp))
          printf("\nThe stack is empty!");
       else {
          while (temp != NULL) {
             printf("%d\n", temp->data);
             temp = temp->next;
          }
       }
       getch();
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 2012-09-30
      • 2012-02-18
      • 2010-12-06
      • 2012-05-24
      • 2019-03-04
      • 2020-01-16
      相关资源
      最近更新 更多