0% found this document useful (0 votes)
2 views

WEEK-II-Data Structures

The document contains two C programs that implement basic operations for circular and doubly linked lists, allowing user interaction through keyboard input. The circular linked list program includes functions for inserting, deleting, displaying, and searching nodes, while the doubly linked list program provides similar functionalities with forward and backward display options. Both programs feature a menu-driven interface for user choices.

Uploaded by

gt60022006
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)
2 views

WEEK-II-Data Structures

The document contains two C programs that implement basic operations for circular and doubly linked lists, allowing user interaction through keyboard input. The circular linked list program includes functions for inserting, deleting, displaying, and searching nodes, while the doubly linked list program provides similar functionalities with forward and backward display options. Both programs feature a menu-driven interface for user choices.

Uploaded by

gt60022006
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/ 10

WEEK-2

1) /*Write a C program that performs all the basic operations of a


circular linked list, and it interacts with the user via keyboard input.*/

SourceCode:

#include <stdio.h>
#include <stdlib.h>
// Define a node structure
struct Node {
int data;
struct Node* next;
};
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = newNode; // Point to itself in circular linked list
return newNode;
}
// Function to insert a node at the end of the circular linked list
void insertEnd(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode; // If the list is empty, make the new node the head
} else {
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head; // Make it circular
}
printf("Node with value %d inserted at the end.\n", value);
}
// Function to insert a node at the beginning of the circular linked list
void insertBeginning(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
*head = newNode; // Make the new node the head
}
printf("Node with value %d inserted at the beginning.\n", value);
}
// Function to delete a node with a given value
void deleteNode(struct Node** head, int value) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct Node *temp = *head, *prev = NULL;
// If head node holds the value to be deleted
if (temp->data == value) {
prev = *head;
while (prev->next != *head) {
prev = prev->next;
}
if (*head == (*head)->next) { // Only one node in the list
free(*head);
*head = NULL;
} else {
prev->next = temp->next;
free(temp);
*head = prev->next; // Update head if necessary
}
printf("Node with value %d deleted.\n", value);
return;
}
// Traverse the list to find the node to be deleted
while (temp->next != *head && temp->data != value) {
prev = temp;
temp = temp->next;
}
// If the value is not found
if (temp->data != value) {
printf("Value %d not found in the list.\n", value);
return;
}
prev->next = temp->next;
free(temp);
printf("Node with value %d deleted.\n", value);
}
// Function to display the circular linked list
void displayList(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
do {
printf("%d -> ", temp->data);
temp = temp->next;
} while (temp != head);
printf("...\n");
}
// Function to search for a value in the circular linked list
int search(struct Node* head, int value) {
if (head == NULL) {
return 0; // List is empty
}
struct Node* temp = head;
do {
if (temp->data == value) {
return 1; // Value found
}
temp = temp->next;
} while (temp != head);
return 0; // Value not found
}
// Function to print the menu
void printMenu() {
printf("\nCircular Linked List Operations:\n");
printf("1. Insert node at the beginning\n");
printf("2. Insert node at the end\n");
printf("3. Delete node\n");
printf("4. Display the list\n");
printf("5. Search for a node\n");
printf("6. Exit\n");
}
int main() {
struct Node* head = NULL;
int choice, value;
while (1) {
printMenu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at the beginning: ");
scanf("%d", &value);
insertBeginning(&head, value);
break;
case 2:
printf("Enter value to insert at the end: ");
scanf("%d", &value);
insertEnd(&head, value);
break;
case 3:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteNode(&head, value);
break;
case 4:
printf("Circular Linked List: ");
displayList(head);
break;
case 5:
printf("Enter value to search for: ");
scanf("%d", &value);
if (search(head, value)) {
printf("Node with value %d found in the list.\n", value);
} else {
printf("Node with value %d not found in the list.\n", value);
}
break;
case 6:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
2) /* C program that performs all the basic operations of a Double linked
list, and it interacts with the user via keyboard input. */
Source Code:
#include <stdio.h>
#include <stdlib.h>
// Define a node structure for the doubly linked list
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Function to create a new node
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = newNode->prev = NULL;
return newNode;
}
// Function to insert a node at the beginning of the doubly linked list
void insertBeginning(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode; // If the list is empty, make the new node the head
} else {
newNode->next = *head;
(*head)->prev = newNode;
*head = newNode;
}
printf("Node with value %d inserted at the beginning.\n", value);
}
// Function to insert a node at the end of the doubly linked list
void insertEnd(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode; // If the list is empty, make the new node the head
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
printf("Node with value %d inserted at the end.\n", value);
}
// Function to delete a node with a given value
void deleteNode(struct Node** head, int value) {
if (*head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = *head;
// If the node to be deleted is the head node
if (temp->data == value) {
if (temp->next == NULL) { // Only one node in the list
free(temp);
*head = NULL;
} else {
*head = temp->next;
(*head)->prev = NULL;
free(temp);
}
printf("Node with value %d deleted.\n", value);
return;
}
// Traverse the list to find the node to delete
while (temp != NULL && temp->data != value) {
temp = temp->next;
}
// If the value is not found
if (temp == NULL) {
printf("Value %d not found in the list.\n", value);
return;
}
// If the node to be deleted is in the middle or end
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
free(temp);
printf("Node with value %d deleted.\n", value);
}
// Function to display the list from the beginning
void displayListForward(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
printf("Doubly Linked List (Forward): ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
// Function to display the list from the end
void displayListBackward(struct Node* head) {
if (head == NULL) {
printf("List is empty.\n");
return;
}
struct Node* temp = head;
// Go to the last node
while (temp->next != NULL) {
temp = temp->next;
}
printf("Doubly Linked List (Backward): ");
while (temp != NULL) {
printf("%d <-> ", temp->data);
temp = temp->prev;
}
printf("NULL\n");
}
// Function to search for a value in the doubly linked list
int search(struct Node* head, int value) {
struct Node* temp = head;
while (temp != NULL) {
if (temp->data == value) {
return 1; // Value found
}
temp = temp->next;
}
return 0; // Value not found
}
// Function to print the menu of operations
void printMenu() {
printf("\nDoubly Linked List Operations:\n");
printf("1. Insert node at the beginning\n");
printf("2. Insert node at the end\n");
printf("3. Delete node\n");
printf("4. Display the list (Forward)\n");
printf("5. Display the list (Backward)\n");
printf("6. Search for a node\n");
printf("7. Exit\n");
}
int main() {
struct Node* head = NULL;
int choice, value;
while (1) {
printMenu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert at the beginning: ");
scanf("%d", &value);
insertBeginning(&head, value);
break;
case 2:
printf("Enter value to insert at the end: ");
scanf("%d", &value);
insertEnd(&head, value);
break;
case 3:
printf("Enter value to delete: ");
scanf("%d", &value);
deleteNode(&head, value);
break;
case 4:
displayListForward(head);
break;
case 5:
displayListBackward(head);
break;
case 6:
printf("Enter value to search for: ");
scanf("%d", &value);
if (search(head, value)) {
printf("Node with value %d found in the list.\n", value);
} else {
printf("Node with value %d not found in the list.\n", value);
}
break;
case 7:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy