SlideShare ist ein Scribd-Unternehmen logo
1 von 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;
}

Weitere ähnliche Inhalte

Ähnlich wie DS Code (CWH).docx

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
 

Ähnlich wie DS Code (CWH).docx (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
 

Kürzlich hochgeladen

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesShubhangi Sonawane
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 

Kürzlich hochgeladen (20)

Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural ResourcesEnergy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
Energy Resources. ( B. Pharmacy, 1st Year, Sem-II) Natural Resources
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 

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