diff --git a/C/Program-71 b/C/Program-71 new file mode 100644 index 00000000..2adf2599 --- /dev/null +++ b/C/Program-71 @@ -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; + } + } +} 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]); + } +} diff --git a/C/README.md b/C/README.md index e484fb77..718a4f7d 100644 --- a/C/README.md +++ b/C/README.md @@ -73,3 +73,7 @@ | Program-69 | Program to find strong number | | Program-70 | Program to make a simple calculator | | Program-71 | Program to implement stack using array & linked list | +| Program-72 | Program to implement stack using linked list | +| Program-73 | Program to implement stack using array | + + diff --git a/Program-72 b/Program-72 new file mode 100644 index 00000000..6c075b7c --- /dev/null +++ b/Program-72 @@ -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]); + } +} +