0% found this document useful (0 votes)
9 views34 pages

DS Lab Manual - Docx-1

The document is a lab manual for a Data Structures course, detailing various exercises related to array manipulation, linked lists, stacks, queues, binary search trees, and hashing. Each exercise includes aims, descriptions, and sample C code for implementing different data structure operations. The manual serves as a practical guide for students to learn and apply fundamental data structure concepts through coding exercises.

Uploaded by

masarapusohith
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)
9 views34 pages

DS Lab Manual - Docx-1

The document is a lab manual for a Data Structures course, detailing various exercises related to array manipulation, linked lists, stacks, queues, binary search trees, and hashing. Each exercise includes aims, descriptions, and sample C code for implementing different data structure operations. The manual serves as a practical guide for students to learn and apply fundamental data structure concepts through coding exercises.

Uploaded by

masarapusohith
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

Data Structures (2305203)

Lab Manual
II Sem. CSE B/S
Faculty: Dr S. NageswaraRao
List of Experiments

Exercise 1: Array Manipulation


i) Write a program to reverse an array.
ii) C Programs to implement the Searching Techniques – Linear & Binary Search
iii) C Programs to implement Sorting Techniques – Bubble, Selection and Insertion Sort

Exercise 2: Linked List Implementation


i) Implement a singly linked list and perform insertion and deletion operations.
ii) Develop a program to reverse a linked list iteratively and recursively.
iii) Solve problems involving linked list traversal and manipulation.

Exercise 3: Linked List Applications


i) Create a program to detect and remove duplicates from a linked list.
ii) Implement a linked list to represent polynomials and perform addition.
iii) Implement a double-ended queue (deque) with essential operations.

Exercise 4: Double Linked List Implementation


i) Implement a doubly linked list and perform various operations to understand its properties
and applications.
ii) Implement a circular linked list and perform insertion, deletion, and traversal.

Exercise 5: Stack Operations


i) Implement a stack using arrays and linked lists.
ii) Write a program to evaluate a postfix expression using a stack.
iii) Implement a program to check for balanced parentheses using a stack.

Exercise 6: Queue Operations


i) Implement a queue using arrays and linked lists.
ii) Develop a program to simulate a simple printer queue system.
iii) Solve problems involving circular queues.

Exercise 7: Stack and Queue Applications


i) Use a stack to evaluate an infix expression and convert it to postfix.
ii) Create a program to determine whether a given string is a palindrome or not.
iii) Implement a stack or queue to perform comparison and check for symmetry.

Exercise 8: Binary Search Tree


i) Implementing a BST using Linked List.
ii) Traversing of BST.

Exercise 9: Hashing
i) Implement a hash table with collision resolution techniques.
ii) Write a program to implement a simple cache using hashing.
Exercise 1: Array Manipulation

1.1 Aim: Write a program to reverse an array.


Description: This program reads elements into an array and displays them in reverse order.​
It reverses the array by printing elements from the last index to the first using a loop.

Code:

#include <stdio.h>

int main() {
int arr[100], n, i;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter the elements:\n");


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Reversed array:\n");
for (i = n - 1; i >= 0; i--)
{
printf("%d ", arr[i]);
}

return 0;
}

1.2

Aim: C Programs to implement the Searching Techniques – Linear & Binary Search
Description: This program uses separate functions to perform Linear Search and Binary Search on an
array.​
Linear Search works for any array, while Binary Search requires the array to be sorted.

#include <stdio.h>

/* Function for Linear Search */


int linearSearch(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key)
return i; // element found
}
return -1; // element not found
}

/* Function for Binary Search (array must be sorted) */


int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1, mid;

