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

Exercise 3 4

The document contains source code for various linked list applications including detecting and removing duplicates, representing and adding polynomials, implementing a double-ended queue, and creating a doubly linked list with insertion, deletion, and display operations. Additionally, it includes a circular linked list implementation with similar functionalities. Each section provides a main function to demonstrate the operations of the respective data structures.

Uploaded by

tpobehara2023
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Exercise 3 4

The document contains source code for various linked list applications including detecting and removing duplicates, representing and adding polynomials, implementing a double-ended queue, and creating a doubly linked list with insertion, deletion, and display operations. Additionally, it includes a circular linked list implementation with similar functionalities. Each section provides a main function to demonstrate the operations of the respective data structures.

Uploaded by

tpobehara2023
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Exercise 3: Linked List Applications

i) Create a program to detect and remove duplicates from a linked list.


SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>

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

void insertion(int pos, int value)


{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;

if (pos < 1) return; // Invalid position

if (pos == 1 || head == NULL)


{ // Insert at the beginning
newNode->next = head;
head = newNode;
}
else
{
struct Node* temp = head;
for (int i = 1; temp && i < pos - 1; i++)
{
temp = temp->next;
}

if (temp == NULL || temp->next == NULL)


{ // Insert at the end
while (temp->next != NULL) temp = temp->next;
temp->next = newNode;
}
else
{ // Insert at the specified position
newNode->next = temp->next;
temp->next = newNode;
}
}
}

void removeDuplicates()
{
struct Node *current = head, *prev, *temp;

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


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

while (temp != NULL)


{
if (current->data == temp->data)
{ // Found duplicate
prev->next = temp->next; // Remove the duplicate
free(temp); // Free memory
temp = prev->next; // Move to the next node
}
else
{
prev = temp;
temp = temp->next;
}
}
current = current->next;
}
}

void display()
{
if (head == NULL)
{
printf("List is empty.\n");
return;
}
struct Node* temp = head;
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main()
{
insertion(1, 10);
insertion(2, 10);
insertion(3, 10);
insertion(4, 20);
insertion(5, 40);
insertion(6, 20);

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

removeDuplicates();
printf("List after removing duplicates:\n");
display();
return 0;
}
ii) Implement a linked list to represent polynomials and perform addition.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>

struct Node
{
int coeff;
int expo;
struct Node* next;
}*head=NULL;

struct Node* insert(struct Node* head, int coeff, int expo)


{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->coeff = coeff;
newNode->expo = expo;
newNode->next = NULL;

if (head == NULL || expo > head->expo)


{ // Insert at beginning
newNode->next = head;
return newNode;
}

struct Node* temp = head;


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

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

return head;
}

// Function to display a polynomial


void display(struct Node* head)
{
if (head == NULL)
{
printf("0\n");
return;
}

struct Node* temp = head;


while (temp != NULL)
{
printf("%dx^%d", temp->coeff, temp->expo);
if (temp->next != NULL)
printf(" + ");
temp = temp->next;
}
printf("\n");
}

// Function to add two polynomials


struct Node* addPolynomials(struct Node* p1, struct Node* p2)
{
struct Node* result = NULL;

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


{
if (p1 == NULL)
{ // Copy remaining terms of p2
result = insert(result, p2->coeff, p2->expo);
p2 = p2->next;
}
else if (p2 == NULL)
{ // Copy remaining terms of p1
result = insert(result, p1->coeff, p1->expo);
p1 = p1->next;
}
else if (p1->expo == p2->expo)
{ // Same exponent, add coefficients
result = insert(result, p1->coeff + p2->coeff, p1->expo);
p1 = p1->next;
p2 = p2->next;
}
else if (p1->expo > p2->expo)
{ // Insert higher exponent first
result = insert(result, p1->coeff, p1->expo);
p1 = p1->next;
}
else
{ // Insert lower exponent first
result = insert(result, p2->coeff, p2->expo);
p2 = p2->next;
}
}

return result;
}

// Main function
int main() {
struct Node* poly1 = NULL;
struct Node* poly2 = NULL;
struct Node* sum = NULL;

// Creating first polynomial: 3x^3 + 5x^2 + 6


poly1 = insert(poly1, 5, 3);
poly1 = insert(poly1, 5, 2);
poly1 = insert(poly1, 6, 0);

// Creating second polynomial: 4x^3 + 2x^1 + 4


poly2 = insert(poly2, 4, 3);
poly2 = insert(poly2, 2, 1);
poly2 = insert(poly2, 4, 0);

// Displaying the polynomials


printf("Polynomial 1: ");
display(poly1);
printf("Polynomial 2: ");
display(poly2);

// Adding the polynomials


sum = addPolynomials(poly1, poly2);

// Displaying the result


printf("Sum: ");
display(sum);

return 0;
}

