0% found this document useful (0 votes)
21 views11 pages

Files 1 2021 March NotesHubDocument 1615875264

The document discusses implementing stacks and queues as linked lists in C. It includes functions to push/pop from stacks, enqueue/dequeue from queues, check if they are empty, display contents, and get the top/front element. Code snippets are provided to demonstrate creating nodes and linked list structures for implementing the stack and queue data structures.

Uploaded by

Kanhaiya Ji Raj
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)
21 views11 pages

Files 1 2021 March NotesHubDocument 1615875264

The document discusses implementing stacks and queues as linked lists in C. It includes functions to push/pop from stacks, enqueue/dequeue from queues, check if they are empty, display contents, and get the top/front element. Code snippets are provided to demonstrate creating nodes and linked list structures for implementing the stack and queue data structures.

Uploaded by

Kanhaiya Ji Raj
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/ 11

STACK IMPLEMENTATION AS LINKED LIST

*C Program to Implement a Stack using Linked List

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

struct node

int info;
struct node *ptr;

top, top1,temp;
int topelement(0:
void push(int data);
void pop)
void empty0;
void display0;
void destroy);
void stackcount();
void create();
int count= 0;

void main0
int no, ch, e

printf("\n 1-Push")
printf(in 2-Pop");
printf("\n 3-Top");
printf("in 4- Empty");
printf(n5-Exit");
printf("\n 6- Dipslay");
printf("in7-Stack Count ):
printf("in 8- Destroy stack);

create):

while (1)
printf("n Enter choice:);
scanf("%d", &ch);
switch (ch)

case 1
printf(Enter data: ");
scanf(%d', &no)
push(no);
break;
case
pop0
break;
case .
if (top == NULL)

printf("No elements in stack");


else
e = topelement();
printi("'\n Top element: %d', e);

break;
case 4
empty0,
Dreak:

case 5
exit(0);
case 6
display0:
break;
case

stack count):
break;
case 8
destroy):
break;
default
printf( Wrong choice, Please enter correct choice "):
break;

* Create empty stack */


void create)

top NULL

*Count stack elements 7


void stack_count()

printf("n No. of elements in stack: %d", count);

/* Push data into stack */


void pushint data)
if (top == NULL)

top =(struct node ")malloc(1*sizeof(struct node);


top->ptr= NULL
top->info = data;

else

temp -(struct node ")malloc(1 *sizeof(struct node);


temp->ptr = top,

temp->info = data;

top = temp;

Count++

* Display stack elements /

void display()
top1 = top

if (top1 = NULL)

printf(Stack is empty);
return

while (top1 = NULL)

printf(%d, top1->info);
top1 top1->ptr,

* Pop Operation on stack */


void pop
top1 = top

if(top1== NULL)
printf("\n Error: Trying to pop from empty stack);
return;

else
top1 top1->ptr
printf(\n Popped value: %d"', top->info);
free(top)
top = top1

count

/* Return top element */


int topelement()
return top->info);

* Check if stack is empty or not


void empty0
if (top == NULL)

printf("n Stack is empty");


else
printf("'\n Stack is not empty with %d elements", count);

* Destroy entire stack */


void destroy)

top1 = t o p

while (top1 != NULL)

top top->ptr,
free(top)
top= top1;
topi=top1->ptr,
free(top1)
top = NULL;

printf("in All stack elements destroyed');


count 0

Queues as linked list

CProgram to Implement Queue Data Structure using Linked List

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

struct node

intinfo;
struct node "ptr,
front, rear,temp, front1;

int frontelement();
void enq(int data);
void deq0;
void empty0:
void display0
void create();
void queuesize );

int count 0;
void main(0
int no, ch, e
printf("\n 1-Enque");
printf("n 2- Deque");
printf("in 3-Front element');
printf(\n 4- Empty")
printf("\n 5- Exit");
printfin 6- Display");
printf(n 7-Queue size");
create,
while (1)
printf("in Enter choice: ");
Scanf%d, &ch);
Switch (ch)

case
printf(Enterdata: ");
scanf(%d', &no);
eng(no);
break;
case 2
deq0:
break
case 3:
e= frontelement();
if (el= 0)
printf(Front element: %d", e);
else
printf("\n No front element in Queue as queue is empty)
break;
case 4
empty0:
break;
case

exit(0)
case 6:
display0:
break;
case 7:

queuesize();
break;
default
printf(Wrong choice, Please enter correct choice ");
brea

* Create an empty queue


void create()
front r e a r = NULL;

Returns queue size/


void queuesize0

printf('n Queue size:%d', count);


|/* Enqueing the queue 7
void enq(int data)
if (rear == NULL)

rear (struct node *)malloc(1*sizeof(struct node));


rear->ptr = NULL;

rear->info = data;
front = rear,

else

temp=(struct node *)malloc(1 *sizeof(struct node));


rear->ptr = temp;

temp-info = data;
temp->ptr = NULL;

rear= temp,

count+
*Displaying the queue elements
oid display)

front1 = front;

if ((front1 == NULL) && (rear == NULL))

printf('Queue is empty");
return
while (front1 != rear)

printf(%d , front1->info);
front1 =
front 1->ptr,
f (front1 == rear)

printf(%d', front1->info);

Dequeing the queue *7


*

oid deq0
front1 = front;

f (front1 = = NULL)

printfC'in Error: Trying to display elements from empty queue")


return,

else
if (front1-ptr != NULL)

front1 front1->ptr
printf("\n Dequed value: %d", front->info);
free(front)
front= front1;

else

printf("in Dequed value: %d", front->info);


free(front)
front NULL;
rear = NULL;

cOunt

* Returns the front element ofqueue


nt frontelement)

if (front I= NULL) && (rear != NULL))


return(front->info);
else
return 0

/* Display if queue is empty or not*/


oicd amntv
if((front == NULL) &&(rear == NULL))
printf("\n Queueempty");
else
printf('Queue notempty");
CProgram to Implement Queue Data Structure using Linked List

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

struct node

intiinfo;
struct node *ptr;
Pfront,*rear,temp,front1;
int frontelement):
void enq(int data);
void deq();
void empty0;
void display):
void create();
void queuesize();

intcount=0;
void main()0
int no, ch, e;

printf("\n 1-Enque");
printf("n 2- Deque")
printf("\n 3- Front element");
printf("n 4-Empty");
printf("\n 5- Exit")
printf("in 6-Display):
printf(\n7-Queue size");
create)
while (1)

printf('in Enter choice : ");


Scanf(%d", &ch);
SWitch (Ch)

case 1:
printf("Enter data: "):
scanf(%d', &no);
eng(no);
break;
case 2:
deq0:
break;
case

e= frontelement();
if (e l= 0)
printf("Front element : %d", e);
else
printf('in No front element in Queue as queue is empty");
brea
case
4
empty0
break;
case 5:
exit(0);
case
6
display):
break;
case 7:
queuesize();
break;
default:
printf(Wrong choice, Please enter correct choice ");
break;

|/* Create an empty queue 7


void create(0
front = rear = NULL,

|/* Returns queue size


void queuesize)
printf("\n Queue size: %d", count);:

Enqueing the queue */


void enq(int data)
if (rear == NULL)

rear= (struct node *)malloc(1*sizeof(struct node);


rear->ptr =NULL;
rear->info = data;

front = rear

else

temp-(struct node *)malloc(1*sizeof(struct node);


rear->ptr = temp;

temp->info = data;
temp->ptr = NULL;

rear = temp,

count++
* Displaying the queue elements
void display0
front1 front;

if ((front1 == NULL) && (rear = = NULL))

printf('Queue is empty");
return
while (front1 = rear)

printf("%d", front1->info);
fronti = front1->ptr;

if (front1 == rear)

printf(%d', front1->info);

* Dequeing the queue /


Void deq)0
front1 = front;

if (front1 == NULL)

printf("'in Eror: Trying to display elements from empty queue");


return;

else
if (front1->ptr = NULL)

front1 = front1->ptr

printf("\n Dequed value: %d", front->info);


free(front);
front = front1;

else

printf("\n Dequed value: %d", front->info);


free(front)
front NULL;
rear NULL;

COunt-

*Returns the front element of queue /


int frontelement)
if (front != NULL) && (rear I= NULL))
return front->info);
else
return 0;

* Display if queue is empty or not


void empty0
if((front == NULL) &&(rear == NULL))
printf("\nQueue empty");
else
printf(Queuenot empty");

You might also like