while (low <= high) {


mid = (low + high) / 2;

if (arr[mid] == key)
return mid; // element found
else if (arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
return -1; // element not found
}

int main() {
int arr[100], n, key, choice, result;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Enter element to search: ");


scanf("%d", &key);

printf("\nChoose Searching Technique:\n");


printf("1. Linear Search\n");
printf("2. Binary Search\n");
printf("Enter your choice: ");
scanf("%d", &choice);

if (choice == 1) {
result = linearSearch(arr, n, key);

} else if (choice == 2) {
result = binarySearch(arr, n, key);

} else {
printf("Invalid choice");
}
if (result != -1)
printf("Element found at position %d ", result + 1);
else
printf("Element not found");

return 0;
}

1.3

Aim: C Programs to implement Sorting Techniques – Bubble, Selection and Insertion Sort

Description:
Bubble Sort:​
Repeatedly compares adjacent elements and swaps them if they are in the wrong order.​
Largest elements move to the end of the array in each pass.
Selection Sort:​
Selects the smallest element from the unsorted part and places it at the beginning.​
Reduces the number of swaps compared to bubble sort.
Insertion Sort:​
Inserts each element into its correct position in the sorted part of the array.​
Efficient for small datasets and nearly sorted arrays.

Code:
#include <stdio.h>

void bubbleSort(int a[], int n) {


for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (a[j] > a[j + 1]) {
int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t;
}
}

void selectionSort(int a[], int n) {


for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++)
if (a[j] < a[min]) min = j;
int t = a[i]; a[i] = a[min]; a[min] = t;
}
}

void insertionSort(int a[], int n) {


for (int i = 1; i < n; i++) {
int key = a[i], j = i - 1;
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
}
}

void display(int a[], int n) {


for (int i = 0; i < n; i++) printf("%d ", a[i]);
}

int main() {
int a[100], n, ch;
​ printf("enter the number of elememts\n");
scanf("%d", &n);
printf("enter the elememts\n");
for (int i = 0; i < n; i++) scanf("%d", &a[i]);
printf("[Link] sort 2. selection sort 3. insertion sort\n");
printf("enter your choice\n");
scanf("%d", &ch);

if (ch == 1) bubbleSort(a, n);


else if (ch == 2) selectionSort(a, n);
else if (ch == 3) insertionSort(a, n);
else return 0;

display(a, n);
return 0;
}

Exercise 2: Linked List Implementation


i) Implement a singly linked list and perform insertion and deletion operations.
ii) Develop a program to reverse a linked list iteratively and recursively.
iii) Solve problems involving linked list traversal and manipulation.

i) Implement a singly linked list and perform insertion and deletion operations.

description: A singly linked list is a dynamic linear data structure where each node contains
data and a pointer to the next node. In this program, insertion and deletion operations are
performed at different positions to efficiently manage and manipulate the linked list elements.

code:

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

// Define Node structure


struct Node {
int data;
struct Node* next;
};

// Global head pointer


struct Node* head = NULL;

// Insert at beginning
void insertAtBeginning(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = head;
head = newNode;
}

// Insert at end
void insertAtEnd(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;

if (head == NULL) {
head = newNode;
return;
}

struct Node* temp = head;


while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}

// Insert at specific position (1-based index)


void insertAtPosition(int data, int position) {
if (position == 1) {
insertAtBeginning(data);
return;
}

struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));


newNode->data = data;

struct Node* temp = head;


for (int i = 1; i < position - 1 && temp != NULL; i++) {
temp = temp->next;
}

if (temp == NULL) {
printf("Position out of range\n");
return;
}

newNode->next = temp->next;
temp->next = newNode;
}

// Delete from beginning


void deleteFromBeginning() {
if (head == NULL) {
printf("List is empty\n");
return;
}

struct Node* temp = head;


head = head->next;
free(temp);
}

// Delete from end


void deleteFromEnd() {
if (head == NULL) {
printf("List is empty\n");
return;
}

if (head->next == NULL) {
free(head);
head = NULL;
return;
}

struct Node* temp = head;


while (temp->next->next != NULL) {
temp = temp->next;
}

free(temp->next);
temp->next = NULL;
}
// Delete by value
void deleteByValue(int value) {
if (head == NULL) {
printf("List is empty\n");
return;
}

if (head->data == value) {
struct Node* temp = head;
head = head->next;
free(temp);
return;
}

struct Node* temp = head;


while (temp->next != NULL && temp->next->data != value) {
temp = temp->next;
}

if (temp->next == NULL) {
printf("Value not found\n");
return;
}

struct Node* nodeToDelete = temp->next;


temp->next = nodeToDelete->next;
free(nodeToDelete);
}

// Display list
void display() {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// Main function (Menu-driven)


int main() {
int choice, data, position;

while (1) {
printf("\n--- Singly Linked List ---\n");
printf("1. Insert at Beginning\n");
printf("2. Insert at End\n");
printf("3. Insert at Position\n");
printf("4. Delete from Beginning\n");
printf("5. Delete from End\n");
printf("6. Delete by Value\n");
printf("7. Display\n");
printf("8. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Enter value: ");
scanf("%d", &data);
insertAtBeginning(data);
break;

case 2:
printf("Enter value: ");
scanf("%d", &data);
insertAtEnd(data);
break;

case 3:
printf("Enter value and position: ");
scanf("%d %d", &data, &position);
insertAtPosition(data, position);
break;

case 4:
deleteFromBeginning();
break;

case 5:
deleteFromEnd();
break;

case 6:
printf("Enter value to delete: ");
scanf("%d", &data);
deleteByValue(data);
break;

case 7:
display();
break;

case 8:
exit(0);

default:
printf("Invalid choice\n");
}
}

return 0;
}

ii) Develop a program to reverse a linked list iteratively and recursively.

description: This program reverses a singly linked list using both iterative and recursive
approaches to demonstrate different pointer manipulation techniques.

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

// Define node structure


struct Node {
int data;
struct Node* next;
};

struct Node* head = NULL;

// Insert at end
void insert(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;

if (head == NULL) {
head = newNode;
return;
}
struct Node* temp = head;
while (temp->next != NULL)
temp = temp->next;

temp->next = newNode;
}

// Display list
void display() {
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

// ?? Reverse Iteratively
void reverseIterative() {
struct Node *prev = NULL, *current = head, *next = NULL;

while (current != NULL) {


next = current->next; // Store next node
current->next = prev; // Reverse pointer
prev = current; // Move prev forward
current = next; // Move current forward
}

head = prev; // Update head


}

// ?? Reverse Recursively
struct Node* reverseRecursive(struct Node* node) {
if (node == NULL || node->next == NULL)
return node;

struct Node* newHead = reverseRecursive(node->next);

node->next->next = node;
node->next = NULL;

return newHead;
}
int main() {
insert(10);
insert(20);
insert(30);
insert(40);

printf("Original List:\n");
display();

// Iterative Reverse
reverseIterative();
printf("\nAfter Iterative Reverse:\n");
display();

// Recursive Reverse
head = reverseRecursive(head);
printf("\nAfter Recursive Reverse:\n");
display();

return 0;
}

iii) Solve problems involving linked list traversal and manipulation.

Description: This program creates a singly linked list by inserting elements at the end and
performs basic traversal operations such as display, counting nodes, and searching for an
[Link] also includes a function to remove duplicate elements from a sorted linked list
using pointer manipulation and dynamic memory management.

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

/* Structure definition of Node */


struct Node {
int data; // Data part
struct Node* next; // Pointer to next node
};

/* Function to insert a node at the end */


void insert(struct Node** head, int value) {
// Allocate memory for new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

// If list is empty, new node becomes head


if (*head == NULL) {
*head = newNode;
return;
}

// Traverse to last node


struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}

// Attach new node at end


temp->next = newNode;
}

/* Function to display linked list */


