SlideShare a Scribd company logo
1 of 20
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

Similar to Implement Array as Abstract Data Type in C

DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxVeerannaKotagi1
 
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
Getting the following errorsError 1 error C2436 {ctor}  mem.pdfGetting the following errorsError 1 error C2436 {ctor}  mem.pdf
Getting the following errorsError 1 error C2436 {ctor} mem.pdfherminaherman
 
i nsert+in+ link list
i nsert+in+ link listi nsert+in+ link list
i nsert+in+ link listEAJAJAhamed
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
Implement of c &amp; its coding programming by sarmad baloch
Implement of c &amp; its coding  programming by sarmad balochImplement of c &amp; its coding  programming by sarmad baloch
Implement of c &amp; its coding programming by sarmad balochSarmad Baloch
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxteyaj1
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfalphaagenciesindia
 
#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdfankitmobileshop235
 
#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdfKUNALHARCHANDANI1
 
program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)Ankit Gupta
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdfdeepua8
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdfanandatalapatra
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklistritu1806
 
M 0 1 2 3 4 5 6 7 0.pdf
 M  0  1  2  3  4  5  6  7    0.pdf M  0  1  2  3  4  5  6  7    0.pdf
M 0 1 2 3 4 5 6 7 0.pdfajay1317
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docxAdamq0DJonese
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfmichardsonkhaicarr37
 

Similar to Implement Array as Abstract Data Type in C (20)

DS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docxDS UNIT3_LINKED LISTS.docx
DS UNIT3_LINKED LISTS.docx
 
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
Getting the following errorsError 1 error C2436 {ctor}  mem.pdfGetting the following errorsError 1 error C2436 {ctor}  mem.pdf
Getting the following errorsError 1 error C2436 {ctor} mem.pdf
 
i nsert+in+ link list
i nsert+in+ link listi nsert+in+ link list
i nsert+in+ link list
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
Implement of c &amp; its coding programming by sarmad baloch
Implement of c &amp; its coding  programming by sarmad balochImplement of c &amp; its coding  programming by sarmad baloch
Implement of c &amp; its coding programming by sarmad baloch
 
Linked lists
Linked listsLinked lists
Linked lists
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
Rewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdfRewrite this code so it can use a generic type instead of integers. .pdf
Rewrite this code so it can use a generic type instead of integers. .pdf
 
#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf
 
#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf#includeiostream#includecstdio#includecstdlibusing names.pdf
#includeiostream#includecstdio#includecstdlibusing names.pdf
 
program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)
 
Vcs29
Vcs29Vcs29
Vcs29
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdf
 
c programming
c programmingc programming
c programming
 
#include iostream #include cstring #include vector #i.pdf
 #include iostream #include cstring #include vector #i.pdf #include iostream #include cstring #include vector #i.pdf
#include iostream #include cstring #include vector #i.pdf
 
C program
C programC program
C program
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
M 0 1 2 3 4 5 6 7 0.pdf
 M  0  1  2  3  4  5  6  7    0.pdf M  0  1  2  3  4  5  6  7    0.pdf
M 0 1 2 3 4 5 6 7 0.pdf
 
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
-- Task 2- Debugging a program with stacks- queues- and doubly-linked.docx
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 

Recently uploaded

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 

Recently uploaded (20)

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 

Implement Array as Abstract Data Type in C

  • 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; }