Lists, Stacks and Queues
Lists, Stacks and
Queues
(Implementation)
BASIC DATA STRUCTRES
PRAKASH HEGADE
SCHOOL OF CSE | KLE Tech
PH 1
Lists, Stacks and Queues
1. Stack
#include <stdio.h> int full(STACK *S) {
#include <stdlib.h> if(S->top == STACKSIZE-1)
#define STACKSIZE 5 return TRUE;
#define TRUE 1 else
#define FALSE 0 return FALSE;
}
struct stack
{ void push(STACK *S) {
int top; if(full(S)){
int items[STACKSIZE]; printf("Stack full\n");
}; return;
typedef struct stack STACK; }
void push(STACK *); int x;
void pop(STACK *); printf("Enter the item to be pushed\n");
void print(STACK *); scanf("%d", &x);
void peek(STACK *);
int empty(STACK *); S->top++;
int full(STACK *); S->items[S->top] = x;
int main() }
{
STACK S; int empty(STACK * S) {
[Link] = -1; if(S->top == -1)
int choice=0; return TRUE;
else
while(1) { return FALSE;
printf("\n Menu\n"); }
printf("1-PUSH\n");
printf("2-POP\n"); void pop(STACK *S) {
printf("3-PEEK\n"); if(empty(S)){
printf("4-PRINT\n"); printf("Stack Empty\n");
printf("5-EXIT\n"); return;
printf("Enter your choice\n"); }
scanf("%d", &choice);
switch(choice) { int x;
case 1: push(&S); x = S->items[S->top];
break; printf("Popped item is %d\n", x);
case 2: pop(&S); S->top--;
break; }
case 3: peek(&S);
break; void peek(STACK *S) {
case 4: print(&S); if(empty(S)){
break; printf("Stack Empty\n");
case 5: printf("Terminating\n"); return;
exit(1); }
}
} int x;
return 0; x = S->items[S->top];
} printf("Peeked item is %d\n", x);
}
PH 2
Lists, Stacks and Queues
void print(STACK *S) {
if(empty(S)){
printf("Stack Empty\n");
return;
}
int i;
for(i = S->top; i >= 0; i--)
printf("| %d |\n", S->items[i]);
}
2. Linear Queue
#include <stdio.h>
#include <stdlib.h> while(1){
#define QUEUESIZE 5 printf("MENU\n");
#define TRUE 1 printf("1-Enqueue\n");
#define FALSE 0 printf("2-Dequeue\n");
printf("3-Display\n");
struct queue printf("4-Exit\n");
{
int front; printf("\nEnter your choice\n ");
int rear; scanf("%d", &choice);
int items[QUEUESIZE];
}; switch(choice){
typedef struct queue QUEUE; case 1: enqueue(&q);
break;
void enqueue(QUEUE *); case 2: dequeue(&q);
void dequeue(QUEUE *); break;
void display(QUEUE *); case 3: display(&q);
int full(QUEUE *); break;
int empty(QUEUE *); case 4: printf("Terminating\n");
exit(0);
int main() }
{ }
QUEUE q;
[Link] = 0; return 0;
[Link] = -1; }
int choice;
/// Function Name: full
/// Description: checks if rear end has reached max position
/// Input Param: Pointer to queue
/// Return type: TRUE if queue full, FALSE otherwise
int full(QUEUE *q) {
if(q->rear == QUEUESIZE - 1)
return TRUE;
else
return FALSE;
}
PH 3
Lists, Stacks and Queues
/// Function Name: enqueue
/// Description: enqueue an item inside queue
/// Input Param: Pointer to queue
/// Return type: void
void enqueue(QUEUE *q) {
if(full(q)){
printf("Queue full\n");
return;
}
int x;
printf("Enter the enqueue item\n");
scanf("%d", &x);
q->rear++;
q->items[q->rear] = x;
}
/// Function Name: empty
/// Description:
/// item f r
/// ------------------------------------------------------------------
/// initial 0 -1
/// one insertion/deletion 1 0
/// ...
/// n insertions/deletions n n-1`
/// Input Param: Pointer to queue
/// Return type: TRUE if queue empty, FALSE otherwise
int empty(QUEUE *q) {
if(q->front > q->rear)
return TRUE;
else
return FALSE;
}
/// Function Name: dequeue
/// Description: dequeue an item from queue
/// Input Param: Pointer to queue
/// Return type: void
void dequeue(QUEUE *q) {
if(empty(q)){
printf("Empty queue\n");
return;
}
int x;
x = q->items[q->front];
printf("Dequeued Item is %d\n", x);
q->front++;
}
PH 4
Lists, Stacks and Queues
/// Function Name: display
/// Description: display the items from queue
/// Input Param: Pointer to queue
/// Return type: void
void display(QUEUE *q) {
if(empty(q)) {
printf("Empty Queue\n");
return;
}
int i;
for(i = q->front; i<= q->rear; i++)
printf("%d\n", q->items[i]);
}
3. Circular Queue
#include <stdio.h> printf("\n**** MENU ****\n");
#include <stdlib.h> printf("1 - Enqueue\n");
#define MAXQUEUE 5 printf("2 - Dequeue\n");
#define TRUE 1 printf("3 - Display\n");
#define FALSE 0 printf("4 - Exit\n");
struct cqueue { printf("**************\n");
int front; printf("Enter your choice: ");
int rear; scanf("%d", &choice);
int items[MAXQUEUE];
}; switch(choice) {
typedef struct cqueue CQUEUE; case 1:
Enqueue(&cq);
void Enqueue(CQUEUE *); break;
void Dequeue(CQUEUE *); case 2: Dequeue(&cq);
int empty(CQUEUE *); break;
int full(CQUEUE *); case 3: display(&cq);
void display(CQUEUE *); break;
case 4: printf("Program will Exit. \n");
int main() { exit(0);
int choice =0; }
CQUEUE cq; }
[Link] = MAXQUEUE - 1; return 0;
[Link] = MAXQUEUE - 1; }
while(1) {
int empty(CQUEUE *pcq) { int full(CQUEUE *pcq){
if(pcq->front == pcq->rear) if(pcq->front == (pcq->rear+1) % MAXQUEUE)
return TRUE; return TRUE;
else else
return FALSE; return FALSE;
} }
PH 5
Lists, Stacks and Queues
void Enqueue(CQUEUE *pcq) { x = pcq->items[pcq->front];
if(full(pcq)){ printf("%d Dequeued\n", x);
printf("Queue full\n"); }
} }
else {
int x;
printf("Enter item to insert:\t"); void display(CQUEUE *pcq)
scanf("%d", &x); {
pcq->rear = (pcq->rear+1) % MAXQUEUE; if(empty(pcq))
pcq->items[pcq->rear] = x; printf("Queue Empty\n");
printf("Insertion Successful\n"); else {
} int i;
} printf("Queue Contents are:\n");
i = (pcq->front + 1) % MAXQUEUE;
void Dequeue(CQUEUE *pcq) while(i != pcq->rear) {
{ printf("%d\n", pcq->items[i]);
if(empty(pcq)){ i = (i+1) % MAXQUEUE;
printf("Queue empty\n"); }
} printf("%d\n", pcq->items[i]);
else { printf("\n");
int x; }
pcq->front=(pcq->front+1)% MAXQUEUE; }
4. Linear Queue as Double Ended Queue
#include <stdio.h> printf("1 - Enqueue Rear\n");
#include <stdlib.h> printf("2 - Enqueue Front\n");
#define MAXQUEUE 5 printf("3 - Dequeue Rear\n");
#define TRUE 1 printf("4 - Dequeue Front\n");
#define FALSE 0 printf("5 - Display\n");
struct dqueue { printf("6 - Exit\n");
int front; printf("**************\n");
int rear; printf("Enter your choice: ");
int items[MAXQUEUE]; scanf("%d", &choice);
};
typedef struct dqueue DQUEUE; switch(choice) {
case 1: EnqueueRear(&q);
void EnqueueRear(DQUEUE *); break;
void DequeueFront(DQUEUE *); case 2: EnqueueFront(&q);
void EnqueueFront(DQUEUE *); break;
void DequeueRear(DQUEUE *); case 3: DequeueRear(&q);
void Display(DQUEUE *); break;
int empty(DQUEUE *); case 4: DequeueFront(&q);
int full(DQUEUE *); break;
case 5: Display(&q);
int main() { break;
int choice =0, x = 0; case 6: printf("Program will Exit. \n");
DQUEUE q; exit(0);
[Link]=0; }
[Link]=-1; }
while(1) { return 0;
printf("\n**** MENU ****\n"); }
PH 6
Lists, Stacks and Queues
// Increment Rear and Insert // Remove and Decrement rear
void EnqueueRear(DQUEUE *pdq) void DequeueRear(DQUEUE *pdq)
{ {
if(full(pdq)) if(empty(pdq))
printf("Queue full\n"); printf("Empty Queue\n");
else { else
int x; {
printf("Enter Enqueue Item\n"); int x;
scanf("%d", &x); x = pdq->items[pdq->rear];
(pdq->rear)++; (pdq->rear)--;
pdq->items[pdq->rear]=x; printf("%d Dequeued\n", x);
} }
} }
// Decrement front and Insert int empty(DQUEUE *pdq)
void EnqueueFront(DQUEUE *pdq) {
{ if(pdq->front > pdq->rear)
// We can insert at front only if front is not equal to zero return TRUE;
if (pdq->front != 0){ else
int x; return FALSE;
printf("Enter Enqueue Item\n"); }
scanf("%d", &x);
(pdq->front)--; int full(DQUEUE *pdq)
pdq->items[pdq->front]=x; {
} if(pdq->rear == MAXQUEUE-1)
else return TRUE;
printf("Enqueue Invalid\n"); else
} return FALSE;
}
// Remove and Increment front
void DequeueFront(DQUEUE *pdq) void Display(DQUEUE *pdq)
{ {
if(empty(pdq)) if(empty(pdq))
printf("Empty Queue\n"); printf("Empty Queue\n");
else { else{
int x; int i= 0;
x = pdq->items[pdq->front]; printf("Queue Items are:\n");
(pdq->front)++; for(i=pdq->front; i<=pdq->rear; i++)
printf("%d Dequeued\n", x); printf("%d\n", pdq->items[i]);
} }
} }
6. Circular Queue as DECK
#include <stdio.h> int items[MAXQUEUE];
#include <stdlib.h> int front;
#define MAXQUEUE 5 int rear;
#define TRUE 1 };
#define FALSE 0 typedef struct dequeue DEQUEUE;
struct dequeue {
PH 7
Lists, Stacks and Queues
void EnqueueFront(DEQUEUE *); printf("****************\n");
void DequeueFront(DEQUEUE *); printf("Enter your choice:\t");
void EnqueueRear(DEQUEUE *); scanf("%d", &choice);
void DequeueRear(DEQUEUE *); switch(choice)
int empty(DEQUEUE *); {
int full(DEQUEUE *); case 1: EnqueueFront(&dq);
void Display(DEQUEUE *); break;
int main() { case 2: EnqueueRear(&dq);
int choice =0; break;
DEQUEUE dq; case 3: DequeueFront(&dq);
[Link]=MAXQUEUE-1; break;
[Link]=MAXQUEUE-1; case 4: DequeueRear(&dq);
while(1) break;
{ case 5: Display(&dq);
printf("\n **** MENU ****\n"); break;
printf("1 - Enqueue Front\n"); case 6: printf("Program will exit\n");
printf("2 - Enqueue Rear\n"); exit(0);
printf("3 - Dequeue Front\n"); }
printf("4 - Dequeue Rear\n"); }
printf("5 - Display\n"); return 0;
printf("6 - Exit\n"); }
int empty(DEQUEUE *pdq) {
if(pdq->front == pdq->rear)
return TRUE;
else
return FALSE;
}
int full(DEQUEUE *pdq){
if(pdq->front == (pdq->rear+1)% MAXQUEUE)
return TRUE;
else
return FALSE;
}
void EnqueueFront(DEQUEUE *pdq) {
if(full(pdq))
printf("Queue Full\n");
else {
int x;
printf("Enter Enqueue Item\n");
scanf("%d", &x);
pdq->items[pdq->front] = x;
pdq->front = (pdq->front - 1 + MAXQUEUE)% MAXQUEUE;
}
}
void EnqueueRear(DEQUEUE *pdq) {
if(full(pdq))
printf("Queue Full\n");
else {
PH 8
Lists, Stacks and Queues
int x;
printf("Enter Enqueue Item\n");
scanf("%d", &x);
pdq->rear = (pdq->rear + 1) % MAXQUEUE;
pdq->items[pdq->rear] = x;
}
}
void DequeueFront(DEQUEUE *pdq) {
if(empty(pdq))
printf("Queue Empty\n");
else {
int x;
pdq->front = (pdq->front+1) % MAXQUEUE;
x = pdq->items[pdq->front];
printf("%d Dequeued\n", x);
}
}
void DequeueRear(DEQUEUE *pdq) {
if(empty(pdq))
printf("Queue Empty\n");
else {
int x;
x= pdq->items[pdq->rear];
pdq->rear = (pdq->rear - 1 + MAXQUEUE) % MAXQUEUE;
printf("%d Dequeued\n", x);
}
}
void Display(DEQUEUE *pdq) {
if(empty(pdq))
printf("Queue Empty\n");
else {
int i;
printf("Queue Contents are:\n");
i = (pdq->front + 1) % MAXQUEUE;
while(i != pdq->rear) {
printf("%d\n", pdq->items[i]);
i = (i+1) % MAXQUEUE;
}
printf("%d\n", pdq->items[i]);
}
}
7. Singly Linked List Implementation
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
PH 9
Lists, Stacks and Queues
typedef struct node NODE;
// Maintain the number of nodes in the list in a global variable
int currnodes = 0;
NODE * insert_at_start(NODE * start);
NODE * insert_at_end(NODE * start);
NODE * insert_at_position(NODE * start);
NODE * delete_from_start(NODE * start);
NODE * delete_from_end(NODE * start);
NODE * delete_from_position(NODE * start);
NODE * getnode();
void getdata(NODE *);
void display_list(NODE *start);
int main() {
NODE * start=NULL;
int choice = 0;
while(1) {
printf("\n\n* * * * * * Menu * * * * * *\n");
printf("1. Insert node at start\n");
printf("2. Insert node at End\n");
printf("3. Insert node at a Position\n");
printf("4. Delete node from start\n");
printf("5. Delete node from end\n");
printf("6. Delete node from a Position\n");
printf("7. Display List\n");
printf("8. Exit\n");
printf("* * * * * * ******** * * * * * *\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch (choice){
case 1: start = insert_at_start(start);
break;
case 2: start = insert_at_end(start);
break;
case 3: start = insert_at_position(start);
break;
case 4: start = delete_from_start(start);
break;
case 5: start = delete_from_end(start);
break;
case 6: start = delete_from_position(start);
break;
case 7: display_list(start);
break;
case 8: printf("Exiting program\n\n");
exit(0);
}
}
return 0;
}
PH 10
Lists, Stacks and Queues
// Function to allocate the memory for the struct node
NODE * getnode() {
NODE * newnode;
newnode = (NODE *) malloc(sizeof(NODE));
// If the memory allocation fails malloc will return NULL
if(newnode == NULL)
printf("Memory allocation failed.\n");
// Return the newnode at any case
return newnode;
}
void getdata(NODE * newnode) {
// Get the information from the user, Initialize the next pointer to NULL
printf("Enter the information of node:\n");
scanf("%d", &newnode->data);
newnode->next = NULL;
}
NODE * insert_at_start(NODE * start) {
NODE * newnode;
newnode = getnode();
// Memory allocation failed
if(newnode == NULL)
return start;
// Get the data from the user
getdata(newnode);
// If the list is empty, newnode is the start of the list
if(start == NULL)
start = newnode;
else {
// Add the newnode at the beginning and update the start
newnode->next = start;
start = newnode;
}
// Increment currnodes, print a message and return updated start
currnodes++;
printf("%d is inserted at front of the list\n\n", newnode->data);
return start;
}
NODE * delete_from_start(NODE * start) {
if(start == NULL)
printf("List is Empty!\n");
else {
NODE * tempnode = start;
start=start->next;
printf("%d is deleted from front of the list\n", tempnode->data);
free(tempnode); currnodes--;
}
return start;
}
PH 11
Lists, Stacks and Queues
NODE * insert_at_end(NODE * start) {
NODE * newnode, * nextnode;
newnode = getnode();
if(newnode == NULL)
return start;
getdata(newnode);
if(start == NULL)
start = newnode;
else {
nextnode = start;
while(nextnode->next != NULL)
nextnode = nextnode->next;
nextnode->next = newnode;
}
currnodes++;
printf("%d is inserted at the end of the list\n\n", newnode->data);
return start;
}
NODE * delete_from_end(NODE * start) {
NODE *prevnode, *nextnode;
if(start == NULL)
printf("List is Empty!\n");
else {
if(currnodes == 1) { // or start->next == NULL
nextnode = start;
start=NULL;
}
else {
nextnode = start;
prevnode = NULL;
while(nextnode->next!=NULL) {
prevnode = nextnode;
nextnode = nextnode->next;
}
prevnode->next = NULL;
}
printf("%d is deleted from end of the list.\n", nextnode->data);
free(nextnode);
currnodes--;
}
return start;
}
void display_list(NODE *start) {
NODE * tempnode;
if(start == NULL)
printf("List is Empty!\n");
else {
tempnode = start;
printf("The list contents are:\n");
PH 12
Lists, Stacks and Queues
while(tempnode != NULL) {
printf("%d --> ", tempnode->data);
tempnode = tempnode->next;
}
printf("NULL\n");
}
}
8. Doubly Linked List Implementation
#include <stdio.h>
struct node {
int data;
struct node *next;
struct node *prev;
};
typedef struct node NODE;
int currnodes = 0;
// Function prototypes same as singly linked list
int main() {
NODE * start;
start = NULL;
int choice = 0;
…..
// Will be same as that of singly Linked List
…..
return 0;
}
NODE * getnode() {
NODE * newnode;
newnode = (NODE *)malloc(sizeof(NODE));
if(newnode == NULL)
printf("Memory Allocation Failed\n");
return newnode;
}
void getdata(NODE * newnode) {
printf("Enetr the information for linked list\n");
scanf("%d", &newnode->data);
newnode->next = NULL;
newnode->prev = NULL;
}
NODE * insert_at_front(NODE * start) {
NODE * newnode;
newnode = getnode();
if(newnode == NULL)
return start;
getdata(newnode);
PH 13
Lists, Stacks and Queues
if(start == NULL)
start = newnode;
else {
newnode->next= start;
start->prev=newnode;
start = newnode;
}
currnodes++;
printf("%d info is inserted at the start of the doubly linked list\n", newnode->data);
return start;
}
NODE * insert_at_end(NODE * start) {
NODE * newnode, *tempnode;
newnode = getnode();
if(newnode == NULL)
return start;
getdata(newnode);
if(start == NULL)
start = newnode;
else {
tempnode = start;
while(tempnode->next != NULL)
tempnode = tempnode->next;
tempnode->next = newnode;
newnode->prev = tempnode;
}
currnodes++;
printf("%d info is inserted at the End of the doubly linked list\n", newnode->data);
return start;
}
NODE * delete_from_start(NODE * start) {
NODE * tempnode;
if(start == NULL)
printf("List is empty\n");
else {
if(currnodes == 1) // or start->next == NULL
{
tempnode = start;
start = NULL;
}
else {
tempnode = start;
start = start->next;
start->prev = NULL;
}
printf("Node %d deleted from the start of the Doubly linked list\n", tempnode->data);
free(tempnode);
currnodes--;
}
PH 14
Lists, Stacks and Queues
return start;
}
NODE * delete_from_end( NODE * start) {
NODE * tempnode, *prevnode;
if(start == NULL)
printf("List is empty\n");
else {
if(currnodes == 1)
{
tempnode = start;
start = NULL;
}
else {
tempnode = start;
while(tempnode->next != NULL)
tempnode = tempnode->next;
prevnode = tempnode;
prevnode = prevnode->prev;
prevnode->next = NULL;
}
printf("Node %d deleted from the end of the Doubly linked list\n", tempnode->data);
free(tempnode);
currnodes--;
}
return start;
}
void display_list(NODE * start) {
NODE * tempnode;
if(currnodes == 0)
printf("List Empty\n");
else {
tempnode = start;
printf("The list contents are:\n");
printf("\nNULL <--> ");
while(tempnode != NULL) {
printf(" %d <--> ", tempnode->data);
tempnode = tempnode->next;
}
printf("NULL\n");
}
}
9. Circular Linked List Implementation
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
PH 15
Lists, Stacks and Queues
typedef struct node NODE;
int currnodes = 0;
NODE * insert_at_start(NODE * last);
NODE * insert_at_end(NODE * last);
NODE * delete_from_start(NODE * last);
NODE * delete_from_end(NODE * last);
NODE * getnode();
void getdata(NODE *);
void display_list(NODE *last);
int main( ) {
NODE * last=NULL;
int choice = 0;
while(1) {
printf("\n\n* * * * * * Menu * * * * * *\n");
printf("1. Insert node at start\n");
printf("2. Insert node at End\n");
printf("3. Delete node from start\n");
printf("4. Delete node from end\n");
printf("5. Display List\n");
printf("6. Exit\n");
printf("* * * * * * ******** * * * * * *\n");
printf("Enter your choice:\n");
scanf("%d", &choice);
switch (choice) {
case 1: last = insert_at_start(last);
break;
case 2: last = insert_at_end(last);
break;
case 3: last = delete_from_start(last);
break;
case 4: last = delete_from_end(last);
break;
case 5: display_list(last);
break;
case 6: printf("Exiting program\n\n");
exit(0);
}
}
return 0;
}
NODE * getnode() {
NODE * newnode;
newnode = (NODE *) malloc(sizeof(NODE));
if(newnode == NULL)
printf("Memory allocation failed.\n");
return newnode;
}
void getdata(NODE * newnode) {
printf("Enter the information of node:\n");
scanf("%d", &newnode->data);
PH 16
Lists, Stacks and Queues
newnode->next = NULL;
}
NODE * insert_at_start(NODE * last) {
NODE * newnode;
newnode = getnode();
if(newnode == NULL)
return last;
getdata(newnode);
if(last == NULL)
last = newnode;
else
newnode->next = last->next;
last->next = newnode;
currnodes++;
printf("%d is inserted at front of the circular list\n\n", newnode->data);
return last;
}
NODE * insert_at_end(NODE * last) {
NODE * newnode;
newnode = getnode();
if(newnode == NULL)
return last;
getdata(newnode);
if(last == NULL)
last = newnode;
else
newnode->next = last->next;
last->next = newnode;
currnodes++;
printf("%d is inserted at the end of the list\n\n", newnode->data);
return newnode;
}
NODE * delete_from_start(NODE * last) {
NODE * tempnode;
if(last == NULL)
printf("List is Empty!\n");
else {
if(last->next == last) {
tempnode = last;
last = NULL;
}
else {
tempnode = last->next;
last->next = tempnode->next;
}
printf("%d is deleted from front of the list\n\n", tempnode->data);
PH 17
Lists, Stacks and Queues
free(tempnode);
currnodes--;
}
return last;
}
NODE * delete_from_end(NODE * last) {
NODE *prevnode = NULL;
if(last == NULL){
printf("List is Empty!\n");
return last;
}
else {
if(currnodes == 1) {
printf("%d is deleted from end of the list.\n", last->data);
free(last);
currnodes--;
return NULL;
}
else {
prevnode = last->next;
while(prevnode->next != last)
prevnode = prevnode->next;
prevnode->next = last->next;
printf("%d is deleted from end of the list.\n", last->data);
free(last);
currnodes--;
return prevnode;
}
}
}
void display_list(NODE *last)
{
NODE * tempnode;
if(last == NULL)
printf("List is Empty!\n");
else {
tempnode = last->next;
printf("The list contents are:\n");
while(tempnode != last) {
printf("%d --> ", tempnode->data);
tempnode = tempnode->next;
}
printf("%d --> ", tempnode->data);
}
}
~*~*~*~*~*~*~
PH 18