void display(struct Node* head) {
struct Node* temp = head;

printf("Linked List: ");


while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

/* Function to count total number of nodes */


int countNodes(struct Node* head) {
int count = 0;

// Traverse entire list


while (head != NULL) {
count++;
head = head->next;
}
return count;
}

/* Function to search an element */


int search(struct Node* head, int key) {

while (head != NULL) {


if (head->data == key)
return 1; // Element found
head = head->next;
}

return 0; // Element not found


}

/* Main function */
int main() {

struct Node* head = NULL;

/* Creating sorted linked list using insert function */


insert(&head, 10);
insert(&head, 20);
insert(&head, 20);
insert(&head, 30);
insert(&head, 40);
insert(&head, 40);

int choice, value;

while (1) {
printf("\n--- Linked List Operations ---\n");
printf("1. Display List\n");
printf("2. Count Nodes\n");
printf("3. Search Element\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
display(head);
break;

case 2:
printf("Total Nodes = %d\n", countNodes(head));
break;

case 3:
printf("Enter value to search: ");
scanf("%d", &value);

if (search(head, value))
printf("Element Found\n");
else
printf("Element Not Found\n");
break;

case 4:
printf("Exiting Program...\n");
exit(0);

default:
printf("Invalid choice\n");
}
}

return 0;
}
Exercise 3: Linked List Applications
i) Create a program to detect and remove duplicates from a linked list.
Description: This program creates a singly linked list and checks for duplicate elements by
comparing each node with the remaining nodes in the list.

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

struct node
{
int data;
struct node *next;
};

struct node *head = NULL;

// Function to insert node at end


void insert(int data)
{
struct node *newnode, *temp;

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


newnode->data = data;
newnode->next = NULL;
if(head == NULL)
{
head = newnode;
}
else
{
temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
}
}

// Function to remove duplicates


void remove_duplicates()
{
struct node *temp, *ptr, *prev;

temp = head;

while(temp != NULL && temp->next != NULL)


{
prev = temp;
ptr = temp->next;

while(ptr != NULL)
{
if(temp->data == ptr->data)
{
prev->next = ptr->next;
free(ptr);
ptr = prev->next;
}
else
{
prev = ptr;
ptr = ptr->next;
}
}
temp = temp->next;
}
}

// Function to display list


