Dsu Micro Project

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

NAME ENROLLRMENT CLASS

SAHIKH NOOR ALAM 1905690291 CO3IA


AMANULLAH SHAIKH 1905690299 CO3IA
AFNAN RSEES SHAIKH 1905690349 CO3IA
SHAIKH SHAHBAAZ AMBIA 1905690336 CO3IA
KHAN MOHD MEHDI 1905690342 CO3IA
SOURCE CODE:-
#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int val;
struct node *next;
};
struct node *head;

int main ()
{
int choice=0;
printf("\n----------------------------------------------
");
printf("\n*********Stack operations using
linked list*********\n");
printf("----------------------------------------------\n");
while(choice != 4)
{
printf("\n\n*****Chose 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 ");
}
};
}
}
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");

}
}

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");

}
}
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;
}
}
}

You might also like