Section 1 Project Objectives Makes use of Cstring library Re
Solution
SECTION - 1 :-
//Here we are using stack principle to insert and delete element
#include <stdio.h>
#include <stdlib.h>//as we are using malloc() and free()
typedef struct llist
{
int data;
struct llist *next;// next is used to denote next element/data in the stack
}stk;
stk *top=NULL;//pointer variable to denote top position of the stack
void push(int ele);//Function to push the element to be inserted into the stack
int pop();//Function to delete the element at the top position following principle LIFO(List In First Out)
void display();//Function to display the stack of elements after respective desired operations
int main()
{
int ch,ele;/*ch variable is used to select the option from switch case(1/2/3/4 options) & ele variable is used to insert the integer element into the stack*/
do/*do while is used as I wanted to display first the intial statements as mentioned below then considering the condition while(1) ...insertion and respective deletion operation takes place*/
{
printf(\"1.INSERTION\ \");
printf(\"2.DELETION\ \");
printf(\"3.DISPLAY\ \");
printf(\"4.EXIT\ \");
scanf(\"%d\",&ch);
switch(ch)
{
case 1 : printf(\"Enter Insertion Element\ \");
scanf(\"%d\",&ele);
push(ele);
break;
case 2 : printf(\"Enter Deletion Element\ \");
scanf(\"%d\",&ele);
pop();
break;
case 3 : display();
break;
case 4 : exit(0);
break;
}
}while(1);
}
void push(int ele)
{
stk *node;
node=(stk *)malloc(sizeof (stk));//Dynamic memory allocation using malloc() for node pointer variable
node->data=ele;//data is of integer type and element is placed in first position
node->next=top;//as it follows stack principle next element to be inserted is pointed as first position
top=node;
}
int pop()
{
int ele;
stk *cur;//declared cur pointer variable to place the first element in the stack into this variable
if(top==NULL)
{
printf(\"stk is empty\ \");
return 0;
}
else
{
cur=top;//top positioned data is placed into cur
top=top->next;//pointing the second data in the stack as first position(top)
ele=cur->data;//sending the data in cur (top element) into ele variable
free(cur);//after these operations deallocating memory of cur having deleted data
return ele;/*returning the ele variable containing top element to the main function switch case2 for deletion operation*/
}
}
void display()
{
stk *cur;//declaring cur pointer variable as a temporary variable to hold top positioned data
if(top==NULL)
{
printf(\"stk is empty\ \");
return ;
}
else
{
cur=top;//sending top positioned data into cur pointer variable
while(cur!=NULL)/*untill the stack first position i.e cur variable which is pointing top position becomes NULL i.e empty loop runs*/
{
printf(\"%d\ \",cur->data);//prints the top positioned integer data
cur=cur->next;//cur variable holding next element data and again printing the element till cur=NULL
}
}
}


