SlideShare a Scribd company logo
1 of 14
Download to read offline
717821E139
QUESTIONS 2.2:
a) Write a C program to implement the following operations on a Singly Linked List.
(1) InsertionAtBegin() (2) InsertionAtEnd() (3) InsertionAtPosition() (4) DeletionAtBegin()
(5) DeletionAtEnd() (6) DeletionAtPosition() (7) Display
b) Write a C program to implement the following operations on a Circular Singly Linked List.
(1) InsertionAtBegin() (2) InsertionAtEnd() (3) InsertionAtPosition() (4) DeletionAtBegin()
(5) DeletionAtEnd() (6) DeletionAtPosition() (7) Display
c) Write a C program to print the sum of the nodes of a Singly Linked List.
Task is to do: 5 + 10 + 20 + 1
Output: 36
717821E139
Ex. No: 2.2(a)
Singly Linked List
Date:
AIM:
To write a C program to implement the following operations on a Singly Linked List.
(1) InsertionAtBegin() (2) InsertionAtEnd() (3) InsertionAtPosition() (4) DeletionAtBegin()
(5) DeletionAtEnd() (6) DeletionAtPosition() (7) Display
PSEUDO CODE:
def __init__(self, data):
self.data = data
self.next = None
class LinkedList:
def __init__(self):
self.head = None
def add(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next is not None:
current = current.next
current.next = new_node
def delete(self, key):
if self.head is None:
return
if self.head.data == key:
self.head = self.head.next
return
current = self.head
prev = None
while current is not None and current.data != key:
prev = current
current = current.next
if current is None:
return
prev.next = current.next
def display(self):
current = self.head
if self.head is None:
print("List is empty")
else:
717821E139
while current is not None:
print(current.data)
current = current.next
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
void InsertionAtBegin(int data) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = head;
head = newNode;
}
void InsertionAtEnd(int data) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
if(head == NULL) {
head = newNode;
return;
}
struct node *temp = head;
while(temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
void InsertionAtPosition(int data, int position) {
struct node *newNode = (struct node*)malloc(sizeof(struct node));
717821E139
newNode->data = data;
if(position == 1) {
newNode->next = head;
head = newNode;
return;
}
struct node *temp = head;
for(int i=1; i<position-1; i++) {
temp = temp->next;
if(temp == NULL) {
printf("Invalid positionn");
return;
}
}
newNode->next = temp->next;
temp->next = newNode;
}
void DeletionAtBegin() {
if(head == NULL) {
printf("List is emptyn");
return;
}
struct node *temp = head;
head = head->next;
free(temp);
}
void DeletionAtEnd() {
if(head == NULL) {
printf("List is emptyn");
return;
}
if(head->next == NULL) {
free(head);
717821E139
head = NULL;
return;
}
struct node *temp1 = head, *temp2=NULL;
while(temp1->next != NULL) {
temp2=temp1;
temp1=temp1->next;
}
free(temp1);
if(temp2 != NULL)
temp2->next=NULL;
}
void DeletionAtPosition(int position) {
if(head == NULL) {
printf("List is emptyn");
return;
}
struct node *temp1=head, *temp2=NULL;
if(position == 1) {
head=temp1->next;
free(temp1);
return;
}
for(int i=1; i<position; i++) {
temp2=temp1;
temp1=temp1->next;
if(temp1 == NULL) {
printf("Invalid positionn");
return;
}
}
temp2->next=temp1->next;
free(temp1);
717821E139
}
void Display() {
struct node *temp=head;
while(temp != NULL) {
printf("%d ",temp->data);
temp=temp->next;
}
}
int main() {
InsertionAtBegin(10);
InsertionAtBegin(20);
InsertionAtEnd(30);
InsertionAtPosition(40, 3);
DeletionAtBegin();
DeletionAtEnd();
DeletionAtPosition(2);
Display();
return 0;
}
OUTPUT:
RESULT:
Thus the program to implement the following operations on a Singly Linked List is compiled and
executed successfully.
717821E139
Ex. No: 2.2(b)
Circular Singly Linked List
Date:
AIM:
To write a C program to implement the following operations on a Circular Singly Linked List.
(1) InsertionAtBegin() (2) InsertionAtEnd() (3) InsertionAtPosition() (4) DeletionAtBegin()
(5) DeletionAtEnd() (6) DeletionAtPosition() (7) Display
PSEUDO CODE:
def __init__(self, data):
self.data = data
self.next = None
class CircularLinkedList:
def __init__(self):
self.head = None
def add(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
new_node.next = self.head
else:
current = self.head
while current.next != self.head:
current = current.next
current.next = new_node
new_node.next = self.head
def delete(self, key):
if self.head is None:
return
if self.head.data == key and self.head.next == self.head:
self.head = None
return
elif self.head.data == key:
current = self.head
while current.next != self.head:
current = current.next
current.next = self.head.next
self.head = self.head.next
else:
current = self.head
prev = None
while current.next != self.head:
prev = current
current = current.next
717821E139
if current.data == key:
prev.next = current.next
return
def display(self):
current = self.head
if self.head is None:
print("List is empty")
else:
while True:
print(current.data)
current = current.next
if current == self.head:
break
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
void insertionAtBegin(int value) {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
struct node *temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
head = newNode;
}
717821E139
}
void insertionAtEnd(int value) {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
struct node *temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
}
}
void insertionAtPosition(int value, int position) {
struct node *newNode = (struct node *)malloc(sizeof(struct node));
newNode->data = value;
if (head == NULL) {
head = newNode;
newNode->next = head;
} else {
struct node *temp = head;
for (int i = 1; i < position - 1; i++) {
temp = temp->next;
if (temp == head) {
printf("Invalid positionn");
return;
}
}
newNode->next = temp->next;
temp->next = newNode;
717821E139
}
}
void deletionAtBegin() {
if (head == NULL) {
printf("List is emptyn");
} else if (head->next == head) {
free(head);
head = NULL;
} else {
struct node *temp1 = head, *temp2 = head;
while (temp2->next != head) {
temp2 = temp2->next;
}
head = temp1->next;
temp2->next = head;
free(temp1);
}
}
void deletionAtEnd() {
if (head == NULL) {
printf("List is emptyn");
} else if (head->next == head) {
free(head);
head = NULL;
} else {
struct node *temp1 = head, *temp2 = NULL;
while (temp1->next != head) {
temp2 = temp1;
temp1 = temp1->next;
}
temp2->next = head;
free(temp1);
}
717821E139
}
void deletionAtPosition(int position) {
if (head == NULL) {
printf("List is emptyn");
} else if (position == 1) {
deletionAtBegin();
} else {
struct node *temp1 = head, *temp2 = NULL;
for (int i = 1; i < position; i++) {
temp2 = temp1;
temp1 = temp1->next;
if (temp1 == head) {
printf("Invalid positionn");
return;
}
}
temp2->next=temp1->next;
free(temp1);
}
}
void display() {
struct node* ptr=head;
if(head==NULL){
printf("List is emptyn");
return;
}
printf("Elements of the list are: ");
do {
printf("%d ",ptr->data);
ptr=ptr->next;
}while(ptr!=head);
}
int main() {
717821E139
insertionAtBegin(10);
insertionAtBegin(20);
insertionAtEnd(30);
insertionAtPosition(40,3);
deletionAtBegin();
deletionAtEnd();
deletionAtPosition(2);
display();
}
OUTPUT:
RESULT:
Thus the program to implement the following operations on a Circular Singly Linked List is
compiled and executed successfully.
717821E139
Ex. No: 2.2(c)
Sum of nodes in Singly Linked List
Date:
AIM:
To write a C program to print the sum of the nodes of a Singly Linked List.
PSEUDO CODE:
sum = 0
current = head
while current is not None:
sum += current.data
current = current.next
print(sum)
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
};
void insert(struct node **head, int data) {
struct node *new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = data;
new_node->next = (*head);
(*head) = new_node;
}
int sum_of_nodes(struct node *head) {
int sum = 0;
struct node *temp = head;
while (temp != NULL) {
sum += temp->data;
temp = temp->next;
}
return sum;
}
void print_list(struct node *node) {
717821E139
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}
int main() {
struct node *head = NULL;
insert(&head, 1);
insert(&head, 10);
insert(&head, 20);
insert(&head, 5);
printf("Linked List: ");
print_list(head);
printf("nSum of nodes: %d", sum_of_nodes(head));
return 0;
}
OUTPUT:
RESULT:
Thus the program to print the sum of the nodes in the Singly Linked List is compiled and executed
successfully.
PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100
INITIAL OF FACULTY

