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

Heart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectHeart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectBoston Institute of Analytics
 
在线办理UM毕业证迈阿密大学毕业证成绩单留信学历认证
在线办理UM毕业证迈阿密大学毕业证成绩单留信学历认证在线办理UM毕业证迈阿密大学毕业证成绩单留信学历认证
在线办理UM毕业证迈阿密大学毕业证成绩单留信学历认证nhjeo1gg
 
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Boston Institute of Analytics
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxMike Bennett
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...Thomas Poetter
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024thyngster
 
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...Boston Institute of Analytics
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
INTRODUCTION TO Natural language processing
INTRODUCTION TO Natural language processingINTRODUCTION TO Natural language processing
INTRODUCTION TO Natural language processingsocarem879
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Cathrine Wilhelmsen
 
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxmodul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxaleedritatuxx
 
毕业文凭制作#回国入职#diploma#degree美国加州州立大学北岭分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#de...
毕业文凭制作#回国入职#diploma#degree美国加州州立大学北岭分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#de...毕业文凭制作#回国入职#diploma#degree美国加州州立大学北岭分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#de...
毕业文凭制作#回国入职#diploma#degree美国加州州立大学北岭分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#de...ttt fff
 
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一F La
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhijennyeacort
 
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一F La
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanMYRABACSAFRA2
 
Real-Time AI Streaming - AI Max Princeton
Real-Time AI  Streaming - AI Max PrincetonReal-Time AI  Streaming - AI Max Princeton
Real-Time AI Streaming - AI Max PrincetonTimothy Spann
 

Recently uploaded (20)

Heart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectHeart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis Project
 
在线办理UM毕业证迈阿密大学毕业证成绩单留信学历认证
在线办理UM毕业证迈阿密大学毕业证成绩单留信学历认证在线办理UM毕业证迈阿密大学毕业证成绩单留信学历认证
在线办理UM毕业证迈阿密大学毕业证成绩单留信学历认证
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
Data Analysis Project : Targeting the Right Customers, Presentation on Bank M...
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptx
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
Minimizing AI Hallucinations/Confabulations and the Path towards AGI with Exa...
 
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
Consent & Privacy Signals on Google *Pixels* - MeasureCamp Amsterdam 2024
 
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
Decoding the Heart: Student Presentation on Heart Attack Prediction with Data...
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
INTRODUCTION TO Natural language processing
INTRODUCTION TO Natural language processingINTRODUCTION TO Natural language processing
INTRODUCTION TO Natural language processing
 
Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)Data Factory in Microsoft Fabric (MsBIP #82)
Data Factory in Microsoft Fabric (MsBIP #82)
 
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptxmodul pembelajaran robotic Workshop _ by Slidesgo.pptx
modul pembelajaran robotic Workshop _ by Slidesgo.pptx
 
毕业文凭制作#回国入职#diploma#degree美国加州州立大学北岭分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#de...
毕业文凭制作#回国入职#diploma#degree美国加州州立大学北岭分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#de...毕业文凭制作#回国入职#diploma#degree美国加州州立大学北岭分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#de...
毕业文凭制作#回国入职#diploma#degree美国加州州立大学北岭分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#de...
 
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
 
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
办理(UWIC毕业证书)英国卡迪夫城市大学毕业证成绩单原版一比一
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population Mean
 
Real-Time AI Streaming - AI Max Princeton
Real-Time AI  Streaming - AI Max PrincetonReal-Time AI  Streaming - AI Max Princeton
Real-Time AI Streaming - AI Max Princeton
 

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