0% found this document useful (0 votes)
762 views

Dsu Micro Project

This C program implements a stack using a linked list. It contains functions to push, pop and display elements in the stack. The main function takes user input to call these functions in a loop until the user chooses the exit option. It allocates memory for new nodes, adds to the front of the list on push and removes from the front on pop by changing the head pointer.

Uploaded by

Noor alam Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
762 views

Dsu Micro Project

This C program implements a stack using a linked list. It contains functions to push, pop and display elements in the stack. The main function takes user input to call these functions in a loop until the user chooses the exit option. It allocates memory for new nodes, adds to the front of the list on push and removes from the front on pop by changing the head pointer.

Uploaded by

Noor alam Shaikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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