From 3d528d95e5a3daa18c09ac5b44d72b6be058b350 Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Sun, 3 Oct 2021 17:01:49 +0530 Subject: [PATCH 1/7] Create Program -70 Linked list implementation of stack Instead of using array, we can also use linked list to implement stack. Linked list allocates the memory dynamically. However, time complexity in both the scenario is same for all the operations i.e. push, pop and peek. In linked list implementation of stack, the nodes are maintained non-contiguously in the memory. Each node contains a pointer to its immediate successor node in the stack. Stack is said to be overflown if the space left in the memory heap is not enough to create a node. DS Linked list implementation stack The top most node in the stack always contains null in its address field. Lets discuss the way in which, each operation is performed in linked list implementation of stack. Adding a node to the stack (Push operation) Adding a node to the stack is referred to as push operation. Pushing an element to a stack in linked list implementation is different from that of an array implementation. In order to push an element onto the stack, the following steps are involved. Create a node first and allocate memory to it. If the list is empty then the item is to be pushed as the start node of the list. This includes assigning value to the data part of the node and assign null to the address part of the node. If there are some nodes in the list already, then we have to add the new element in the beginning of the list (to not violate the property of the stack). For this purpose, assign the address of the starting element to the address field of the new node and make the new node, the starting node of the list. Time Complexity : o(1) DS Linked list implementation stack C implementation : void push () { int val; struct node *ptr =(struct node*)malloc(sizeof(struct node)); if(ptr == NULL) { printf("not able to push the element"); } else { printf("Enter the value"); scanf("%d",&val); if(head==NULL) { ptr->val = val; ptr -> next = NULL; head=ptr; } else { ptr->val = val; ptr->next = head; head=ptr; } printf("Item pushed"); } } Deleting a node from the stack (POP operation) Deleting a node from the top of stack is referred to as pop operation. Deleting a node from the linked list implementation of stack is different from that in the array implementation. In order to pop an element from the stack, we need to follow the following steps : Check for the underflow condition: The underflow condition occurs when we try to pop from an already empty stack. The stack will be empty if the head pointer of the list points to null. Adjust the head pointer accordingly: In stack, the elements are popped only from one end, therefore, the value stored in the head pointer must be deleted and the node must be freed. The next node of the head node now becomes the head node. Time Complexity : o(n) C implementation void pop() { int item; struct node *ptr; if (head == NULL) { printf("Underflow"); } else { item = head->val; ptr = head; head = head->next; free(ptr); printf("Item popped"); } } Display the nodes (Traversing) Displaying all the nodes of a stack needs traversing all the nodes of the linked list organized in the form of stack. For this purpose, we need to follow the following steps. Copy the head pointer into a temporary pointer. Move the temporary pointer through all the nodes of the list and print the value field attached to every node. Time Complexity : o(n) C Implementation void display() { int i; struct node *ptr; ptr=head; if(ptr == NULL) { printf("Stack is empty\n"); } else { printf("Printing Stack elements \n"); while(ptr!=NULL) { printf("%d\n",ptr->val); ptr = ptr->next; } } } --- C/Program-70 | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 C/Program-70 diff --git a/C/Program-70 b/C/Program-70 new file mode 100644 index 00000000..2adf2599 --- /dev/null +++ b/C/Program-70 @@ -0,0 +1,121 @@ +#include +#include +void push(); +void pop(); +void display(); +struct node +{ +int val; +struct node *next; +}; +struct node *head; + +void main () +{ + int choice=0; + printf("\n*********Stack operations using linked list*********\n"); + printf("\n----------------------------------------------\n"); + while(choice != 4) + { + printf("\n\nChose one from the below options...\n"); + printf("\n1.Push\n2.Pop\n3.Show\n4.Exit"); + printf("\n Enter your choice \n"); + scanf("%d",&choice); + switch(choice) + { + case 1: + { + push(); + break; + } + case 2: + { + pop(); + break; + } + case 3: + { + display(); + break; + } + case 4: + { + printf("Exiting...."); + break; + } + default: + { + printf("Please Enter valid choice "); + } + }; +} +} +//adding a node +void push () +{ + int val; + struct node *ptr = (struct node*)malloc(sizeof(struct node)); + if(ptr == NULL) + { + printf("not able to push the element"); + } + else + { + printf("Enter the value"); + scanf("%d",&val); + if(head==NULL) + { + ptr->val = val; + ptr -> next = NULL; + head=ptr; + } + else + { + ptr->val = val; + ptr->next = head; + head=ptr; + + } + printf("Item pushed"); + + } +} +// deleting a node +void pop() +{ + int item; + struct node *ptr; + if (head == NULL) + { + printf("Underflow"); + } + else + { + item = head->val; + ptr = head; + head = head->next; + free(ptr); + printf("Item popped"); + + } +} +//display node transversal +void display() +{ + int i; + struct node *ptr; + ptr=head; + if(ptr == NULL) + { + printf("Stack is empty\n"); + } + else + { + printf("Printing Stack elements \n"); + while(ptr!=NULL) + { + printf("%d\n",ptr->val); + ptr = ptr->next; + } + } +} From 2f11a6287b64654a8371877f83847754b433fc94 Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Sun, 3 Oct 2021 17:04:47 +0530 Subject: [PATCH 2/7] Rename Program-70 to Program-71 --- C/{Program-70 => Program-71} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename C/{Program-70 => Program-71} (100%) diff --git a/C/Program-70 b/C/Program-71 similarity index 100% rename from C/Program-70 rename to C/Program-71 From 902e95b5446a19ba9ac954657ad50bddea29dd99 Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Sun, 3 Oct 2021 17:06:07 +0530 Subject: [PATCH 3/7] Update README.md --- C/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/C/README.md b/C/README.md index 8a0b269a..e8df52d0 100644 --- a/C/README.md +++ b/C/README.md @@ -72,3 +72,4 @@ | Program-68 | Program to accept 0s and 1s as input and print if it consists 3 consecutive 0s | | Program-69 | Program to find strong number | | Program-70 | Program to make a simple calculator | +| Program-71 | Program to implement stack using linked list | From 9bbcb3298c02537a8833c1d6782b6b9926e0f9d3 Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Sun, 3 Oct 2021 17:08:01 +0530 Subject: [PATCH 4/7] Update README.md --- C/README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/C/README.md b/C/README.md index e8df52d0..905c0aec 100644 --- a/C/README.md +++ b/C/README.md @@ -73,3 +73,6 @@ | Program-69 | Program to find strong number | | Program-70 | Program to make a simple calculator | | Program-71 | Program to implement stack using linked list | +| Program-72 | Program to implement stack using array | + + From a5026b4973e6b2dcb6f955fba13b1e0d1e6e9469 Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Sun, 3 Oct 2021 17:10:20 +0530 Subject: [PATCH 5/7] Add files via upload Array implementation of Stack In array implementation, the stack is formed by using the array. All the operations regarding the stack are performed using arrays. Lets see how each operation can be implemented on the stack using array data structure. Adding an element onto the stack (push operation) Adding an element into the top of the stack is referred to as push operation. Push operation involves following two steps. Increment the variable Top so that it can now refere to the next memory location. Add element at the position of incremented top. This is referred to as adding new element at the top of the stack. Stack is overflown when we try to insert an element into a completely filled stack therefore, our main function must always avoid stack overflow condition. Algorithm: begin if top = n then stack full top = top + 1 stack (top) : = item; end Time Complexity : o(1) implementation of push algorithm in C language void push (int val,int n) //n is size of the stack { if (top == n ) printf("\n Overflow"); else { top = top +1; stack[top] = val; } } Deletion of an element from a stack (Pop operation) Deletion of an element from the top of the stack is called pop operation. The value of the variable top will be incremented by 1 whenever an item is deleted from the stack. The top most element of the stack is stored in an another variable and then the top is decremented by 1. the operation returns the deleted value that was stored in another variable as the result. The underflow condition occurs when we try to delete an element from an already empty stack. Algorithm : begin if top = 0 then stack empty; item := stack(top); top = top - 1; end; Time Complexity : o(1) Implementation of POP algorithm using C language int pop () { if(top == -1) { printf("Underflow"); return 0; } else { return stack[top - - ]; } } Visiting each element of the stack (Peek operation) Peek operation involves returning the element which is present at the top of the stack without deleting it. Underflow condition can occur if we try to return the top element in an already empty stack. Algorithm : PEEK (STACK, TOP) Begin if top = -1 then stack empty item = stack[top] return item End Time complexity: o(n) Implementation of Peek algorithm in C language int peek() { if (top == -1) { printf("Underflow"); return 0; } else { return stack [top]; } } --- C/Stack implementation using array.c | 65 ++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 C/Stack implementation using array.c diff --git a/C/Stack implementation using array.c b/C/Stack implementation using array.c new file mode 100644 index 00000000..6c075b7c --- /dev/null +++ b/C/Stack implementation using array.c @@ -0,0 +1,65 @@ +#include +#include + +#define SIZE 10 + +void push(int); +void pop(); +void display(); + +int stack[SIZE], top = -1; + +void main() +{ + int value, choice; + clrscr(); + while(1){ + printf("\n\n***** MENU *****\n"); + printf("1. Push\n2. Pop\n3. Display\n4. Exit"); + printf("\nEnter your choice: "); + scanf("%d",&choice); + switch(choice){ + case 1: printf("Enter the value to be insert: "); + scanf("%d",&value); + push(value); + break; + case 2: pop(); + break; + case 3: display(); + break; + case 4: exit(0); + default: printf("\nWrong selection!!! Try again!!!"); + } + } +} +//adding a top stack using array index +void push(int value){ + if(top == SIZE-1) + printf("\nStack is Full!!! Insertion is not possible!!!"); + else{ + top++; + stack[top] = value; + printf("\nInsertion success!!!"); + } +} +//deleting a stack top usin array index +void pop(){ + if(top == -1) + printf("\nStack is Empty!!! Deletion is not possible!!!"); + else{ + printf("\nDeleted : %d", stack[top]); + top--; + } +} +//display stack transversal using array index iteration +void display(){ + if(top == -1) + printf("\nStack is Empty!!!"); + else{ + int i; + printf("\nStack elements are:\n"); + for(i=top; i>=0; i--) + printf("%d\n",stack[i]); + } +} + From 7083a5610c16dc0a92bf23325c032dd0434f4611 Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Sun, 3 Oct 2021 17:11:55 +0530 Subject: [PATCH 6/7] Rename C/Stack implementation using array.c to Program-72 --- C/Stack implementation using array.c => Program-72 | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename C/Stack implementation using array.c => Program-72 (100%) diff --git a/C/Stack implementation using array.c b/Program-72 similarity index 100% rename from C/Stack implementation using array.c rename to Program-72 From 5bf20dbffacc2941833c879f054be95503fcf9f7 Mon Sep 17 00:00:00 2001 From: AyushSingh-github <70777486+AyushSingh-github@users.noreply.github.com> Date: Sun, 3 Oct 2021 17:15:08 +0530 Subject: [PATCH 7/7] Create Program-72 --- C/Program-72 | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 C/Program-72 diff --git a/C/Program-72 b/C/Program-72 new file mode 100644 index 00000000..58a9ca9f --- /dev/null +++ b/C/Program-72 @@ -0,0 +1,64 @@ +#include +#include + +#define SIZE 10 + +void push(int); +void pop(); +void display(); + +int stack[SIZE], top = -1; + +void main() +{ + int value, choice; + clrscr(); + while(1){ + printf("\n\n***** MENU *****\n"); + printf("1. Push\n2. Pop\n3. Display\n4. Exit"); + printf("\nEnter your choice: "); + scanf("%d",&choice); + switch(choice){ + case 1: printf("Enter the value to be insert: "); + scanf("%d",&value); + push(value); + break; + case 2: pop(); + break; + case 3: display(); + break; + case 4: exit(0); + default: printf("\nWrong selection!!! Try again!!!"); + } + } +} +//adding a top stack using array index +void push(int value){ + if(top == SIZE-1) + printf("\nStack is Full!!! Insertion is not possible!!!"); + else{ + top++; + stack[top] = value; + printf("\nInsertion success!!!"); + } +} +//deleting a stack top usin array index +void pop(){ + if(top == -1) + printf("\nStack is Empty!!! Deletion is not possible!!!"); + else{ + printf("\nDeleted : %d", stack[top]); + top--; + } +} +//display stack transversal using array index iteration +void display(){ + if(top == -1) + printf("\nStack is Empty!!!"); + else{ + int i; + printf("\nStack elements are:\n"); + for(i=top; i>=0; i--) + printf("%d\n",stack[i]); + } +}