Professional Documents
Culture Documents
Linked List:
A linked list is a linear data structure consisting of a sequence of nodes, where each node
stores an element and a reference (or pointer) to the next node in the sequence. Linked lists
provide dynamic memory allocation, which allows for efficient insertion and deletion of
elements.
Common Operations:
Insertion: Adding a new node with an element to the linked list. Depending on where you
want to insert, you update the pointers of the neighbouring nodes accordingly.
Deletion: Removing a node from the linked list. Update pointers of neighbouring nodes to
skip the deleted node, and free its memory.
Search: Finding a specific element in the linked list by traversing from the beginning until the
element is found or the end is reached.
Count: count number of nodes in a Linked List by traversing through Linked List.
Artificial Intelligence and Data Science Department
CODE:
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
};
int count(){
int count=0;
while(temp!=NULL){
count++;
temp=temp->next;
return count;
void create(){
int n ;
scanf("%d",&n);
new->next=NULL;
new->data=n;
head = new;
tail = new ;
void insert(){
int choice;
scanf("%d",&choice);
switch(choice){
case 1: insertByValue();
break;
case 2: insertByPosition();
break;
void insertByValue(){
int n;
scanf("%d",&n);
while (Atvalue->data!=n){
Artificial Intelligence and Data Science Department
Atvalue=Atvalue->next;
if (Atvalue==NULL){
return;
int choice;
scanf("%d",&choice);
switch(choice){
case 1: insertAfterValue(Atvalue);
break;
case 2: insertBeforeValue(Atvalue);
break;
int n;
scanf("%d",&n);
new->data=n;
new->next=Atvalue->next;
Atvalue->next=new;
Artificial Intelligence and Data Science Department
if(Atvalue==tail){
tail=new;
int n;
scanf("%d",&n);
if(Atvalue==head){
new->next=head;
head=new;
while (Beforeval->next!=Atvalue){
Beforeval=Beforeval->next;
Beforeval->next=new;
new->next=Atvalue;
new->data=n;
void insertByPosition(){
int n;
scanf("%d",&n);
if(n>count()+1){
printf("INVALID POSITION");
return;
if(n==count()+1){
scanf("%d",&n);
new->next=NULL;
new->data=n;
tail->next=new;
tail=new;
return;
int i=1;
while (i!=n){
Atvalue=Atvalue->next;
i++;
if (Atvalue==NULL){
return;
scanf("%d",&n);
if(Atvalue==head){
new->next=head;
head=new;
while (Beforevalue->next!=Atvalue){
Beforevalue=Beforevalue->next;
Beforevalue->next=new;
new->next=Atvalue;
new->data=n;
void Delete(){
if (head==NULL) {
printf("LIST IS EMPTY");
return;
int choice;
scanf("%d",&choice);
switch(choice){
case 1: DeleteByValue();
Artificial Intelligence and Data Science Department
break;
case 2: DeleteByPosition();
break;
void DeleteByValue(){
int n;
scanf("%d",&n);
while (Atvalue->data!=n){
Atvalue=Atvalue->next;
if (Atvalue==NULL){
return;
if(Atvalue==head){
head=Atvalue->next;
free(Atvalue);
return;
while (Beforeval->next!=Atvalue){
Artificial Intelligence and Data Science Department
Beforeval=Beforeval->next;
Beforeval->next=Atvalue->next;
if (Atvalue==tail){
tail=Beforeval;
free(Atvalue);
void DeleteByPosition(){
int n;
scanf("%d",&n);
int i=1;
while (i!=n){
Atvalue=Atvalue->next;
i++;
if (Atvalue==NULL){
return;
if(Atvalue==head){
head=Atvalue->next;
free(Atvalue);
Artificial Intelligence and Data Science Department
return;
while (Beforeval->next!=Atvalue){
Beforeval=Beforeval->next;
Beforeval->next=Atvalue->next;
if (Atvalue==tail){
tail=Beforeval;
free(Atvalue);
void Search(){
if (head==NULL) {
printf("LIST IS EMPTY");
return;
int n ;
int pos = 1;
scanf("%d",&n);
while(temp->data!=n){
Artificial Intelligence and Data Science Department
temp=temp->next;
pos++;
if (temp==NULL){
return;
void print(){
while(temp!=NULL){
printf("%d ",temp->data);
temp=temp->next;
void main(){
int choice;
do{
scanf("%d",&choice);
switch(choice){
case 1:
Artificial Intelligence and Data Science Department
if (head==NULL){
create();
else if (head!=NULL){
insert();
break;
case 2: Delete();
break;
case 3: print();
break;
case 4: Search();
break;
case 5:
break;
}while(choice!=5);