Posts

Showing posts with the label implementation

Stack implementation through Linked list in C

Image
Stack implementation through Linked list in C We can avoid the size limitation of a stack implemented with an array, with the help of a linked list to hold the stack elements. As needed in case of array, we have to decide where to insert elements in the list and where to delete them so that push and pop will run at the fastest. Primarily, there are two operations of a stack; push() and pop() . A stack carries lifo behavior i.e. last in, first out. You know that while implementing stack with an array and to achieve lifo behavior, we used push and pop elements at the end of the array. Instead of pushing and popping elements at the beginning of the array that contains overhead of shifting elements towards right to push an element at the start and shifting elements towards left to pop an element from the start. To avoid this overhead of shifting left and right, we decided to push and pop elements at the end of the array. Now, if we use linked list to implement the stack, where wi...

Sorted Linked List implementation in C

Sorted Linked List implementation in C include "stdio.h" include "stdlib.h" include "conio.h" void del(int data); void insert(int value); void display(); struct node {     int data;     struct node *link; }; struct node *top=NULL,*temp, *temp1, *temp2, *temp3; int main() {     int choice,data;       while(1) //infinite loop is used to insert/delete infinite number of elements in linked list     {               printf(" 1.Insert 2.Delete 3.Display 4.Exit ");         printf(" Enter ur choice:");         scanf("%d",&choice);         switch(choice)         {         case 1:                                 printf("Enter a new element :");             scanf("%d",&data);      ...

Stack Implementation using array

Image
Stack Implementation using array Lets implement the stack using the arrays. The stack shown in the below diagram may be considered as an array. Here the array is shown vertically. We can implement the stack using array. The interface will remain as push and pop methods. The user of the stack does not need to know that the stack is internally implemented with the help of array. The worst case for insertion and deletion from an array may happen when we insert and delete from the beginning of the array. We have to shift elements to the right for insertion and left for removal of an element. We face the same problem while implementing the list with the use of the array. If we push and pop the elements from the start of the array for stack implementation, this problem will arise. In case of push, we have to shift the stack elements to the right. However, in case of pop, after removing the element, we have to shift the elements of stack that are in the array to the left. If we push the eleme...