2017-02-12 2 views
2

Ich bekomme eine falsche Ausgabe, während ich versuche, Daten im Stack mit diesem Programm zu übertragen. Obwohl die Stapelgröße 5 ist, wird beim Drucken der Stapelelemente eine Endlosschleife ausgeführt, während falsche Werte angegeben werden. Was ist der Fehler?Fehler beim Implementieren des Stacks mit der Linkedlist in C

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

struct node { 
    int data; 
    struct node *next; 
}; 

struct node *top = NULL; 

int count = 0; 

void push(int num) { 
    struct node *newNode = (struct node*)malloc(sizeof(int)); 
    newNode->data = num; 
    newNode->next = NULL; 

    if (top == NULL) { 
     top = newNode; 
    } else { 
     struct node *temp = (struct node*)malloc(sizeof(int)); 
     temp = top; 
     top = newNode; 
     top->next = temp; 
     free(temp); 
    } 
    count++; 
} 

int pop() { 
    if (top == NULL) { 
     printf("\nUnderflow- Stack is empty!"); 
     return -1; 
    } 
    struct node *temp = (struct node*)malloc(sizeof(int)); 
    temp = top; 
    top = top->next; 
    return temp->data; 
} 

int stackTop() { 
    if (top == NULL) { 
     printf("\nStack is empty"); 
     return -1; 
    } 
    return top->data; 
} 

void printStack() { 
    if (top == NULL) { 
     printf("\nStack is empty. Nothing to print"); 
    } 
    printf("\n"); 
    while (top != NULL) { 
     printf("%d ", top->data); 
     top = top->next; 
    } 
} 

/* Count stack elements */ 
void stack_count() { 
    printf("\n No. of elements in stack : %d", count); 
} 

int main(void) { 
    int poppedValue, topValue; 
    push(1); 
    push(2); 
    push(3); 
    push(4); 
    push(5); 

    stack_count(); 

    printStack(); 

    poppedValue = pop(); 
    topValue = stackTop(); 

    printf("\nPop item : %d", poppedValue); 
    printf("\nTop Value: %d", topValue); 

    return 0; 
} 

Ausgang:

No. of elements in stack : 5 
5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 0 5 .... 
+0

Sie haben einen Fehler in 'push'. Lesen Sie weiter, wie [kleine Programme zu debuggen] (https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) finden Sie es. – StoryTeller

+0

'struct Knoten * newNode = (struct Knoten *) malloc (sizeof (int));' -> 'struct Knoten * newNode = malloc (sizeof (struct Knoten));' – BLUEPIXY

+0

'struct Knoten * temp = (struct Knoten *) malloc (Größe von (int)); Temp = oben; top = neuer Knoten; top-> nächste = temp; frei (temp); '->' newNode-> next = top; top = newNode; ' – BLUEPIXY

Antwort

0

Sie brauchen nicht zu verschwenden, indem sie es mit Speicher nur einen temp Knoten in Ihrem push() function.You zu verwenden.

Folgendes ist der verbesserte Code und es wird erfolgreich ausgeführt.

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

struct node{ 
    int data; 
    struct node* next; 
}; 
struct node* top = NULL; 

int count = 0; 

void push(int num){ 
    struct node* newNode = (struct node*)malloc(sizeof(struct node)); 
    newNode->data = num; 
    newNode->next =top; 
    top=newNode; 
    count++; 
} 

int pop(){ 
    if(top == NULL){ 
     printf("\nUnderflow- Stack is empty!"); 
     return -1; 
    } 

    int ans=top->data; 
    struct node *temp = top; 
    top = top->next; 
    free(temp); 
    count--; 
    return ans; 
} 

int stackTop(){ 
    if(top == NULL){ 
     printf("\nStack is empty"); 
     return -1; 
    } 
    return top->data; 
} 


void printStack(){ 
    struct node *t=top; 
    if(t == NULL){ 
     printf("\nStack is empty. Nothing to print"); 
    } 
    printf("\n"); 
    while(t != NULL){ 
     printf("%d ",t->data); 
     t=t->next; 
    } 
} 

