0% found this document useful (0 votes)
8 views4 pages

Implementation of Queue and Linked List

The document provides a C implementation of a queue and a linked list, detailing functions for enqueueing, dequeueing, and displaying elements in the queue, as well as inserting, deleting, and searching nodes in the linked list. It includes error handling for full and empty states in the queue and demonstrates various operations on the linked list, such as insertion at the beginning, end, and after a specific node. The main function showcases the usage of these data structures and their respective operations.

Uploaded by

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

Implementation of Queue and Linked List

The document provides a C implementation of a queue and a linked list, detailing functions for enqueueing, dequeueing, and displaying elements in the queue, as well as inserting, deleting, and searching nodes in the linked list. It includes error handling for full and empty states in the queue and demonstrates various operations on the linked list, such as insertion at the beginning, end, and after a specific node. The main function showcases the usage of these data structures and their respective operations.

Uploaded by

JASKIRAT GOGIA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

---------- Queue implementation in C -----------------

#include <stdio.h>
#define SIZE 5

void enQueue(int);
void deQueue();
void display();

int items[SIZE], front = -1, rear = -1;

int main() {
deQueue(); -------- deQueue() = If
firstly we apply deque then it is not possible on empty queue

enQueue(50); ------- Adding the 5


element in Queue
enQueue(24);
enQueue(38);
enQueue(76);
enQueue(16);

enQueue(); ---- 6th element can't be


added to because the queue is full

display();

deQueue(); ------- deQueue() =


removes element entered firstly i.e. 1

display(); ------- Now we have just


4 elements left in the queue

return 0;
}

void enQueue(int value) {


if (rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (front == -1)
front = 0;
rear++;
items[rear] = value;
printf("\nInserted -> %d", value);
}
}

void deQueue() {
if (front == -1)
printf("\nQueue is Empty!!");
else {
printf("\nDeleted : %d", items[front]);
front++;
if (front > rear)
front = rear = -1;
}
}
void display() { ------- Function to print
the queue
if (rear == -1)
printf("\nQueue is Empty!!!");
else {
int i;
printf("\nQueue elements are:\n");
for (i = front; i <= rear; i++)
printf("%d ", items[i]);
}
printf("\n");
}

___________________________________________________________________________________
_____________________________________________________________________

----------------------- Implementation of LinkedList (Insertion & Deletion)


------------------------------

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;

void printList()
{ ---------
display the list
struct node *p = head;
printf("\n[");

while(p != NULL)
{ --------- start from
the beginning
printf(" %d ",p->data);
p = p->next;
}
printf("]");
}

void insertatbegin(int data){


--------- insertion at the beginning

struct node *lk = (struct node*) malloc(sizeof(struct node));


----- create a link
lk->data = data;
lk->next = head;
---- point it to old first node

head = lk;
----- point first to new first node
}
void insertatend(int data){

struct node *lk = (struct node*) malloc(sizeof(struct node));


----- create a link
lk->data = data;
struct node *linkedlist = head;

while(linkedlist->next != NULL)
------ point it to old first node
linkedlist = linkedlist->next;

linkedlist->next = lk;
------ point first to new first node
}
void insertafternode(struct node *list, int data){
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
lk->next = list->next;
list->next = lk;
}
void deleteatbegin(){
head = head->next;
}
void deleteatend(){
struct node *linkedlist = head;
while (linkedlist->next->next != NULL)
linkedlist = linkedlist->next;
linkedlist->next = NULL;
}
void deletenode(int key){
struct node *temp = head, *prev;
if (temp != NULL && temp->data == key) {
head = temp->next;
return;
}

while (temp != NULL && temp->data != key) {


----- Find the key to be deleted
prev = temp;
temp = temp->next;
}

if (temp == NULL) return;


---- If the key is not present

prev->next = temp->next;
----- Remove the node
}
int searchlist(int key){
struct node *temp = head;
while(temp != NULL) {
if (temp->data == key) {
return 1;
}
temp=temp->next;
}
return 0;
}
void main(){
int k=0;
insertatbegin(12);
insertatbegin(22);
insertatend(30);
insertatend(44);
insertatbegin(50);
insertafternode(head->next->next, 33);
printf("Linked List: ");

printList();
------ print list
deleteatbegin();
deleteatend();
deletenode(12);
printf("\nLinked List after deletion: ");

printList();
insertatbegin(4);
insertatbegin(16);
printf("\nUpdated Linked List: ");
printList();
k = searchlist(16);
if (k == 1)
printf("\nElement is found");
else
printf("\nElement is not present in the list");
}

You might also like