iii) Implement a double-ended queue (deque) with essential operations.


SOURCE CODE:

LEAVE 3 PAGES WILL DO AFTER UNIT-4


Exercise 4: Double Linked List Implementation
i) Implement a doubly linked list and perform various operations to understand its
properties and applications.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* prev;
struct Node* next;
}* head = NULL;

void insertion(int pos, int value)


{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->prev = NULL;
newNode->next = NULL;
if (pos < 1) return; // Invalid position
if (pos == 1 || head == NULL)
{ // Insert at the beginning or if the list is empty
newNode->next = head;
if (head != NULL)
{
head->prev = newNode;
}
head = newNode;
}
else
{
struct Node* temp = head;
for (int i = 1; temp != NULL && i < pos - 1; i++)
{
temp = temp->next;
}
if (temp == NULL)
{ // If the position exceeds the list length, insert at the end
printf("Position exceeds the list length. Inserting at the end.\n");
while (temp != NULL && temp->next != NULL)
{
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
}
else
{ // Insert the node at the specified position
newNode->next = temp->next;
if (temp->next != NULL)
{
temp->next->prev = newNode;
}
temp->next = newNode;
newNode->prev = temp;
}
}
printf("Node inserted.\n");
}

void deletion(int value)


{
if (head == NULL)
{
printf("List is empty.\n");
return;
}
struct Node* temp = head;
if (head->data == value) // Delete from the beginning
{
head = temp->next;
if (head != NULL)
{
head->prev = NULL;
}
free(temp);
printf("Node deleted.\n");
return;
}
while (temp != NULL && temp->data != value)
{
temp = temp->next;
}
if (temp == NULL)
{
printf("Node with value %d not found.\n", value);
return;
}
if (temp->next != NULL) // Delete from the middle or end
{
temp->next->prev = temp->prev;
}
if (temp->prev != NULL) {
temp->prev->next = temp->next;
}
free(temp);
printf("Node deleted.\n");
}

void display()
{
if (head == NULL)
{
printf("List is empty.\n");
return;
}
struct Node* temp = head;
while (temp != NULL)
{
printf("%d <-> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

int main()
{
int choice, value, pos;
while (1)
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: printf("Enter value and position: ");
scanf("%d %d", &value, &pos);
insertion(pos, value);
break;
case 2: printf("Enter value to delete: ");
scanf("%d", &value);
deletion(value);
break;
case 3: display();
break;
case 4: return 0;
default:printf("Invalid choice! Please try again.\n");
}
}
}
ii) Implement a circular linked list and perform insertion, deletion, and traversal.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node* next;
} *head = NULL;

void insertion(int pos, int value)


{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if (pos < 1) return; // Invalid position
if (head == NULL) // If the list is empty
{
newNode->next = newNode; // Point to itself
head = newNode;
}
else if (pos == 1)
{ // Insert at the beginning
struct Node* temp = head;
while (temp->next != head)
{
temp = temp->next;
}
newNode->next = head;
temp->next = newNode;
head = newNode;
}
else
{ // Insert at the given position
struct Node* temp = head;
for (int i = 1; temp->next != head && i < pos - 1; i++)
{
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
printf("Node inserted.\n");
}

void deletion(int value)


{
if (head == NULL)
{
printf("List is empty.\n");
return;
}
struct Node *temp = head, *prev = NULL;
if (head->data == value)
{
struct Node* last = head;
while (last->next != head)
{
last = last->next;
}
if (head->next == head)
{ // Single node case
free(head);
head = NULL;
}
else
{
last->next = head->next;
free(head);
head = last->next;
}
printf("Node deleted.\n");
return;
}
while (temp->next != head && temp->data != value) // Find the node to delete
{
prev = temp;
temp = temp->next;
}
if (temp->data == value)
{
prev->next = temp->next;
free(temp);
printf("Node deleted.\n");
}
else
{
printf("Node with value %d not found.\n", value);
}
}
void display()
{
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("(Head)\n");
}
int main()
{
int choice, value, pos;
while (1)
{
printf("\n1. Insert\n2. Delete\n3. Display\n4. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: printf("Enter value and position: ");
scanf("%d %d", &value, &pos);
insertion(pos, value);
break;
case 2: printf("Enter value to delete: ");
scanf("%d", &value);
deletion(value);
break;
case 3: display();
break;
case 4: return 0;
default: printf("Invalid choice! Please try again.\n");
}
}
}

You might also like