SlideShare a Scribd company logo
Implementing Array as an Abstract
Data Type in C Language
#include<stdio.h>
#include<stdlib.h>
struct myArray
{
int total_size;
int used_size;
int *ptr;
};
voidcreateArray(structmyArray*a, inttSize,intuSize){
// (*a).total_size =tSize;
// (*a).used_size=uSize;
// (*a).ptr= (int*)malloc(tSize*sizeof(int));
a->total_size = tSize;
a->used_size =uSize;
a->ptr = (int*)malloc(tSize *sizeof(int));
}
voidshow(structmyArray*a){
for (inti = 0; i < a->used_size;i++)
{
printf("%dn",(a->ptr)[i]);
}
}
voidsetVal(structmyArray*a){
int n;
for (inti = 0; i < a->used_size;i++)
{
printf("Enterelement%d",i);
scanf("%d",&n);
(a->ptr)[i] =n;
}
}
intmain(){
struct myArraymarks;
createArray(&marks,10,2);
printf("We are runningsetVal nown");
setVal(&marks);
printf("We are runningshownown");
show(&marks);
return0;
}
Coding Insertion Operation in Array in
Data Structures in C language
#include<stdio.h>
voiddisplay(intarr[],intn){
// Code forTraversal
for (inti = 0; i < n; i++)
{
printf("%d",arr[i]);
}
printf("n");
}
intindInsertion(intarr[],intsize,intelement,int capacity,intindex){
// code for Insertion
if(size>=capacity){
return-1;
}
for (inti = size-1;i >=index;i--)
{
arr[i+1] = arr[i];
}
arr[index] =element;
return1;
}
intmain(){
int arr[100] = {7, 8, 12, 27, 88};
int size = 5, element=45, index=1;
display(arr,size);
indInsertion(arr,size,element,100,index);
size +=1;
display(arr,size);
return0;
}
Coding Deletion Operation in Array
Using C Language
#include <stdio.h>
voiddisplay(intarr[],intn)
{
// Code forTraversal
for (inti = 0; i < n; i++)
{
printf("%d",arr[i]);
}
printf("n");
}
voidindDeletion(intarr[],intsize,intindex)
{
// code for Deletion
for (inti = index;i < size-1;i++)
{
arr[i] = arr[i + 1];
}
}
intmain()
{
int arr[100] = {7, 8, 12, 27, 88};
int size = 5, element=45, index =0;
display(arr,size);
indDeletion(arr,size,index);
size -= 1;
display(arr,size);
return0;
}
Linear Vs Binary Search + Code in C
Language
#include<stdio.h>
intlinearSearch(intarr[],intsize,intelement){
for (inti = 0; i < size;i++)
{
if(arr[i]==element){
returni;
}
}
return-1;
}
intbinarySearch(intarr[],intsize,intelement){
int low,mid,high;
low= 0;
high= size-1;
// Keepsearchinguntil low<=high
while(low<=high){
mid= (low+ high)/2;
if(arr[mid] ==element){
returnmid;
}
if(arr[mid]<element){
low= mid+1;
}
else{
high= mid -1;
}
}
return-1;
}
intmain(){
// Unsortedarray for linearsearch
// intarr[] = {1,3,5,56,4,3,23,5,4,54634,56,34};
// intsize = sizeof(arr)/sizeof(int);
// Sortedarray for binarysearch
int arr[] = {1,3,5,56,64,73,123,225,444};
int size = sizeof(arr)/sizeof(int);
int element=444;
int searchIndex =binarySearch(arr,size,element);
printf("The element%dwasfoundatindex %d n",element,searchIndex);
return0;
}
Linked List Data Structure: Creation and
Traversal in C Language
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
voidlinkedListTraversal(structNode *ptr)
{
while (ptr!= NULL)
{
printf("Element:%dn",ptr->data);
ptr = ptr->next;
}
}
intmain()
{
struct Node *head;
struct Node *second;
struct Node *third;
struct Node *fourth;
// Allocate memoryfornodesinthe linkedlistinHeap
head= (structNode *)malloc(sizeof(structNode));
second= (structNode *)malloc(sizeof(structNode));
third= (structNode *)malloc(sizeof(structNode));
fourth= (structNode *)malloc(sizeof(structNode));
// Linkfirstand secondnodes
head->data= 7;
head->next=second;
// Linksecondand thirdnodes
second->data= 11;
second->next=third;
// Linkthirdand fourthnodes
third->data= 41;
third->next=fourth;
// Terminate the listatthe thirdnode
fourth->data= 66;
fourth->next=NULL;
linkedListTraversal(head);
return0;
}
Insertion in a Linked List in C Language
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node * next;
};
voidlinkedListTraversal(structNode *ptr)
{
while (ptr!= NULL)
{
printf("Element:%dn",ptr->data);
ptr = ptr->next;
}
}
// Case 1
struct Node * insertAtFirst(structNode *head,intdata){
struct Node * ptr = (structNode *) malloc(sizeof(structNode));
ptr->data = data;
ptr->next= head;
returnptr;
}
// Case 2
struct Node * insertAtIndex(structNode *head,intdata,intindex){
struct Node * ptr = (structNode *) malloc(sizeof(structNode));
struct Node * p = head;
int i = 0;
while (i!=index-1)
{
p = p->next;
i++;
}
ptr->data = data;
ptr->next= p->next;
p->next= ptr;
returnhead;
}
// Case 3
struct Node * insertAtEnd(structNode *head,intdata){
struct Node * ptr = (structNode *) malloc(sizeof(structNode));
ptr->data = data;
struct Node * p = head;
while(p->next!=NULL){
p = p->next;
}
p->next= ptr;
ptr->next= NULL;
returnhead;
}
// Case 4
struct Node * insertAfterNode(structNode *head,structNode *prevNode,intdata){
struct Node * ptr = (structNode *) malloc(sizeof(structNode));
ptr->data = data;
ptr->next= prevNode->next;
prevNode->next=ptr;
returnhead;
}
intmain(){
struct Node *head;
struct Node *second;
struct Node *third;
struct Node *fourth;
// Allocate memoryfornodesinthe linkedlistinHeap
head= (structNode *)malloc(sizeof(structNode));
second= (structNode *)malloc(sizeof(structNode));
third= (structNode *)malloc(sizeof(structNode));
fourth= (structNode *)malloc(sizeof(structNode));
// Linkfirstand secondnodes
head->data= 7;
head->next=second;
// Linksecondand thirdnodes
second->data= 11;
second->next=third;
// Linkthirdand fourthnodes
third->data= 41;
third->next=fourth;
// Terminate the listatthe thirdnode
fourth->data= 66;
fourth->next=NULL;
printf("Linkedlistbefore insertionn");
linkedListTraversal(head);
// head= insertAtFirst(head,56);
// head= insertAtIndex(head,56,1);
// head= insertAtEnd(head,56);
head= insertAfterNode(head,third,45);
printf("nLinkedlistafterinsertionn");
linkedListTraversal(head);
return0;
Delete a Node from Linked List (C Code
For Deletion From Beginning, End,
Specified Position & Key)
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
voidlinkedListTraversal(structNode *ptr)
{
while (ptr!= NULL)
{
printf("Element:%dn",ptr->data);
ptr = ptr->next;
}
}
// Case 1: Deletingthe firstelementfromthe linkedlist
struct Node * deleteFirst(structNode *head){
struct Node * ptr = head;
head= head->next;
free(ptr);
returnhead;
}
// Case 2: Deletingthe elementata givenindex fromthe linkedlist
struct Node * deleteAtIndex(structNode *head,intindex){
struct Node *p = head;
struct Node *q = head->next;
for (inti = 0; i < index-1;i++)
{
p = p->next;
q = q->next;
}
p->next= q->next;
free(q);
returnhead;
}
// Case 3: Deletingthe lastelement
struct Node * deleteAtLast(structNode *head){
struct Node *p = head;
struct Node *q = head->next;
while(q->next!=NULL)
{
p = p->next;
q = q->next;
}
p->next= NULL;
free(q);
returnhead;
}
// Case 4: Deletingthe elementwithagivenvalue fromthe linkedlist
struct Node * deleteAtIndex(structNode *head,intvalue){
struct Node *p = head;
struct Node *q = head->next;
while(q->data!=value &&q->next!=NULL)
{
p = p->next;
q = q->next;
}
if(q->data== value){
p->next=q->next;
free(q);
}
returnhead;
}
intmain()
{
struct Node *head;
struct Node *second;
struct Node *third;
struct Node *fourth;
// Allocate memoryfornodesinthe linkedlistinHeap
head= (structNode *)malloc(sizeof(structNode));
second= (structNode *)malloc(sizeof(structNode));
third= (structNode *)malloc(sizeof(structNode));
fourth= (structNode *)malloc(sizeof(structNode));
// Linkfirstand secondnodes
head->data= 4;
head->next=second;
// Linksecondand thirdnodes
second->data= 3;
second->next=third;
// Linkthirdand fourthnodes
third->data= 8;
third->next=fourth;
// Terminate the listatthe thirdnode
fourth->data= 1;
fourth->next=NULL;
printf("Linkedlistbefore deletionn");
linkedListTraversal(head);
// head= deleteFirst(head);//Fordeletingfirstelementof the linkedlist
// head= deleteAtIndex(head,2);
head= deleteAtLast(head);
printf("Linkedlistafterdeletionn");
linkedListTraversal(head);
return0;
}
Circular Linked Lists: Operations in C
Language
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
voidlinkedListTraversal(structNode *head){
struct Node *ptr = head;
do{
printf("Elementis%dn",ptr->data);
ptr = ptr->next;
}while(ptr!=head);
}
struct Node * insertAtFirst(structNode *head,intdata){
struct Node * ptr = (structNode *) malloc(sizeof(structNode));
ptr->data = data;
struct Node * p = head->next;
while(p->next!=head){
p = p->next;
}
// At thispointppointsto the last node of thiscircularlinkedlist
p->next= ptr;
ptr->next= head;
head= ptr;
returnhead;
}
intmain(){
struct Node *head;
struct Node *second;
struct Node *third;
struct Node *fourth;
// Allocate memoryfornodesinthe linkedlistinHeap
head= (structNode *)malloc(sizeof(structNode));
second= (structNode *)malloc(sizeof(struct Node));
third= (structNode *)malloc(sizeof(structNode));
fourth= (structNode *)malloc(sizeof(structNode));
// Linkfirstand secondnodes
head->data= 4;
head->next=second;
// Linksecondand thirdnodes
second->data= 3;
second->next=third;
// Linkthirdand fourthnodes
third->data= 6;
third->next=fourth;
// Terminate the listatthe thirdnode
fourth->data= 1;
fourth->next=head;
printf("CircularLinkedlistbefore insertionn");
linkedListTraversal(head);
head= insertAtFirst(head,54);
head= insertAtFirst(head,58);
head= insertAtFirst(head,59);
printf("CircularLinkedlistafterinsertionn");
linkedListTraversal(head);
return0;
}
Doubly Linked Lists Explained With
Code in C Language
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
voidlinkedListTraversal(structNode *head){
struct Node *ptr = head;
do{
printf("Elementis%dn", ptr->data);
ptr = ptr->next;
}while(ptr!=head);
}
struct Node * insertAtFirst(structNode *head,intdata){
struct Node * ptr = (structNode *) malloc(sizeof(structNode));
ptr->data = data;
struct Node * p = head->next;
while(p->next!=head){
p = p->next;
}
// At thispointppointsto the last node of thiscircularlinkedlist
p->next= ptr;
ptr->next= head;
head= ptr;
returnhead;
}
intmain(){
struct Node *head;
struct Node *second;
struct Node *third;
struct Node *fourth;
// Allocate memoryfornodesinthe linkedlistinHeap
head= (structNode *)malloc(sizeof(structNode));
second= (structNode *)malloc(sizeof(structNode));
third= (structNode *)malloc(sizeof(structNode));
fourth= (structNode *)malloc(sizeof(structNode));
// Linkfirstand secondnodes
head->data= 4;
head->next=second;
// Linksecondand thirdnodes
second->data= 3;
second->next=third;
// Linkthirdand fourthnodes
third->data= 6;
third->next=fourth;
// Terminate the listatthe thirdnode
fourth->data= 1;
fourth->next=head;
printf("CircularLinkedlistbefore insertionn");
linkedListTraversal(head);
head= insertAtFirst(head,54);
head= insertAtFirst(head,58);
head= insertAtFirst(head,59);
printf("CircularLinkedlistafterinsertionn");
linkedListTraversal(head);
return0;
}

More Related Content

PPTX
Linked list
PPT
17 linkedlist (1)
PPTX
linkedlistforslideshare-210123143943.pptx
PPT
Linkedlist
PPT
Linked list
PPT
Linked list
PPT
Linked list
PPT
Linked list
Linked list
17 linkedlist (1)
linkedlistforslideshare-210123143943.pptx
Linkedlist
Linked list
Linked list
Linked list
Linked list

Similar to DS Code (CWH).docx (20)

PPT
Linked list
PPT
Linked list
PPT
Linked list
PPTX
linkedlist.pptx
PPTX
Linked List Data structure using C programming and all the detailed informat...
PPTX
Ll.pptx
PPTX
linked list.pptx
PPTX
Unit 1 LINEAR DATA STRUCTURES
PPT
Unit ii(dsc++)
PPT
DS Unit 2.ppt
PPTX
Linked List.pptx
PPTX
Unit II Data Structure 2hr topic - List - Operations.pptx
PPTX
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
PPT
linkedlist.ppt
PPTX
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
PPT
Linked list1.ppt
PPTX
UNIT 3a.pptx
PPT
Singly Circular Linked List – Last node points to the first node.
PPT
Wk11-linkedlist.ppt linked list complete iit
Linked list
Linked list
Linked list
linkedlist.pptx
Linked List Data structure using C programming and all the detailed informat...
Ll.pptx
linked list.pptx
Unit 1 LINEAR DATA STRUCTURES
Unit ii(dsc++)
DS Unit 2.ppt
Linked List.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
linked list.pptxdj bdjbhjddnbfjdndvdhbfvgh
linkedlist.ppt
UNIT 2LINKEDLISdddddddddddddddddddddddddddT.pptx
Linked list1.ppt
UNIT 3a.pptx
Singly Circular Linked List – Last node points to the first node.
Wk11-linkedlist.ppt linked list complete iit
Ad

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
RMMM.pdf make it easy to upload and study
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Basic Mud Logging Guide for educational purpose
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Insiders guide to clinical Medicine.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Complications of Minimal Access Surgery at WLH
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Microbial disease of the cardiovascular and lymphatic systems
RMMM.pdf make it easy to upload and study
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Basic Mud Logging Guide for educational purpose
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
01-Introduction-to-Information-Management.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Insiders guide to clinical Medicine.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Complications of Minimal Access Surgery at WLH
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
VCE English Exam - Section C Student Revision Booklet
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Ad

DS Code (CWH).docx

  • 1. Implementing Array as an Abstract Data Type in C Language #include<stdio.h> #include<stdlib.h> struct myArray { int total_size; int used_size; int *ptr; }; voidcreateArray(structmyArray*a, inttSize,intuSize){ // (*a).total_size =tSize; // (*a).used_size=uSize; // (*a).ptr= (int*)malloc(tSize*sizeof(int)); a->total_size = tSize; a->used_size =uSize; a->ptr = (int*)malloc(tSize *sizeof(int)); } voidshow(structmyArray*a){ for (inti = 0; i < a->used_size;i++) { printf("%dn",(a->ptr)[i]); } } voidsetVal(structmyArray*a){ int n; for (inti = 0; i < a->used_size;i++)
  • 2. { printf("Enterelement%d",i); scanf("%d",&n); (a->ptr)[i] =n; } } intmain(){ struct myArraymarks; createArray(&marks,10,2); printf("We are runningsetVal nown"); setVal(&marks); printf("We are runningshownown"); show(&marks); return0; } Coding Insertion Operation in Array in Data Structures in C language #include<stdio.h> voiddisplay(intarr[],intn){ // Code forTraversal for (inti = 0; i < n; i++) { printf("%d",arr[i]); }
  • 3. printf("n"); } intindInsertion(intarr[],intsize,intelement,int capacity,intindex){ // code for Insertion if(size>=capacity){ return-1; } for (inti = size-1;i >=index;i--) { arr[i+1] = arr[i]; } arr[index] =element; return1; } intmain(){ int arr[100] = {7, 8, 12, 27, 88}; int size = 5, element=45, index=1; display(arr,size); indInsertion(arr,size,element,100,index); size +=1; display(arr,size); return0; } Coding Deletion Operation in Array Using C Language
  • 4. #include <stdio.h> voiddisplay(intarr[],intn) { // Code forTraversal for (inti = 0; i < n; i++) { printf("%d",arr[i]); } printf("n"); } voidindDeletion(intarr[],intsize,intindex) { // code for Deletion for (inti = index;i < size-1;i++) { arr[i] = arr[i + 1]; } } intmain() { int arr[100] = {7, 8, 12, 27, 88}; int size = 5, element=45, index =0; display(arr,size); indDeletion(arr,size,index); size -= 1; display(arr,size); return0; }
  • 5. Linear Vs Binary Search + Code in C Language #include<stdio.h> intlinearSearch(intarr[],intsize,intelement){ for (inti = 0; i < size;i++) { if(arr[i]==element){ returni; } } return-1; } intbinarySearch(intarr[],intsize,intelement){ int low,mid,high; low= 0; high= size-1; // Keepsearchinguntil low<=high while(low<=high){ mid= (low+ high)/2; if(arr[mid] ==element){ returnmid; } if(arr[mid]<element){ low= mid+1; } else{ high= mid -1;
  • 6. } } return-1; } intmain(){ // Unsortedarray for linearsearch // intarr[] = {1,3,5,56,4,3,23,5,4,54634,56,34}; // intsize = sizeof(arr)/sizeof(int); // Sortedarray for binarysearch int arr[] = {1,3,5,56,64,73,123,225,444}; int size = sizeof(arr)/sizeof(int); int element=444; int searchIndex =binarySearch(arr,size,element); printf("The element%dwasfoundatindex %d n",element,searchIndex); return0; } Linked List Data Structure: Creation and Traversal in C Language #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next;
  • 7. }; voidlinkedListTraversal(structNode *ptr) { while (ptr!= NULL) { printf("Element:%dn",ptr->data); ptr = ptr->next; } } intmain() { struct Node *head; struct Node *second; struct Node *third; struct Node *fourth; // Allocate memoryfornodesinthe linkedlistinHeap head= (structNode *)malloc(sizeof(structNode)); second= (structNode *)malloc(sizeof(structNode)); third= (structNode *)malloc(sizeof(structNode)); fourth= (structNode *)malloc(sizeof(structNode)); // Linkfirstand secondnodes head->data= 7; head->next=second; // Linksecondand thirdnodes second->data= 11; second->next=third;
  • 8. // Linkthirdand fourthnodes third->data= 41; third->next=fourth; // Terminate the listatthe thirdnode fourth->data= 66; fourth->next=NULL; linkedListTraversal(head); return0; } Insertion in a Linked List in C Language #include<stdio.h> #include<stdlib.h> struct Node{ int data; struct Node * next; }; voidlinkedListTraversal(structNode *ptr) { while (ptr!= NULL) { printf("Element:%dn",ptr->data); ptr = ptr->next; } }
  • 9. // Case 1 struct Node * insertAtFirst(structNode *head,intdata){ struct Node * ptr = (structNode *) malloc(sizeof(structNode)); ptr->data = data; ptr->next= head; returnptr; } // Case 2 struct Node * insertAtIndex(structNode *head,intdata,intindex){ struct Node * ptr = (structNode *) malloc(sizeof(structNode)); struct Node * p = head; int i = 0; while (i!=index-1) { p = p->next; i++; } ptr->data = data; ptr->next= p->next; p->next= ptr; returnhead; } // Case 3 struct Node * insertAtEnd(structNode *head,intdata){ struct Node * ptr = (structNode *) malloc(sizeof(structNode)); ptr->data = data;
  • 10. struct Node * p = head; while(p->next!=NULL){ p = p->next; } p->next= ptr; ptr->next= NULL; returnhead; } // Case 4 struct Node * insertAfterNode(structNode *head,structNode *prevNode,intdata){ struct Node * ptr = (structNode *) malloc(sizeof(structNode)); ptr->data = data; ptr->next= prevNode->next; prevNode->next=ptr; returnhead; } intmain(){ struct Node *head; struct Node *second; struct Node *third; struct Node *fourth; // Allocate memoryfornodesinthe linkedlistinHeap head= (structNode *)malloc(sizeof(structNode));
  • 11. second= (structNode *)malloc(sizeof(structNode)); third= (structNode *)malloc(sizeof(structNode)); fourth= (structNode *)malloc(sizeof(structNode)); // Linkfirstand secondnodes head->data= 7; head->next=second; // Linksecondand thirdnodes second->data= 11; second->next=third; // Linkthirdand fourthnodes third->data= 41; third->next=fourth; // Terminate the listatthe thirdnode fourth->data= 66; fourth->next=NULL; printf("Linkedlistbefore insertionn"); linkedListTraversal(head); // head= insertAtFirst(head,56); // head= insertAtIndex(head,56,1); // head= insertAtEnd(head,56); head= insertAfterNode(head,third,45); printf("nLinkedlistafterinsertionn"); linkedListTraversal(head); return0;
  • 12. Delete a Node from Linked List (C Code For Deletion From Beginning, End, Specified Position & Key) #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node *next; }; voidlinkedListTraversal(structNode *ptr) { while (ptr!= NULL) { printf("Element:%dn",ptr->data); ptr = ptr->next; } } // Case 1: Deletingthe firstelementfromthe linkedlist struct Node * deleteFirst(structNode *head){ struct Node * ptr = head; head= head->next; free(ptr); returnhead; }
  • 13. // Case 2: Deletingthe elementata givenindex fromthe linkedlist struct Node * deleteAtIndex(structNode *head,intindex){ struct Node *p = head; struct Node *q = head->next; for (inti = 0; i < index-1;i++) { p = p->next; q = q->next; } p->next= q->next; free(q); returnhead; } // Case 3: Deletingthe lastelement struct Node * deleteAtLast(structNode *head){ struct Node *p = head; struct Node *q = head->next; while(q->next!=NULL) { p = p->next; q = q->next; } p->next= NULL; free(q); returnhead; }
  • 14. // Case 4: Deletingthe elementwithagivenvalue fromthe linkedlist struct Node * deleteAtIndex(structNode *head,intvalue){ struct Node *p = head; struct Node *q = head->next; while(q->data!=value &&q->next!=NULL) { p = p->next; q = q->next; } if(q->data== value){ p->next=q->next; free(q); } returnhead; } intmain() { struct Node *head; struct Node *second; struct Node *third; struct Node *fourth; // Allocate memoryfornodesinthe linkedlistinHeap head= (structNode *)malloc(sizeof(structNode)); second= (structNode *)malloc(sizeof(structNode)); third= (structNode *)malloc(sizeof(structNode)); fourth= (structNode *)malloc(sizeof(structNode)); // Linkfirstand secondnodes head->data= 4;
  • 15. head->next=second; // Linksecondand thirdnodes second->data= 3; second->next=third; // Linkthirdand fourthnodes third->data= 8; third->next=fourth; // Terminate the listatthe thirdnode fourth->data= 1; fourth->next=NULL; printf("Linkedlistbefore deletionn"); linkedListTraversal(head); // head= deleteFirst(head);//Fordeletingfirstelementof the linkedlist // head= deleteAtIndex(head,2); head= deleteAtLast(head); printf("Linkedlistafterdeletionn"); linkedListTraversal(head); return0; } Circular Linked Lists: Operations in C Language #include<stdio.h>
  • 16. #include<stdlib.h> struct Node { int data; struct Node *next; }; voidlinkedListTraversal(structNode *head){ struct Node *ptr = head; do{ printf("Elementis%dn",ptr->data); ptr = ptr->next; }while(ptr!=head); } struct Node * insertAtFirst(structNode *head,intdata){ struct Node * ptr = (structNode *) malloc(sizeof(structNode)); ptr->data = data; struct Node * p = head->next; while(p->next!=head){ p = p->next; } // At thispointppointsto the last node of thiscircularlinkedlist p->next= ptr; ptr->next= head; head= ptr; returnhead;
  • 17. } intmain(){ struct Node *head; struct Node *second; struct Node *third; struct Node *fourth; // Allocate memoryfornodesinthe linkedlistinHeap head= (structNode *)malloc(sizeof(structNode)); second= (structNode *)malloc(sizeof(struct Node)); third= (structNode *)malloc(sizeof(structNode)); fourth= (structNode *)malloc(sizeof(structNode)); // Linkfirstand secondnodes head->data= 4; head->next=second; // Linksecondand thirdnodes second->data= 3; second->next=third; // Linkthirdand fourthnodes third->data= 6; third->next=fourth; // Terminate the listatthe thirdnode fourth->data= 1; fourth->next=head;
  • 18. printf("CircularLinkedlistbefore insertionn"); linkedListTraversal(head); head= insertAtFirst(head,54); head= insertAtFirst(head,58); head= insertAtFirst(head,59); printf("CircularLinkedlistafterinsertionn"); linkedListTraversal(head); return0; } Doubly Linked Lists Explained With Code in C Language #include<stdio.h> #include<stdlib.h> struct Node { int data; struct Node *next; }; voidlinkedListTraversal(structNode *head){ struct Node *ptr = head; do{ printf("Elementis%dn", ptr->data); ptr = ptr->next; }while(ptr!=head); }
  • 19. struct Node * insertAtFirst(structNode *head,intdata){ struct Node * ptr = (structNode *) malloc(sizeof(structNode)); ptr->data = data; struct Node * p = head->next; while(p->next!=head){ p = p->next; } // At thispointppointsto the last node of thiscircularlinkedlist p->next= ptr; ptr->next= head; head= ptr; returnhead; } intmain(){ struct Node *head; struct Node *second; struct Node *third; struct Node *fourth; // Allocate memoryfornodesinthe linkedlistinHeap head= (structNode *)malloc(sizeof(structNode)); second= (structNode *)malloc(sizeof(structNode)); third= (structNode *)malloc(sizeof(structNode)); fourth= (structNode *)malloc(sizeof(structNode)); // Linkfirstand secondnodes
  • 20. head->data= 4; head->next=second; // Linksecondand thirdnodes second->data= 3; second->next=third; // Linkthirdand fourthnodes third->data= 6; third->next=fourth; // Terminate the listatthe thirdnode fourth->data= 1; fourth->next=head; printf("CircularLinkedlistbefore insertionn"); linkedListTraversal(head); head= insertAtFirst(head,54); head= insertAtFirst(head,58); head= insertAtFirst(head,59); printf("CircularLinkedlistafterinsertionn"); linkedListTraversal(head); return0; }