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

Dsa Asses4

Uploaded by

gawono2945
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)
16 views

Dsa Asses4

Uploaded by

gawono2945
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/ 17

Hardik Chhallani

Reg No.: 22BCE2454

Single Linked List


Aim: Implementation of Single Linked List using
C Program and performing various operations.

Program Code:
#include <stdio.h>
#include <stdlib.h>
struct linked_list {
int data;
struct linked_list *next;
};
typedef struct linked_list node;
node *createNode(int value) {
node *newNode = (node*) malloc(sizeof(node));
if (newNode == NULL) {
printf("Could not create new node\n");
}
newNode->data = value;
newNode->next = NULL;
return newNode;
}
node *insertAtEnd(node *head, int value) {
node *newNode = createNode(value);
if (head == NULL) {
head = newNode;
} else {
node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
return head;
}
node *insertAtFront(node *head, int value) {
node *newNode = createNode(value);
newNode->next = head;
head = newNode;
return head;
}
node *insertAtMiddle(node *head, int value, int position) {
node *newNode = createNode(value);
if (head == NULL || position == 1) {
newNode->next = head;
head = newNode;
} else {
int i = 1;
node *current = head;
while (i < position-1 && current->next != NULL) {
current = current->next;
i++;
}
newNode->next = current->next;
current->next = newNode;
}
return head;
}
node *deleteAtFront(node *head) {
if (head == NULL) {
printf("Error: List is empty\n");
return NULL;
} else {
node *temp = head;
head = head->next;
printf("Deleted element: %d\n", temp->data);
free(temp);
return head;
}
}
node *deleteAtEnd(node *head) {
if (head == NULL) {
printf("Error: List is empty\n");
return NULL;
} else if (head->next == NULL) {
printf("Deleted element: %d\n", head->data);
free(head);
return NULL;
} else {
node *current = head;
while (current->next->next != NULL) {
current = current->next;
}
node *temp = current->next;
printf("Deleted element: %d\n", temp->data);
current->next = NULL;
free(temp);
return head;
}
}
node *deleteAtMiddle(node *head, int position) {
if (head == NULL) {
printf("Error: List is empty\n");
return NULL;
} else if (position == 1) {
node *temp = head;
head = head->next;
printf("Deleted element: %d\n", temp->data);
free(temp);
return head;
} else {
int i = 1;
node *current = head;
while (i < position-1 && current->next != NULL) {
current = current->next;
i++;
}
if (current->next == NULL) {
printf("Error: Invalid position\n");
return head;
}
node *temp = current->next;
printf("Deleted element: %d\n", temp->data);
current->next = temp->next;
free(temp);
return head;
}
}
void printList(node *head) {
if (head == NULL) {
printf("List is empty\n");
} else {
node *current = head;
printf("List: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
}

int main()
{
node *list=NULL;
int choice,element,position;
while(1)
{
printf("1: INSERT AT END\n2. INSERT AT MIDDLE\n3. INSERT AT
FRONT\n4. DELETE AT FRONT\n5. DELETE AT END\n6. DELETE AT
MIDDLE\n7. EXIT\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter Element to Insert: ");
scanf("%d",&element);
list=insertAtEnd(list,element);
break;
case 2:
printf("Enter Element to Insert: ");
scanf("%d",&element);
printf("Enter position: ");
scanf("%d",&position);
list=insertAtMiddle(list,element,position);
break;
case 3:
printf("Enter Element to Insert: ");
scanf("%d",&element);
list=insertAtFront(list,element); break;
case 4:
list=deleteAtFront(list);
break;
case 5:
list=deleteAtEnd(list);
break;
case 6:
printf("Enter position: ");
scanf("%d",&position);
list=deleteAtMiddle(list,position);
break;
case 7: exit(0); break;
default: printf("Wrong Choice Entered!"); break;
}
printList(list);
}
}

Output:
Result:
The above code shows the application of Double
Linked List using C Programming
Double Linked List
Aim: Implementation of Double Linked List using
C Program and performing various operations.

Program code:

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

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

node *createNode(int value) {


node *newNode = (node*) malloc(sizeof(node));
if (newNode == NULL) {
printf("Could not create new node\n");
return NULL;
}
newNode->data = value;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

node *insertAtEnd(node *head, int value) {


node *newNode = createNode(value);
if (head == NULL) {
head = newNode;
} else {
node *current = head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
return head;
}

node *insertAtFront(node *head, int value) {


node *newNode = createNode(value);
if (head == NULL) {
head = newNode;
} else {
head->prev = newNode;
newNode->next = head;
head = newNode;
}
return head;
}

node *insertAtMiddle(node *head, int value, int position) {


node *newNode = createNode(value);
if (head == NULL || position == 1) {
head = insertAtFront(head, value);
} else {
int i = 1;
node *current = head;
while (i < position-1 && current->next != NULL) {
current = current->next;
i++;
}
if (current->next == NULL) {
current->next = newNode;
newNode->prev = current;
} else {
newNode->next = current->next;
current->next->prev = newNode;
current->next = newNode;
newNode->prev = current;
}
}
return head;
}

node *deleteAtFront(node *head) {


if (head == NULL) {
printf("Error: List is empty\n");
return NULL;
} else {
node *temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
printf("Deleted element: %d\n", temp->data);
free(temp);
return head;
}
}

node *deleteAtEnd(node *head) {


if (head == NULL) {
printf("Error: List is empty\n");
return NULL;
} else if (head->next == NULL) {
printf("Deleted element: %d\n", head->data);
free(head);
return NULL;
} else {
node *current = head;
while (current->next->next != NULL) {
current = current->next;
}
node *temp = current->next;
printf("Deleted element: %d\n", temp->data);
free(temp);
current->next = NULL;
return head;
}
}
node *deleteAtMiddle(node *head, int position) {
if (head == NULL) {
printf("Error: List is empty\n");
return NULL;
}
node *temp = head;
int i = 1;
while (i < position && temp != NULL) {
temp = temp->next;
i++;
}
if (temp == NULL) {
printf("Error: Invalid position\n");
return head;
}
if (temp == head) {
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
free(temp);
} else if (temp->next == NULL) {
temp->prev->next = NULL;
free(temp);
} else {
temp->prev->next = temp->next;
temp->next->prev = temp->prev;
free(temp);
}
printf("Deleted element at position %d\n", position);
return head;
}

void printList(node *head) {


if (head == NULL) {
printf("List is empty\n");
} else {
node *current = head;
printf("List: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
}

int main()
{
node *list=NULL;
int choice,element,position;
while(1)
{
printf("1: INSERT AT END\n2. INSERT AT MIDDLE\n3. INSERT AT
FRONT\n4. DELETE AT FRONT\n5. DELETE AT END\n6. DELETE AT
MIDDLE\n7. EXIT\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter Element to Insert: ");
scanf("%d",&element);
list=insertAtEnd(list,element);
break;
case 2:
printf("Enter Element to Insert: ");
scanf("%d",&element);
printf("Enter position: ");
scanf("%d",&position);
list=insertAtMiddle(list,element,position);
break;
case 3:
printf("Enter Element to Insert: ");
scanf("%d",&element);
list=insertAtFront(list,element); break;
case 4:
list=deleteAtFront(list);
break;
case 5:
list=deleteAtEnd(list);
break;
case 6:
printf("Enter position: ");
scanf("%d",&position);
list=deleteAtMiddle(list,position);
break;
case 7: exit(0); break;
default: printf("Wrong Choice Entered!"); break;
}
printList(list);
}
}

Output:
Result:
The above code shows the application of Double
Linked List using C Programming

You might also like