0

I am implementing a stack using Linked_list in C lang. I have successfully created first Node. And when I am trying to add further nodes dynamically in the first position as stack follows LIFO principle, I believe I am not correctly linking pointers so that the order of Linked list remains intact. The code is pretty simple as shown below, I just need to understand, when I am trying to print the entire linkedlist, it is only printing the data of very first node. Looks like the entire chain is breaking after the first node. Where am I going wrong ?

// Implementation of stack using a linked-list
#include<stdio.h>
#include<stdlib.h>

typedef struct sllnode
{
int data;
struct sllnode *next;
}
stack;

void create(stack *head, int elem)
{
head -> data = elem;
head -> next = NULL;
}

void insert(stack *head, int elem)
{
stack *newNode = malloc( sizeof(stack) );
newNode -> data = elem;
newNode -> next = head;
head = newNode;
}

void display(stack *head)
{
if( head == NULL )
{
    printf(" NULL value returned \n");
}
stack *ptr = head;
while(ptr != NULL)
{
    printf("%d \n", ptr -> data);
    ptr = ptr -> next;
}
}

int main(void)
{
stack *head = malloc( sizeof(stack) );
create(head, 10);
insert(head, 15);
insert(head, 25);
insert(head, 26);
insert(head, 27);
insert(head, 28);
display(head);
}
Arif Ali Shaik
  • 189
  • 1
  • 8
  • Do some research about how C passes arguments to function (hint: it make *copies*) and how to *emulate pass by reference in C*. – Some programmer dude Jan 15 '18 at 11:35
  • 2
    There is not any function with name `create()` in your entire program. – Gaurav Pathak Jan 15 '18 at 11:36
  • 1
    I understand your head() function is your creat() function. In order for your code to work fine, you just need to change the line printf("%d \n", head -> data); to printf("%d \n", ptr -> data); – Dixel Jan 15 '18 at 12:51
  • Hey, @jhonnieDiangi thanks for understanding that, I have edited the function name. – Arif Ali Shaik Jan 15 '18 at 13:32
  • You are welcome. My full solution for you is to comment the line "head = newNode;" in insert(). Then to add this line "return newNode;" at the end of inser(). Finally, every time you insert a node in your main(), make shure to update head like this: head = insert(head, 15); – Dixel Jan 15 '18 at 13:46
  • @jhonnieDiangi I have a written an another program for a Singly Linked List. It is not a stack. Its just the normal SlinedList that does not follow the LIFO principle of stack. ( So nodes can be inserted at any position including 0th position ). please check the link for this new program : [ https://ideone.com/SRVSEJ ] I have added a comment on Line 45. Though I am not using a pointer to a pointer this is working fine unlike previous program. Please help me out brother. This is creating a confusion in my understanding. – Arif Ali Shaik Jan 16 '18 at 18:13
  • @aash20 Here edited the previous code here: https://ideone.com/O2IQOI however, I'm not quite sure why it works when I return newNode in insert() but doesn't when I just change the pointer to head into newNode inside insert(). It may be linked to the fact that the memory is allocated on the function's stack. I hope you can find someone to answer your question properly as it seems more specific and clear now. – Dixel Jan 16 '18 at 19:44

1 Answers1

1

This function:

void insert(stack *head, int elem) {
    stack *newNode = malloc( sizeof(stack) );
    newNode -> data = elem;
    newNode -> next = head;
    head = newNode;
}

Modifies the local copy of head.

It should be:

void insert(stack **head, int elem) {
    stack *newNode = malloc( sizeof(stack) );
    newNode -> data = elem;
    newNode -> next = *head;
    *head = newNode;
}

And called as insert(&head,elem);.

For greater clarity why not:

void create(stack **head){
   *head=NULL;
}

Rather than allocating space in the body of the code.

And

void destroy(stack** head){
    while(*head!=NULL){
        stack* next=(*head)->next;
        free(*head);
        *head=next;
    }
}
Persixty
  • 8,165
  • 2
  • 13
  • 35
  • I have added comment to throw more lite on the same. I have a written an another program for a Singly Linked List. It is not a stack. Its just the normal SlinedList that does not follow the LIFO principle of stack. ( So nodes can be inserted at any position including 0th position ). please check the link for this new program : [ https://ideone.com/SRVSEJ ] I have added a comment on Line 45. Though I am not using a pointer to a pointer this is working fine unlike previous program. Please help me out brother. This is creating a confusion in my understanding. – Arif Ali Shaik Jan 16 '18 at 18:15
  • @aash20 In the linked code you don't change what the head element is. The insert code treats 0 as a special case and changes the values rather than creating a new node. – Persixty Jan 17 '18 at 13:33
  • Hey now what do you have to say in this new link below, I am changing the head element. request you to talk about line 45. It executes perfectly now. [ https://ideone.com/F8cpIO ] @Persixty – Arif Ali Shaik Jan 17 '18 at 19:10