0% found this document useful (0 votes)
81 views7 pages

Lab 2 Continue

The document contains C programs for manipulating linked lists, including creating, displaying, reversing, finding the length, searching for elements, sorting, and removing duplicates. It provides a menu-driven interface for users to interact with the linked list functionalities. Each function is defined to handle specific tasks related to linked list operations.
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)
81 views7 pages

Lab 2 Continue

The document contains C programs for manipulating linked lists, including creating, displaying, reversing, finding the length, searching for elements, sorting, and removing duplicates. It provides a menu-driven interface for users to interact with the linked list functionalities. Each function is defined to handle specific tasks related to linked list operations.
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

Exercise 2

2.2 Develop a program to reverse a linked list iteratively

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

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

void create( );
void display( );
void reverse( );

int main()
{
int choice;
do
{
printf("\nLinked List Menu\n");
printf("\n 1.Create a linked list.\n");
printf("\n 2.Reverse of a linked list.\n");
printf("\n 3.Exit\n");
printf("\nEnter Your Choice:");
scanf("%d", &choice);
switch(choice)
{
case 1:create(); display(); break;
case 2: reverse(); display(); break;
case 3: exit(0); break;
default: printf("Invalid Choice. Please Enter Valid Choice....");
}
}while(choice!=3);
}

void create( )
{
int size;
printf("Enter size of the list: ");
scanf("%d", &size);
head = (struct node *)malloc(sizeof(struct node));
printf("Enter the data of node 1: ");
scanf("%d", &(head->data));
head->next = NULL;

struct node *temp = head;


int i;
for(i=2; i<=size; i++)
{
struct node *newnode = (struct node *)malloc(sizeof(struct node));
printf("Enter the data of node %d: ", i);
scanf("%d", &(newnode->data));
newnode->next = NULL;
temp->next = newnode;
temp = temp->next;
}
}
void display( )
{
struct node *temp = head;
printf("Updated Linked List is:");
while(temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

void reverse( )
{
struct node *Prev, *Current, *Next;
Prev = Next = NULL;
Current = head;
printf("\nReversed Linked List is:");
if(head == NULL)
{
printf("\nList is empty");
}
else
{
while(Current != NULL)
{
Next = Current -> next;
Current -> next = Prev;
Prev = Current;
Current = Next;
}
head = Prev;
}
}

2.3 Solve problems involving linked list traversal and manipulation.


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

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

void create( );
void display( );
void length( );
void searching( );
void sorting() ;
int main( )
{
int choice;
do
{
printf("\n Linked List Menu\n");
printf("\n 1.Create a linked list.\n");
printf("\n 2.Length of a linked list.\n");
printf("\n 3.Searching in a linked list.\n");
printf("\n 4.Sorting in a linked list.\n");
printf("\n 5.Exit\n");
printf("\nEnter Your Choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: create( ); display(); break;
case 2: length( ); break;
case 3: searching( ); break;
case 4: sorting( ); display( ); break;
case 5: exit(0);
default: printf("Invalid Choice. Please Enter Valid Choice....\n");
}
} while(choice != 5);
}

void create( )
{
int size;
printf("Enter size of the list: ");
scanf("%d", &size);
head = (struct node *)malloc(sizeof(struct node));
if (head == NULL)
{
printf("Memory allocation failed!\n");
exit(1);
}
printf("Enter the data of node 1: ");
scanf("%d", &(head->data));
head->next = NULL;

struct node *temp = head;


for(int i = 2; i <= size; i++)
{
struct node *newnode = (struct node *)malloc(sizeof(struct node));
if (newnode == NULL)
{
printf("Memory allocation failed!\n");
exit(1);
}
printf("Enter the data of node %d: ", i);
scanf("%d", &(newnode->data));
newnode->next = NULL;
temp->next = newnode;
temp = temp->next;
}
}
void display( )
{
struct node *temp = head;
printf("Updated Linked List is: ");
while(temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}

void length( )
{
int count = 0;
struct node *temp = head;
if(temp == NULL)
{
printf("\nList is empty\n");
}
else
{
while(temp != NULL)
{
count = count + 1;
temp = temp->next;
}
printf("The length of the list is: %d\n", count);
}
}

void searching( )
{
struct node *temp = head;
int found = 0, se, pos = 0;
printf("Enter data to be searched: ");
scanf("%d", &se);
if(temp == NULL)
{
printf("\nList is empty\n");
}
else
{
while(temp != NULL)
{
pos = pos + 1;
if(temp->data == se)
{
printf("The element %d found in node-%d in the linked list.\n", se,
pos);
found = 1; break;
}
temp = temp->next;
}
if(found == 0)
{
printf("The element %d not found in the linked list.\n", se);
}
}
}

void sorting( )
{
int temp;
struct node *i, *j;
for(i = head; i != NULL; i = i->next)
{
for(j = i->next; j != NULL; j = j->next)
{
if(i->data > j->data)
{
temp = i->data;
i->data = j->data;
j->data = temp;
}
}
}
}

Exercise 3: Linked List Applications


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

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

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

void create( );
void display( );
void removeDuplicates( );

int main( )
{
int choice;
do
{
printf("\n Linked List Menu\n");
printf("\n 1.Create a linked list.\n");
printf("\n 2. Remove Duplicates in a linked list.\n");
printf("\n 3.Exit\n");
printf("\nEnter Your Choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: create( ); display( ); break;
case 2: removeDuplicates( ); display( ); break;
case 3: exit(0);
default: printf("Invalid Choice. Please Enter Valid Choice....\n");
}
} while(choice != 3);
}

void create( )
{
int size;
printf("Enter size of the list: ");
scanf("%d", &size);

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


if (head == NULL)
{
printf("Memory allocation failed!\n");
exit(1);
}

printf("Enter the data of node 1: ");


scanf("%d", &(head->data));
head->next = NULL;

struct node *temp = head;


for(int i = 2; i <= size; i++)
{
struct node *newnode = (struct node *)malloc(sizeof(struct node));
if (newnode == NULL)
{
printf("Memory allocation failed!\n");
exit(1);
}
printf("Enter the data of node %d: ", i);
scanf("%d", &(newnode->data));
newnode->next = NULL;
temp->next = newnode;
temp = temp->next;
}
}

void display( )
{
struct node *temp = head;
printf("Updated Linked List is: ");
while(temp != NULL)
{
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
void removeDuplicates( )
{
struct node *current = head;
struct node *prev, *temp;

if(current == NULL)
{
printf("\nList is empty, duplicates not found");
return;
}

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


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

while(temp!=NULL)
{
if(current->data == temp->data)
{
prev -> next = temp -> next;
free(temp);
temp = prev -> next;
}
else
{
prev = temp;
temp = temp->next;
}
}
current = current -> next;
}
printf("\nDuplicate nodes deleted successfully\n");
}

You might also like