More Related Content

Similar to 137 Lab-2.2.pdf

reverse the linked list (2-4-8-10) by- stack- iteration- recursion- U.docx
reverse the linked list (2-4-8-10) by- stack- iteration- recursion-  U.docxreverse the linked list (2-4-8-10) by- stack- iteration- recursion-  U.docx
reverse the linked list (2-4-8-10) by- stack- iteration- recursion- U.docxacarolyn
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfFOREVERPRODUCTCHD
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfmail931892
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklistritu1806
 
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
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfwasemanivytreenrco51
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptxSanthiya S
 
Help explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfHelp explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfalmonardfans
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYMalikireddy Bramhananda Reddy
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++mustkeem khan
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diplomamustkeem khan
 
fix the error - class Node{ int data- Node next-.pdf
fix the error -   class Node{           int data-           Node next-.pdffix the error -   class Node{           int data-           Node next-.pdf
fix the error - class Node{ int data- Node next-.pdfAKVIGFOEU
 
Need help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docxNeed help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docxJason0x0Scottw
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptxSajalFayyaz
 

Similar to 137 Lab-2.2.pdf (20)

reverse the linked list (2-4-8-10) by- stack- iteration- recursion- U.docx
reverse the linked list (2-4-8-10) by- stack- iteration- recursion-  U.docxreverse the linked list (2-4-8-10) by- stack- iteration- recursion-  U.docx
reverse the linked list (2-4-8-10) by- stack- iteration- recursion- U.docx
 
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdfimplement the ListLinked ADT (the declaration is given in ListLinked.pdf
implement the ListLinked ADT (the declaration is given in ListLinked.pdf
 
Help I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdfHelp I keep getting the same error when running a code. Below is the.pdf
Help I keep getting the same error when running a code. Below is the.pdf
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
 
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
 
C program
C programC program
C program
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
pleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdfpleaase I want manual solution forData Structures and Algorithm An.pdf
pleaase I want manual solution forData Structures and Algorithm An.pdf
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
 
Help explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdfHelp explain the code with line comments public class CompletedLis.pdf
Help explain the code with line comments public class CompletedLis.pdf
 
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDYDATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
DATASTRUCTURES PPTS PREPARED BY M V BRAHMANANDA REDDY
 
DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++DATA STRUCTURE USING C & C++
DATA STRUCTURE USING C & C++
 
DSU C&C++ Practical File Diploma
DSU C&C++ Practical File DiplomaDSU C&C++ Practical File Diploma
DSU C&C++ Practical File Diploma
 
fix the error - class Node{ int data- Node next-.pdf
fix the error -   class Node{           int data-           Node next-.pdffix the error -   class Node{           int data-           Node next-.pdf
fix the error - class Node{ int data- Node next-.pdf
 
Need help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docxNeed help getting past an error in C++! I have all my code pasted down.docx
Need help getting past an error in C++! I have all my code pasted down.docx
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
 

Recently uploaded

Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts ServiceSapana Sha
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfRachmat Ramadhan H
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Jack DiGiovanna
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...Suhani Kapoor
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubaihf8803863
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...shivangimorya083
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfLars Albertsson
 
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Servicejennyeacort
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptxAnupama Kate
 
Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationBoston Institute of Analytics
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130Suhani Kapoor
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Callshivangimorya083
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdfHuman37
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 

Recently uploaded (20)

Call Girls In Mahipalpur O9654467111 Escorts Service
Call Girls In Mahipalpur O9654467111  Escorts ServiceCall Girls In Mahipalpur O9654467111  Escorts Service
Call Girls In Mahipalpur O9654467111 Escorts Service
 
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdfMarket Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
Market Analysis in the 5 Largest Economic Countries in Southeast Asia.pdf
 
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
Building on a FAIRly Strong Foundation to Connect Academic Research to Transl...
 
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
VIP High Profile Call Girls Amravati Aarushi 8250192130 Independent Escort Se...
 
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in  KishangarhDelhi 99530 vip 56974 Genuine Escort Service Call Girls in  Kishangarh
Delhi 99530 vip 56974 Genuine Escort Service Call Girls in Kishangarh
 
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls DubaiDubai Call Girls Wifey O52&786472 Call Girls Dubai
Dubai Call Girls Wifey O52&786472 Call Girls Dubai
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
Full night 🥵 Call Girls Delhi New Friends Colony {9711199171} Sanya Reddy ✌️o...
 
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
꧁❤ Aerocity Call Girls Service Aerocity Delhi ❤꧂ 9999965857 ☎️ Hard And Sexy ...
 
Schema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdfSchema on read is obsolete. Welcome metaprogramming..pdf
Schema on read is obsolete. Welcome metaprogramming..pdf
 
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts ServiceCall Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
Call Girls In Noida City Center Metro 24/7✡️9711147426✡️ Escorts Service
 
100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx100-Concepts-of-AI by Anupama Kate .pptx
100-Concepts-of-AI by Anupama Kate .pptx
 
Data Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health ClassificationData Science Project: Advancements in Fetal Health Classification
Data Science Project: Advancements in Fetal Health Classification
 
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
VIP Call Girls Service Miyapur Hyderabad Call +91-8250192130
 
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip CallDelhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
Delhi Call Girls CP 9711199171 ☎✔👌✔ Whatsapp Hard And Sexy Vip Call
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
Deep Generative Learning for All - The Gen AI Hype (Spring 2024)
 
20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf20240419 - Measurecamp Amsterdam - SAM.pdf
20240419 - Measurecamp Amsterdam - SAM.pdf
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 

137 Lab-2.2.pdf

  • 1. 717821E139 QUESTIONS 2.2: a) Write a C program to implement the following operations on a Singly Linked List. (1) InsertionAtBegin() (2) InsertionAtEnd() (3) InsertionAtPosition() (4) DeletionAtBegin() (5) DeletionAtEnd() (6) DeletionAtPosition() (7) Display b) Write a C program to implement the following operations on a Circular Singly Linked List. (1) InsertionAtBegin() (2) InsertionAtEnd() (3) InsertionAtPosition() (4) DeletionAtBegin() (5) DeletionAtEnd() (6) DeletionAtPosition() (7) Display c) Write a C program to print the sum of the nodes of a Singly Linked List. Task is to do: 5 + 10 + 20 + 1 Output: 36
  • 2. 717821E139 Ex. No: 2.2(a) Singly Linked List Date: AIM: To write a C program to implement the following operations on a Singly Linked List. (1) InsertionAtBegin() (2) InsertionAtEnd() (3) InsertionAtPosition() (4) DeletionAtBegin() (5) DeletionAtEnd() (6) DeletionAtPosition() (7) Display PSEUDO CODE: def __init__(self, data): self.data = data self.next = None class LinkedList: def __init__(self): self.head = None def add(self, data): new_node = Node(data) if self.head is None: self.head = new_node else: current = self.head while current.next is not None: current = current.next current.next = new_node def delete(self, key): if self.head is None: return if self.head.data == key: self.head = self.head.next return current = self.head prev = None while current is not None and current.data != key: prev = current current = current.next if current is None: return prev.next = current.next def display(self): current = self.head if self.head is None: print("List is empty") else:
  • 3. 717821E139 while current is not None: print(current.data) current = current.next SOURCE CODE: #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; void InsertionAtBegin(int data) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = data; newNode->next = head; head = newNode; } void InsertionAtEnd(int data) { struct node *newNode = (struct node*)malloc(sizeof(struct node)); newNode->data = data; newNode->next = NULL; if(head == NULL) { head = newNode; return; } struct node *temp = head; while(temp->next != NULL) { temp = temp->next; } temp->next = newNode; } void InsertionAtPosition(int data, int position) { struct node *newNode = (struct node*)malloc(sizeof(struct node));
  • 4. 717821E139 newNode->data = data; if(position == 1) { newNode->next = head; head = newNode; return; } struct node *temp = head; for(int i=1; i<position-1; i++) { temp = temp->next; if(temp == NULL) { printf("Invalid positionn"); return; } } newNode->next = temp->next; temp->next = newNode; } void DeletionAtBegin() { if(head == NULL) { printf("List is emptyn"); return; } struct node *temp = head; head = head->next; free(temp); } void DeletionAtEnd() { if(head == NULL) { printf("List is emptyn"); return; } if(head->next == NULL) { free(head);
  • 5. 717821E139 head = NULL; return; } struct node *temp1 = head, *temp2=NULL; while(temp1->next != NULL) { temp2=temp1; temp1=temp1->next; } free(temp1); if(temp2 != NULL) temp2->next=NULL; } void DeletionAtPosition(int position) { if(head == NULL) { printf("List is emptyn"); return; } struct node *temp1=head, *temp2=NULL; if(position == 1) { head=temp1->next; free(temp1); return; } for(int i=1; i<position; i++) { temp2=temp1; temp1=temp1->next; if(temp1 == NULL) { printf("Invalid positionn"); return; } } temp2->next=temp1->next; free(temp1);
  • 6. 717821E139 } void Display() { struct node *temp=head; while(temp != NULL) { printf("%d ",temp->data); temp=temp->next; } } int main() { InsertionAtBegin(10); InsertionAtBegin(20); InsertionAtEnd(30); InsertionAtPosition(40, 3); DeletionAtBegin(); DeletionAtEnd(); DeletionAtPosition(2); Display(); return 0; } OUTPUT: RESULT: Thus the program to implement the following operations on a Singly Linked List is compiled and executed successfully.
  • 7. 717821E139 Ex. No: 2.2(b) Circular Singly Linked List Date: AIM: To write a C program to implement the following operations on a Circular Singly Linked List. (1) InsertionAtBegin() (2) InsertionAtEnd() (3) InsertionAtPosition() (4) DeletionAtBegin() (5) DeletionAtEnd() (6) DeletionAtPosition() (7) Display PSEUDO CODE: def __init__(self, data): self.data = data self.next = None class CircularLinkedList: def __init__(self): self.head = None def add(self, data): new_node = Node(data) if self.head is None: self.head = new_node new_node.next = self.head else: current = self.head while current.next != self.head: current = current.next current.next = new_node new_node.next = self.head def delete(self, key): if self.head is None: return if self.head.data == key and self.head.next == self.head: self.head = None return elif self.head.data == key: current = self.head while current.next != self.head: current = current.next current.next = self.head.next self.head = self.head.next else: current = self.head prev = None while current.next != self.head: prev = current current = current.next
  • 8. 717821E139 if current.data == key: prev.next = current.next return def display(self): current = self.head if self.head is None: print("List is empty") else: while True: print(current.data) current = current.next if current == self.head: break SOURCE CODE: #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; struct node *head = NULL; void insertionAtBegin(int value) { struct node *newNode = (struct node *)malloc(sizeof(struct node)); newNode->data = value; if (head == NULL) { head = newNode; newNode->next = head; } else { struct node *temp = head; while (temp->next != head) { temp = temp->next; } temp->next = newNode; newNode->next = head; head = newNode; }
  • 9. 717821E139 } void insertionAtEnd(int value) { struct node *newNode = (struct node *)malloc(sizeof(struct node)); newNode->data = value; if (head == NULL) { head = newNode; newNode->next = head; } else { struct node *temp = head; while (temp->next != head) { temp = temp->next; } temp->next = newNode; newNode->next = head; } } void insertionAtPosition(int value, int position) { struct node *newNode = (struct node *)malloc(sizeof(struct node)); newNode->data = value; if (head == NULL) { head = newNode; newNode->next = head; } else { struct node *temp = head; for (int i = 1; i < position - 1; i++) { temp = temp->next; if (temp == head) { printf("Invalid positionn"); return; } } newNode->next = temp->next; temp->next = newNode;
  • 10. 717821E139 } } void deletionAtBegin() { if (head == NULL) { printf("List is emptyn"); } else if (head->next == head) { free(head); head = NULL; } else { struct node *temp1 = head, *temp2 = head; while (temp2->next != head) { temp2 = temp2->next; } head = temp1->next; temp2->next = head; free(temp1); } } void deletionAtEnd() { if (head == NULL) { printf("List is emptyn"); } else if (head->next == head) { free(head); head = NULL; } else { struct node *temp1 = head, *temp2 = NULL; while (temp1->next != head) { temp2 = temp1; temp1 = temp1->next; } temp2->next = head; free(temp1); }
  • 11. 717821E139 } void deletionAtPosition(int position) { if (head == NULL) { printf("List is emptyn"); } else if (position == 1) { deletionAtBegin(); } else { struct node *temp1 = head, *temp2 = NULL; for (int i = 1; i < position; i++) { temp2 = temp1; temp1 = temp1->next; if (temp1 == head) { printf("Invalid positionn"); return; } } temp2->next=temp1->next; free(temp1); } } void display() { struct node* ptr=head; if(head==NULL){ printf("List is emptyn"); return; } printf("Elements of the list are: "); do { printf("%d ",ptr->data); ptr=ptr->next; }while(ptr!=head); } int main() {
  • 13. 717821E139 Ex. No: 2.2(c) Sum of nodes in Singly Linked List Date: AIM: To write a C program to print the sum of the nodes of a Singly Linked List. PSEUDO CODE: sum = 0 current = head while current is not None: sum += current.data current = current.next print(sum) SOURCE CODE: #include <stdio.h> #include <stdlib.h> struct node { int data; struct node *next; }; void insert(struct node **head, int data) { struct node *new_node = (struct node *)malloc(sizeof(struct node)); new_node->data = data; new_node->next = (*head); (*head) = new_node; } int sum_of_nodes(struct node *head) { int sum = 0; struct node *temp = head; while (temp != NULL) { sum += temp->data; temp = temp->next; } return sum; } void print_list(struct node *node) {
  • 14. 717821E139 while (node != NULL) { printf("%d ", node->data); node = node->next; } } int main() { struct node *head = NULL; insert(&head, 1); insert(&head, 10); insert(&head, 20); insert(&head, 5); printf("Linked List: "); print_list(head); printf("nSum of nodes: %d", sum_of_nodes(head)); return 0; } OUTPUT: RESULT: Thus the program to print the sum of the nodes in the Singly Linked List is compiled and executed successfully. PREPARATION 30 LAB PERFORMANCE 30 REPORT 40 TOTAL 100 INITIAL OF FACULTY