/* Count stack elements */ 
    void stack_count() 
    { 
     printf("\n No. of elements in stack : %d", count); 
    } 

int main(void) { 

    int poppedValue, topValue; 
    push(1); 
    push(2); 
    push(3); 
    push(4); 
    push(5); 

    stack_count(); 


    printStack(); 

    poppedValue = pop(); 
    topValue = stackTop(); 

    printf("\nPop item : %d", poppedValue); 
    printf("\nTop Value: %d", topValue); 

    return 0; 
} 

OUTPUT

No. of elements in stack : 5 
5 4 3 2 1 
Pop item : 5 
Top Value: 4 
+0

Sie haben Recht, also werde ich meine Antwort aktualisieren. – a874

+0

'push()' kann vereinfacht werden und 'pop()' sollte 'count' dekrementieren. – chqrlie

1

Ihr Programm hat mehrere Probleme:

  • Sie die node Strukturen mit der falschen Größe zuzuordnen: sizeof(int). Der Rückgabewert von malloc() wird in C nicht benötigt und als schlechter Stil betrachtet. Sie sollten diese Methode verwenden, die die richtige Größe garantiert:

    struct node *newNode = malloc(sizeof(*newNode)); 
    

    Dieses Problem allein verursacht nicht definiertes Verhalten, die das beobachtete Problem erklären könnte.

  • Sie drucken Zeilenumbrüche vor Ihren Nachrichten, dies ist kein Fehler, führt aber zu einer verwirrenden Ausgabe. Sie sollten den Zeilenumbruch am Ende Ihrer Nachrichten platzieren.

  • Die push-Funktion weist Speicher für temp zu, verwendet sie aber nicht. temp wird sofort mit einem anderen Wert überschrieben, den Sie free vorzeitig, was zu mehreren möglichen Aufrufe an free für das gleiche Objekt führt. Dies ist definitiv ein Fehler, der zu undefiniertem Verhalten führt.

  • Die pop Funktion vermasselt auch den Stapel und befreit den obersten Knoten nicht.

  • Sie vergessen, count zu dekrementieren, wenn Sie Elemente aus dem Stapel knallen.

  • Funktion printStack() ändert den Stapel top, Speicher ist nicht mehr zugänglich. Sie sollten eine temporäre Variable verwenden. Hier

ist eine modifizierte Version:

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

struct node { 
    int data; 
    struct node *next; 
}; 

struct node *top = NULL; 
int count = 0; 

void push(int num) { 
    struct node *newNode = malloc(sizeof(*newNode)); 
    if (newNode == NULL) { 
     printf("Memory allocation failed\n"); 
     return; 
    } 
    newNode->data = num; 
    newNode->next = top; 
    top = newNode; 
    count++; 
} 

int pop(void) { 
    if (top == NULL) { 
     printf("Underflow. Stack is empty!\n"); 
     return -1; 
    } else { 
     int data = top->data; 
     struct node *temp = top; 
     top = top->next; 
     free(temp); 
     count--; 
     return data; 
    } 
} 

int stackTop(void) { 
    if (top == NULL) { 
     printf("Stack is empty\n"); 
     return -1; 
    } 
    return top->data; 
} 

void printStack(void) { 
    if (top == NULL) { 
     printf("Stack is empty. Nothing to print\n"); 
     return; 
    } 
    struct node *temp = top; 
    while (temp != NULL) { 
     printf("%d ", temp->data); 
     temp = temp->next; 
    } 
    printf("\n"); 
} 

/* Count stack elements */ 
void stack_count(void) { 
    printf("No. of elements in stack: %d\n", count); 
} 

int main(void) { 
    int poppedValue, topValue; 

    push(1); 
    push(2); 
    push(3); 
    push(4); 
    push(5); 
    stack_count(); 
    printStack(); 
    poppedValue = pop(); 
    topValue = stackTop(); 
    printf("Popped item: %d\n", poppedValue); 
    printf("Top Value: %d\n", topValue); 

    return 0; 
} 

Ausgang:

No. of elements in stack: 5 
5 4 3 2 1 
Popped item: 5 
Top Value: 4 
Verwandte Themen