0% found this document useful (0 votes)
1 views5 pages

C Program to Implement Stack Using Linked List

The document contains C programs that implement a Stack and a Queue using linked lists. The Stack program allows for push, pop, and display operations, while the Queue program supports enqueue, dequeue, and display operations. Both implementations utilize dynamic memory allocation for node creation and manage the linked list structure accordingly.

Uploaded by

koushikmalakar
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)
1 views5 pages

C Program to Implement Stack Using Linked List

The document contains C programs that implement a Stack and a Queue using linked lists. The Stack program allows for push, pop, and display operations, while the Queue program supports enqueue, dequeue, and display operations. Both implementations utilize dynamic memory allocation for node creation and manage the linked list structure accordingly.

Uploaded by

koushikmalakar
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

C Program to implement Stack using Linked List(Pointer)

#include<stdio.h>
#include<stdlib.h>

struct node{
int data;
struct node*next;
}*head=NULL;

struct node *create(int value)


{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
return temp;
}

void push(int value)


{
struct node *newnode;
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
else
{
newnode->next=head;
head=newnode;
}
}

void pop()
{
struct node *temp;
if(head==NULL)
{
1|Page
printf("Stack is underflow");
}
else
{
temp=head;
head=head->next;
free(temp);
}
}

void show()
{
struct node *temp;
if(head==NULL)
{
printf("Stack is empty");
}
else
{
temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next;
}
}
}

void main()
{
int ch,pos,value;
do
{
printf("\n1. Push\n2. Pop\n3. Show\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter the value: ");
scanf("%d",&value);
2|Page
push(value);
break;
case 2: pop();
break;
case 3: show();
break;
case 4:break;
default: printf("\nyour choice is wrong!.. ");
}
}while(ch!=4);
}

C Program to implement Queue using Linked List(pointer)


#include<stdio.h>
#include<stdlib.h>

struct node{
int data;
struct node*next;
}*head=NULL;

struct node *create(int value)


{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
return temp;
}

void enqueue(int value)


{
struct node *newnode, *temp;
newnode=create(value);
if(head==NULL)
{
head=newnode;
}
3|Page
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}

void dequeue()
{
struct node *temp;
if(head==NULL)
{
printf("Queue Underflow");
}
else
{
temp=head;
head=head->next;
free(temp);
}
}

void display()
{
struct node *temp;
if(head==NULL)
{
printf("Queue is empty");
}
else
{
temp=head;
while(temp->next!=NULL)
{
printf("%d, ",temp->data);
temp=temp->next;
4|Page
}
printf("%d",temp->data);
}
}

void main()
{
int ch,pos,value;
do
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter data to insert: ");
scanf("%d",&value);
enqueue(value);
break;
case 2: dequeue();
break;
case 3: display();
break;
case 4:break;
default: printf("\nyour choice is wrong!..");
}
}while(ch!=4);
}

5|Page

You might also like