0% found this document useful (0 votes)
7 views13 pages

DS Lab Programs.docx

The document contains multiple C programs for managing linked lists and queues, including operations like insertion, deletion, display, and searching. It covers both standard and ordered linked lists, as well as circular queues, providing menu-driven interfaces for user interaction. Each program includes function definitions and a main function to execute the desired operations based on user input.

Uploaded by

sjusyedsuheb95
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)
7 views13 pages

DS Lab Programs.docx

The document contains multiple C programs for managing linked lists and queues, including operations like insertion, deletion, display, and searching. It covers both standard and ordered linked lists, as well as circular queues, providing menu-driven interfaces for user interaction. Each program includes function definitions and a main function to execute the desired operations based on user input.

Uploaded by

sjusyedsuheb95
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/ 13

/* Menu driven program to create a linked list in C with the options

1. Insert a number into Linked list


2. Display the contents of the linked list
3. Delete and return the data on the first node of a given number in the linked list
4. Delete and return the data on the last node of a given number in the linked list
5. Return the maximum number in the list.
6. EXIT */

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

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

void begin_insert(int);
void display();
void begin_delete();
void last_delete();
int max_no();
void freeList();
int Menu();

void begin_insert(int value) {


struct node* newnode ;
newnode = (struct node*)malloc(sizeof(struct node));
if (newnode == NULL) {
printf("Memory allocation failed!\n");
return;
}
newnode->data = value;
newnode->next = head;
head = newnode;
printf("%d inserted into the linked list.\n", value);
}

void display() {
if (head == NULL) {
printf("The linked list is empty.\n");
return;
}
struct node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}

// Delete from beginning


void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}

// Delete from Last


void last_delete()
{
struct node *ptr,*ptr1;
if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL) // only one node in the list
{
head = NULL;
free(head);
printf("\nOnly node of the list deleted ...\n");
}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}

int max_no() {
if (head == NULL) {
printf("The linked list is empty.\n");
return -1;
}
struct node* current = head;
int max = current->data;
current = current->next;
while (current != NULL) {
if (current->data > max)
max = current->data;
current = current->next;
}
return max;
}

void freeList() {
struct node *current , *nextnode;
current = head;
while (current != NULL) {
nextnode = current->next;
free(current);
current = nextnode;
}
head = NULL;
}

int Menu( )
{
int ch;
printf("\n Menu \n");
printf("1. Insert a number in the beginning of the linked list\n");
printf("2. Display the contents of the linked list\n");
printf("3. Delete from Beginning \n”);
printf("4. Delete from Last \n");
printf("5. Maximum number in the list. \n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
return ch;
}

int main() {
int choice, value, key;
while (1) {
choice = Menu();
switch (choice) {
case 1:
printf("Enter the number to insert: ");
scanf("%d", &value);
begin_insert(value);
break;
case 2:
printf("Contents of the linked list: ");
display();
break;
case 3:
begin_delete();
break;
case 4:
last_delete();
break;
case 5:
printf("Maximun Value in the list is %d ",max_no() );
break;
case 6:
freeList();
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice! Please try again.\n");
}
}
return 1;
}

*************************************************
/* Menu driven program to create an ORDERED LINKED LIST in C with the
options:
1. Insert a number into a LINKED LIST
2. DISPLAY the contents of the linked list
3. Delete a given number in the linked list
4. Search for a given number in the list
5. Return the number of nodes/data items in the list
6. EXIT */
#include <stdio.h>
#include <stdlib.h>

// Define a node in the linked list


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

// Function prototypes
void insertOrdered(int);
void displayList();
void deleteNode(int);
int searchNode(int key);
int countNodes();
int Menu();
void freeList();

// Function to insert a number in an ordered List


void insertOrdered(int num) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = num;
newNode->next = NULL;
// Insert at the beginning or when the list is empty
if ( (head == NULL ) || (head->data >= num) ){
newNode->next = head;
head = newNode;
}
// Traverse the list to find the insertion point
else{
struct Node* current = head;
while (current->next != NULL && current->next->data < num) {
current = current->next;
}
// Insert the new node
newNode->next = current->next;
current->next = newNode;
}
printf("New List after Insertion \n");
displayList();
}
// Function to display the linked list
void displayList() {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* current = head;
while (current != NULL) {
printf("%d -> ", current->data);
current = current->next;
}
printf("NULL\n");
}

// Function to delete a node with a given key


void deleteNode(int key) {
if (head == NULL) {
printf("List is empty. Nothing to delete.\n");
}
struct Node* temp;
if (head->data == key) {
temp = head;
head = head->next;
free(temp);
printf("Deleted %d from the list.\n", key);
}
else
{
struct Node* current = head;
while (current->next != NULL && current->next->data != key) {
current = current->next;
}
if (current->next == NULL) {
printf("Number %d not found in the list.\n", key);
} else {
temp = current->next;
current->next = current->next->next;
free(temp);
printf("Deleted %d from the list.\n", key);
}
}
}

// Function to search for a number in the list


int searchNode(int key) {
struct Node* current = head;
while (current != NULL) {
if (current->data == key)
return 1;
current = current->next;
}
return 0;
}

// Function to count the number of nodes in the list


int countNodes() {
int count = 0;
struct Node* current = head;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}

void freeList() {
struct Node *current, *nextnode;
current = head;
while (current != NULL) {
nextnode = current->next;
free(current);
current = nextnode;
}
head = NULL;
}

int Menu() {
int ch;
printf("\nOrdered Linked List \n");
printf("Menu:\n");
printf("1. Insert a number into the linked list\n");
printf("2. Display the contents of the linked list\n");
printf("3. Delete a given number in the linked list\n");
printf("4. Search for a given number in the list\n");
printf("5. Return the number of nodes/data items in the list\n");
printf("6. Exit\n");
printf("Enter your choice: ");
scanf("%d",&ch);
return ch;
}
// Main function
int main() {
int choice, data, key, result;

while (1) {
choice = Menu();
switch (choice) {
case 1:
printf("Enter the number to insert: ");
scanf("%d", &data);
insertOrdered(data);
break;
case 2:
printf("Contents of the linked list: \n");
displayList();
break;
case 3:
printf("Enter the number to delete: ");
scanf("%d", &key);
deleteNode(key);
break;
case 4:
printf("Enter the number to search: ");
scanf("%d", &key);
result = searchNode(key);
if (result)
printf("Number %d found in the list.\n", key);
else
printf("Number %d not found in the list.\n", key);
break;
case 5:
result = countNodes();
printf("Number of nodes in the list: %d\n", result);
break;
case 6:
freeList();
printf("Exiting the program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
/* Menu driven program to implement Q using Linked List
1. Insert a number ( ENQUEUE)
2. Delete a number (DEQUEUE)
3. Display the contents of the Queue.
4. EXIT */

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front=NULL, *rear=NULL;

void insert(int);
int delete();
void display();

void main ()
{
int choice, item;
while(1)
{
printf("\n*************************Main Menu*****************************\n");
printf("\n=================================================================\n");
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the value to be inserted : ");
scanf("%d",&item);
insert(item);
break;
case 2:
printf("Deleted %d from the Q", delete() );
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}

void insert(int item)


{
struct node *newnode;
newnode = (struct node *) malloc (sizeof(struct node));
if(newnode == NULL)
{
printf("\nOVERFLOW\n");
return;
}
else
{
newnode -> data = item;
if(front == NULL)
{
front = newnode;
rear = newnode;
front -> next = NULL;
rear -> next = NULL;
}
else
{
rear -> next = newnode;
rear = newnode;
rear->next = NULL;
}
}
}
int delete ()
{
int item;
struct node *ptr;
if(front == NULL)
{
printf("\nUNDERFLOW\n");
return 0;
}
else
{
ptr = front;
front = front -> next;
item = ptr->data;
free(ptr);
return item;
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{
printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("%d\n",ptr -> data);
ptr = ptr -> next;
}
}
}

/* Menu drive program for Circular Q using Linked list


1. Insert a number ( ENQUEUE)
2. Delete a number (DEQUEUE)
3. Display the contents of the Queue.
4. EXIT */

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node *front = NULL, *rear = NULL;

void enqueue(int item) {


struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = item;
if (front == NULL) {
front = newNode;
rear = newNode;
rear->next = front;
} else {
rear->next = newNode;
rear = newNode;
rear->next = front;
}
printf("%d enqueued successfully.\n", item);
}

int dequeue() {
if (front == NULL) {
printf("Queue is empty.\n");
return 0;
}
int item = front->data;
struct Node* temp = front;
if (front == rear) {
front = NULL;
rear = NULL;
} else {
front = front->next;
rear->next = front;
}
free(temp);
return item;
}

void display() {
if (front == NULL) {
printf("Queue is empty.\n");
return;
}
struct Node* current = front;
printf("The circular queue is: ");
do {
printf("%d ", current->data);
current = current->next;
} while (current != front);
printf("\n");
}

int main() {
int choice, data;
while(1) {
printf("\n");
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter the data to be enqueued: ");
scanf("%d", &data);
enqueue(data);
break;
case 2:
printf("%d dequeued successfully.\n", dequeue());
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
exit(0);
default:
printf("Invalid choice.\n");
break;
}
}
return 0;
}

You might also like