3) linked list program
3) linked list program
AIM: To implement the singly linked list operations to insert, Delete, Count
and Display.
ALGORITHM:
Inserting
Deleting
Display
Program
//Singly Linked List
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
void insert();
void display();
void delete();
int count();
int main() {
int option = 0;
printf("\nOptions\n");
printf("1 : Insert into Linked List \n");
printf("2 : Delete from Linked List \n");
printf("3 : Display Linked List\n");
printf("4 : Count Linked List\n");
printf("Others : Exit()\n");
printf("Enter your option:");
scanf("%d", &option);
switch (option) {
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
count();
break;
default:
break;
}
}
return 0;
}
void insert() {
printf("\nEnter Element for Insert Linked List : \n");
scanf("%d", &data);
if (first_node == 0) {
first_node = temp_node;
} else {
head_node->next = temp_node;
}
temp_node->next = 0;
head_node = temp_node;
fflush(stdin);
}
void delete() {
int countvalue, pos, i = 0;
countvalue = count();
temp_node = first_node;
printf("\nDisplay Linked List : \n");
void display() {
int count = 0;
temp_node = first_node;
printf("\nDisplay Linked List : \n");
while (temp_node != 0) {
printf("# %d # ", temp_node->value);
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
}
int count() {
int count = 0;
temp_node = first_node;
while (temp_node != 0) {
count++;
temp_node = temp_node -> next;
}
printf("\nNo Of Items In Linked List : %d\n", count);
return count;
}
Output
RESULT: Thus the C program is implemented and verified the output for Singly Linked list
to insert Delete and display.
3b). To implement Doubly Linked List.
AIM: Write a C program to implement Doubly linked List
Algorithm: insert
Algorithm: Delete
Program
//Doubly Linked List
#include <stdio.h>
#include <stdlib.h>
void append(int);
void addatbeg(int);
void remov(int);
void display();
int main()
{
int n, ch;
do
{
printf("\n\nOperations on doubly linked list");
printf("\n1. Append \n2. Add at beginning \n3. Remove\n4. Display\n0.
Exit\n");
printf("\nEnter Choice 0-4? : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter number: ");
scanf("%d", &n);
append(n);
break;
case 2:
printf("\nEnter number: ");
scanf("%d", &n);
addatbeg(n);
break;
case 3:
printf("\nEnter number to delete: ");
scanf("%d", &n);
remov(n);
break;
case 4:
display();
break;
}
}while (ch != 0);
}
nptr->prev = temp;
temp->next = nptr;
}
}
if (start != NULL)
start->prev = nptr;
start = nptr;
}
RESULT: Thus the C program is implemented and verified the output for Doubly Linked list
to Append, Add at beginning, Delete and Display.