Chris3101
Chris3101

Reputation: 55

Why Is this basic C program causing an infinite loop?

my task is to build a basic linked list that "represents a stack". So data can only be accessed according to Last-in-first-out principal. I have to apply certain functions on that "stack". My code compiles fine, but with execution, it just prints 1 infinitely. I don't know how this can be since I am really only using one while-loop. Does anyone know what my problem is? And maybe how I can prevent such mistakes in the future. Thank you for any help. And apologies in Advance, I am a beginner!

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


typedef struct Stack {
    struct Stack *next;
    int data;
} Stack;

Stack* push(Stack*head, int d) {   //add a datapoint to the top of the stack
    Stack *temp;
    temp=malloc(sizeof(Stack));
    temp->data=d;
    temp->next=head;

    return temp;
}


Stack* pop(Stack*head) {      //read the top data point, and delete it
    Stack* newHead=head->next;
    printf("%i", head->data );
    free(head);
    return newHead;
}

int peek(Stack*head) {   // return the top datapoint, without delete
    return head->data;
}

void isempty(Stack*head) {     
    if (head==NULL) {
        printf("Stack empty");
    }

    printf("Stack has data");
}

void print(Stack*head) {    //print complete Stack
    Stack* cursor;
    cursor=head;

    while (cursor!=NULL) {
        printf("%i", cursor->data);
    }
}
int main(int argc, const char * argv[]) {
    Stack* head;
    head=malloc(sizeof(Stack));
    head=NULL;
    head=push(head, 4);
    head=push(head, 2);
    head=push(head, 1);
    printf("%i", peek(head));
    print(head);
    head=pop(head);
    print(head);
    isempty(head);
    head=pop(head);
    head=pop(head);
    isempty(head);


    return 0;
}

Upvotes: 1

Views: 94

Answers (5)

Rabbid76
Rabbid76

Reputation: 210909

You have to step one forward in your loop in function print:

void print( Stack*head )
{
    Stack* cursor = head;
    while (cursor != NULL)   // 
    {
        printf("%i", cursor->data);
        cursor = cursor->next; // step one forward
    }
}

Further there is a memory leak. You allocate memory for variable head, but you set it to NULLright after:

Stack* head;
// head=malloc(sizeof(Stack)); // delete this
head=NULL;

Upvotes: 1

Roland Illig
Roland Illig

Reputation: 41627

The while loop has a problem:

while (cursor!=NULL) {
    printf("%i", cursor->data);
    cursor = cursor->next; // you forgot this line
}

Without this line, the cursor will never change.

Upvotes: 1

Ed Heal
Ed Heal

Reputation: 59997

In this code:

while (cursor!=NULL) {
    printf("%i", cursor->data);
}

You are not changing the cursor. So change it to

for (cursor = head; cursor!=NULL; cursor = cursor->next) {
    printf("%i", cursor->data);
}

Upvotes: 2

Tom Karzes
Tom Karzes

Reputation: 24052

You have an infinite loop in function print. You need to update cursor inside the loop, otherwise it will loop forever. Here's how I would do it:

void print(Stack*head) {    //print complete Stack
    Stack* cursor;

    for (cursor = head; cursor != NULL; cursor = cursor->next) {
        printf("%i", cursor->data);
    }
}

Upvotes: 1

2501
2501

Reputation: 25752

You don't increment cursor in function print:

    while (cursor!=NULL) {
        printf("%i", cursor->data);
   }

The code also leaks memory here:

head=malloc(sizeof(Stack));
head=NULL;

Upvotes: 6

Related Questions