SlideShare a Scribd company logo
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
Doublylinklistritu1806
 
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
 
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
Sarmad 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.pdf
wasemanivytreenrco51
 
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
Lakshmi Sarvani Videla
 
C Assignment Help
C Assignment HelpC Assignment Help
C Assignment Help
Programming Homework Help
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
DaniyalAli81
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
Lakshmi Sarvani Videla
 
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
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
Santhiya S
 
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
 
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
 
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
deepua8
 
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
AKVIGFOEU
 
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
 
data structure3.pptx
data structure3.pptxdata structure3.pptx
data structure3.pptx
SajalFayyaz
 

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

一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
enxupq
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
nscud
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
Opendatabay
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
ewymefz
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
slg6lamcq
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
vcaxypu
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
ewymefz
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
ukgaet
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Subhajit Sahu
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
ewymefz
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
Tiktokethiodaily
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
NABLAS株式会社
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
ewymefz
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
vcaxypu
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
NABLAS株式会社
 

Recently uploaded (20)

一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单一比一原版(QU毕业证)皇后大学毕业证成绩单
一比一原版(QU毕业证)皇后大学毕业证成绩单
 
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
一比一原版(CBU毕业证)不列颠海角大学毕业证成绩单
 
Opendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptxOpendatabay - Open Data Marketplace.pptx
Opendatabay - Open Data Marketplace.pptx
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单
 
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
一比一原版(UniSA毕业证书)南澳大学毕业证如何办理
 
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
一比一原版(ArtEZ毕业证)ArtEZ艺术学院毕业证成绩单
 
一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单一比一原版(BU毕业证)波士顿大学毕业证成绩单
一比一原版(BU毕业证)波士顿大学毕业证成绩单
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
一比一原版(UVic毕业证)维多利亚大学毕业证成绩单
 
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
Algorithmic optimizations for Dynamic Levelwise PageRank (from STICD) : SHORT...
 
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
一比一原版(UPenn毕业证)宾夕法尼亚大学毕业证成绩单
 
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
1.Seydhcuxhxyxhccuuxuxyxyxmisolids 2019.pptx
 
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
【社内勉強会資料_Octo: An Open-Source Generalist Robot Policy】
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
SOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape ReportSOCRadar Germany 2024 Threat Landscape Report
SOCRadar Germany 2024 Threat Landscape Report
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
一比一原版(UofM毕业证)明尼苏达大学毕业证成绩单
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
一比一原版(RUG毕业证)格罗宁根大学毕业证成绩单
 
社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .社内勉強会資料_LLM Agents                              .
社内勉強会資料_LLM Agents                              .
 

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