void display()
{
struct node *temp = head;

while(temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main()
{
insert(10);
insert(20);
insert(30);
insert(20);
insert(40);
insert(10);

printf("Original List:\n");
display();

remove_duplicates();

printf("List after removing duplicates:\n");


display();

return 0;
}

ii) Implement a linked list to represent polynomials and perform addition.


Description:This program represents a polynomial using a singly linked list, where each node
stores the coefficient and exponent of a [Link] linked lists are created to store the terms of
two different [Link] program performs polynomial addition by comparing
exponents of corresponding terms and storing the result in another linked list.

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

struct node
{
int coeff;
int exp;
struct node *next;
};

// Function to create a polynomial


struct node* create()
{
struct node *head=NULL, *temp=NULL, *newnode;
int n,i;

printf("Enter number of terms: ");


scanf("%d",&n);

for(i=0;i<n;i++)
{
newnode=(struct node*)malloc(sizeof(struct node));

printf("Enter coefficient and exponent: ");


scanf("%d%d",&newnode->coeff,&newnode->exp);

newnode->next=NULL;

if(head==NULL)
{
head=newnode;
temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}
return head;
}

// Function to add two polynomials


struct node* add(struct node *p1, struct node *p2)
{
struct node *result=NULL, *temp=NULL, *newnode;

while(p1!=NULL && p2!=NULL)


{
newnode=(struct node*)malloc(sizeof(struct node));
newnode->next=NULL;

if(p1->exp == p2->exp)
{
newnode->coeff = p1->coeff + p2->coeff;
newnode->exp = p1->exp;
p1=p1->next;
p2=p2->next;
}
else if(p1->exp > p2->exp)
{
newnode->coeff = p1->coeff;
newnode->exp = p1->exp;
p1=p1->next;
}
else
{
newnode->coeff = p2->coeff;
newnode->exp = p2->exp;
p2=p2->next;
}

if(result==NULL)
{
result=newnode;
temp=newnode;
}
else
{
temp->next=newnode;
temp=newnode;
}
}

while(p1!=NULL)
{
newnode=(struct node*)malloc(sizeof(struct node));
newnode->coeff=p1->coeff;
newnode->exp=p1->exp;
newnode->next=NULL;

temp->next=newnode;
temp=newnode;
p1=p1->next;
}

while(p2!=NULL)
{
newnode=(struct node*)malloc(sizeof(struct node));
newnode->coeff=p2->coeff;
newnode->exp=p2->exp;
newnode->next=NULL;

temp->next=newnode;
temp=newnode;
p2=p2->next;
}

return result;
}

// Function to display polynomial


void display(struct node *head)
{
while(head!=NULL)
{
printf("%dx^%d",head->coeff,head->exp);
if(head->next!=NULL)
printf(" + ");
head=head->next;
}
printf("\n");
}

int main()
{
struct node *poly1,*poly2,*poly3;

printf("Enter first polynomial\n");


poly1=create();

printf("Enter second polynomial\n");


poly2=create();

printf("First Polynomial: ");


display(poly1);

printf("Second Polynomial: ");


display(poly2);

poly3=add(poly1,poly2);

printf("Resultant Polynomial after addition: ");


display(poly3);

return 0;
}
Exercise 4: Double Linked List Implementation
i) Implement a doubly linked list and perform various operations to understand its properties
and applications.

Description: This program implements a Doubly Linked List, where each node contains data
along with pointers to the previous and next [Link] program performs insertion operations
such as inserting nodes at the beginning or end of the [Link] also performs deletion operations
by removing nodes from the beginning or end and updates the links accordingly.

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

struct node
{
int data;
struct node *prev;
struct node *next;
};

struct node *head = NULL;

// Insert at beginning
void insert_begin()
{
struct node *newnode;

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


printf("Enter data: ");
scanf("%d",&newnode->data);

newnode->prev = NULL;
newnode->next = head;

if(head != NULL)
head->prev = newnode;

head = newnode;
}

// Insert at end
void insert_end()
{
struct node *newnode,*temp;

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


printf("Enter data: ");
scanf("%d",&newnode->data);

newnode->next = NULL;

if(head == NULL)
{
newnode->prev = NULL;
head = newnode;
}
else
{
temp = head;
while(temp->next != NULL)
temp = temp->next;

temp->next = newnode;
newnode->prev = temp;
}
}
// Insert at specified position
void insert_position()
{
struct node *newnode,*temp;
int pos,i;

printf("Enter position: ");


scanf("%d",&pos);

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


printf("Enter data: ");
scanf("%d",&newnode->data);

temp = head;

for(i=1;i<pos-1;i++)
temp = temp->next;

newnode->next = temp->next;
newnode->prev = temp;

if(temp->next != NULL)
temp->next->prev = newnode;

temp->next = newnode;
}

// Delete at beginning
void delete_begin()
{
struct node *temp;

if(head == NULL)
{
printf("List empty\n");
return;
}

temp = head;
head = head->next;

if(head != NULL)
head->prev = NULL;

free(temp);
}

// Delete at end
void delete_end()
{
struct node *temp;

if(head == NULL)
{
printf("List empty\n");
return;
}

temp = head;

if(temp->next == NULL)
{
head = NULL;
free(temp);
return;
}

while(temp->next != NULL)
temp = temp->next;

temp->prev->next = NULL;
free(temp);
}

// Delete at specified position


void delete_position()
{
struct node *temp;
int pos,i;

printf("Enter position: ");


scanf("%d",&pos);

temp = head;
for(i=1;i<pos;i++)
temp = temp->next;

if(temp->prev != NULL)
temp->prev->next = temp->next;

if(temp->next != NULL)
temp->next->prev = temp->prev;

free(temp);
}

// Display list
void display()
{
struct node *temp = head;

if(head == NULL)
{
printf("List empty\n");
return;
}

printf("List elements: ");


while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}

int main()
{
int choice;

while(1)
{
printf("\[Link] Beginning");
printf("\[Link] End");
printf("\[Link] Position");
printf("\[Link] Beginning");
printf("\[Link] End");
printf("\[Link] Position");
printf("\[Link]");
printf("\[Link]");
printf("\nEnter choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1: insert_begin(); break;
case 2: insert_end(); break;
case 3: insert_position(); break;
case 4: delete_begin(); break;
case 5: delete_end(); break;
case 6: delete_position(); break;
case 7: display(); break;
case 8: exit(0);
default: printf("Invalid choice\n");
}
}

return 0;
}

ii) Implement a circular linked list and perform insertion, deletion, and traversal.
Description: This program implements a circular singly linked list, where the last node points
back to the head node instead of [Link] performs insertion and deletion operations at the
beginning, end, and specified position, and displays the elements of the list.

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

