DS Lab Programs.docx
DS Lab Programs.docx
#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 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");
}
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>
// Function prototypes
void insertOrdered(int);
void displayList();
void deleteNode(int);
int searchNode(int key);
int countNodes();
int Menu();
void freeList();
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");
}
}
}
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node *front = NULL, *rear = NULL;
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;
}