
I was expecting A->next to store the address of temp, no?Īt no point do you get the address of temp. What next node? You only ever created one node. So A contains the the address of the node allocated by malloc. Temp contains the the address of the node allocated by malloc. When we did A = temp, this is not clear, what did we assigned exactly? Second, when we created node temp and assigned 2 to its data, and NULL to its next, it's all clear, but when we did A = temp, this is not clear, what did we assigned exactly? I mean, how can I get from A to the next node, A now has A->data = 2 and A->next = NULL, I was expecting A->next to store the address of temp, no? Please if you can explain in the simplest terms the basic abstract inner workings?įor example, if I want to create a linked list with two elements 2 and 3. We create a node A, my first question here, why I can't use the same concept of accessing its address and content NULL the same as in Example 2 or how can I do that? In the node example, the basic idea is to create an initial node, and then from there, to link other nodes when inserting at the end for instance. Such that p holds the address of a and *p holds the content of it. I have understood how pointers work, for example: //Example 2 Struct Node* next // pointer to the next node This is my initial copied code: #include Printf("\n1.Push\n2.Pop\n3.Show\n4.I am new to C and I want to implement the linked list. Printf("\n\nChose one from the below options.\n") The next node of the head node now becomes the head node.

In order to push an element onto the stack, the following steps are involved.

Pushing an element to a stack in linked list implementation is different from that of an array implementation. Adding a node to the stack (Push operation)Īdding a node to the stack is referred to as push operation. Lets discuss the way in which, each operation is performed in linked list implementation of stack. The top most node in the stack always contains null in its address field. Stack is said to be overflown if the space left in the memory heap is not enough to create a node. Each node contains a pointer to its immediate successor node in the stack. In linked list implementation of stack, the nodes are maintained non-contiguously in the memory.

However, time complexity in both the scenario is same for all the operations i.e.

Linked list allocates the memory dynamically. Instead of using array, we can also use linked list to implement stack. Next → ← prev Linked list implementation of stack