struct node
{
int data;
struct node *prev;
struct node *next;
};

struct node *head = NULL;


// Insert at beginning
void insert_begin()
{
struct node *newnode;

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


printf("Enter data: ");
scanf("%d",&newnode->data);

newnode->prev = NULL;
newnode->next = head;

if(head != NULL)
head->prev = newnode;

head = newnode;
}

// Insert at end
void insert_end()
{
struct node *newnode,*temp;

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


printf("Enter data: ");
scanf("%d",&newnode->data);

newnode->next = NULL;

if(head == NULL)
{
newnode->prev = NULL;
head = newnode;
}
else
{
temp = head;
while(temp->next != NULL)
temp = temp->next;

temp->next = newnode;
newnode->prev = temp;
}
}
// Insert at specified position
void insert_position()
{
struct node *newnode,*temp;
int pos,i;

printf("Enter position: ");


scanf("%d",&pos);

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


printf("Enter data: ");
scanf("%d",&newnode->data);

temp = head;

for(i=1;i<pos-1;i++)
temp = temp->next;

newnode->next = temp->next;
newnode->prev = temp;

if(temp->next != NULL)
temp->next->prev = newnode;

temp->next = newnode;
}

// Delete at beginning
void delete_begin()
{
struct node *temp;

if(head == NULL)
{
printf("List empty\n");
return;
}

temp = head;
head = head->next;

if(head != NULL)
head->prev = NULL;

free(temp);
}

// Delete at end
void delete_end()
{
struct node *temp;

if(head == NULL)
{
printf("List empty\n");
return;
}

temp = head;

if(temp->next == NULL)
{
head = NULL;
free(temp);
return;
}

while(temp->next != NULL)
temp = temp->next;

temp->prev->next = NULL;
free(temp);
}

// Delete at specified position


void delete_position()
{
struct node *temp;
int pos,i;

printf("Enter position: ");


scanf("%d",&pos);

temp = head;

for(i=1;i<pos;i++)
temp = temp->next;

if(temp->prev != NULL)
temp->prev->next = temp->next;
if(temp->next != NULL)
temp->next->prev = temp->prev;

free(temp);
}

// Display list
void display()
{
struct node *temp = head;

if(head == NULL)
{
printf("List empty\n");
return;
}

printf("List elements: ");


while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}

int main()
{
int choice;

while(1)
{
printf("\[Link] Beginning");
printf("\[Link] End");
printf("\[Link] Position");
printf("\[Link] Beginning");
printf("\[Link] End");
printf("\[Link] Position");
printf("\[Link]");
printf("\[Link]");
printf("\nEnter choice: ");
scanf("%d",&choice);

switch(choice)
{
case 1: insert_begin(); break;
case 2: insert_end(); break;
case 3: insert_position(); break;
case 4: delete_begin(); break;
case 5: delete_end(); break;
case 6: delete_position(); break;
case 7: display(); break;
case 8: exit(0);
default: printf("Invalid choice\n");
}
}

return 0;
}

You might also like