SlideShare a Scribd company logo
1 of 14
Download to read offline
717822E504
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
717822E504
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:
717822E504
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));
717822E504
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);
717822E504
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);
717822E504
}
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.
717822E504
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
717822E504
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;
}
717822E504
}
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;
717822E504
}
}
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);
}
717822E504
}
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() {
717822E504
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.
717822E504
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) {
717822E504
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 Lab-2.2 717822E504.pdf

How to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdfHow to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdf
footstatus
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
ritu1806
 
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
mail931892
 
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
FOREVERPRODUCTCHD
 
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
wasemanivytreenrco51
 
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
Jason0x0Scottw
 
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
Malikireddy Bramhananda Reddy
 
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
almonardfans
 
Please finish the int LLInsert function.typedef struct STUDENT {.pdf
Please finish the int LLInsert function.typedef struct STUDENT {.pdfPlease finish the int LLInsert function.typedef struct STUDENT {.pdf
Please finish the int LLInsert function.typedef struct STUDENT {.pdf
fortmdu
 

Similar to Lab-2.2 717822E504.pdf (20)

How to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdfHow to delete one specific node in linked list in CThanksSolu.pdf
How to delete one specific node in linked list in CThanksSolu.pdf
 
Doublylinklist
DoublylinklistDoublylinklist
Doublylinklist
 
C program
C programC program
C program
 
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
 
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
 
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
 
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
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
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
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
 
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
 
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
 
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
 
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
 
Please finish the int LLInsert function.typedef struct STUDENT {.pdf
Please finish the int LLInsert function.typedef struct STUDENT {.pdfPlease finish the int LLInsert function.typedef struct STUDENT {.pdf
Please finish the int LLInsert function.typedef struct STUDENT {.pdf
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
 

Recently uploaded

Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
amitlee9823
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
amitlee9823
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
gajnagarg
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
amitlee9823
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Riyadh +966572737505 get cytotec
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
gajnagarg
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
only4webmaster01
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
amitlee9823
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
amitlee9823
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night StandCall Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Hsr Layout ☎ 7737669865 🥵 Book Your One night Stand
 
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Junnasandra Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
Just Call Vip call girls Mysore Escorts ☎️9352988975 Two shot with one girl (...
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
Abortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get CytotecAbortion pills in Jeddah | +966572737505 | Get Cytotec
Abortion pills in Jeddah | +966572737505 | Get Cytotec
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Rabindra Nagar  (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Rabindra Nagar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
 
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 

Lab-2.2 717822E504.pdf

  • 1. 717822E504 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. 717822E504 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. 717822E504 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. 717822E504 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. 717822E504 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. 717822E504 } 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. 717822E504 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. 717822E504 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. 717822E504 } 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. 717822E504 } } 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. 717822E504 } 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. 717822E504 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. 717822E504 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