SlideShare a Scribd company logo
1 of 11
Download to read offline
717821E101
QUESTIONS 2.4:
a) Write a C program to implement the Stack Data Structure using Singly Linked List.
b) Write a C program to implement the Queue Data Structure using Singly Linked List.
c) Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the
list(in-place) and return head of the merged list.
Example 1:
Input:
N = 4, M = 3
Value N[] = {5,10,15,40}
Value M[] = {2,3,20}
Output: 2 3 5 10 15 20 40
Explanation: After merging the two linked
lists, we have merged list as 2, 3, 5, 10, 15, 20, 40.
Example 2:
Input:
N = 2, M = 2
Value N[] = {1,1}
Value M[] = {2,4}
Output:1 1 2 4
Explanation: After merging the given two
linked list, we have 1, 1, 2, 4 as output.
717821E101
Ex. No: 2.4(a)
Stack using Singly Linked List
Date:
AIM:
To write a C program to implement the Stack data structure using Singly Linked List.
PSEUDO CODE:
Structure Node:
data
next
top = null
Function createNode(data):
newNode = new Node
newNode.data = data
newNode.next = null
return newNode
Function push(data):
newNode = createNode(data)
newNode.next = top
top = newNode
Function pop():
if top is null:
print "Stack is empty!"
return -1
data = top.data
temp = top
top = top.next
free(temp)
return data
Function display():
if top is null:
print "Stack is empty!"
return
temp = top
print "Stack: "
while temp is not null:
print temp.data
temp = temp.next
Main function:
717821E101
push(1)
push(2)
push(3)
display()
print "Popped", pop(), "from the stack"
print "Popped", pop(), "from the stack"
display()
push(4)
push(5)
display()
print "Popped", pop(), "from the stack"
print "Popped", pop(), "from the stack"
print "Popped", pop(), "from the stack"
display()
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// Global pointer for top of stack
struct Node* top = NULL;
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// Function to push an element onto the stack
void push(int data) {
struct Node* newNode = createNode(data);
newNode->next = top;
top = newNode;
}
717821E101
// Function to pop an element from the stack
int pop() {
if (top == NULL) {
printf("Stack is empty!n");
return -1;
}
int data = top->data;
struct Node* temp = top;
top = top->next;
free(temp);
return data;
}
// Function to display the stack
void display() {
if (top == NULL) {
printf("Stack is empty!n");
return;
}
struct Node* temp = top;
printf("Stack: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("n");
}
int main() {
push(1);
push(2);
push(3);
display();
printf("Popped %d from the stackn", pop());
printf("Popped %d from the stackn", pop());
717821E101
display();
push(4);
push(5);
display();
printf("Popped %d from the stackn", pop());
printf("Popped %d from the stackn", pop());
printf("Popped %d from the stackn", pop());
display();
return 0;
}
OUTPUT:
RESULT:
Thus the program to implement Stack data structure using Singly Linked List is compiled and
executed successfully.
717821E101
Ex. No: 2.4(b)
Queue using Singly Linked List
Date:
AIM:
To write a C program to implement the Queue Data Structure using Singly Linked List.
PSEUDO CODE:
• Create a Node structure with data and next pointer.
• Create front and rear pointers and initialize them to NULL.
• Implement functions to enqueue an element at rear and dequeue an element from front.
• Display the elements of the queue using front and rear pointers.
• Terminate the program.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
// Node structure
struct Node {
int data;
struct Node* next;
};
// Global pointers for front and rear of the queue
struct Node* front = NULL;
struct Node* rear = NULL;
// Function to create a new node
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void enqueue(int data) {
struct Node* newNode = createNode(data);
if (rear == NULL) {
front = rear = newNode;
return;
}
717821E101
rear->next = newNode;
rear = newNode;
}
int dequeue() {
if (front == NULL) {
printf("Queue is empty!n");
return -1;
}
int data = front->data;
struct Node* temp = front;
front = front->next;
free(temp);
if (front == NULL) {
rear = NULL;
}
return data;
}
// Function to display the queue
void display() {
if (front == NULL) {
printf("Queue is empty!n");
return;
}
struct Node* temp = front;
printf("Queue: ");
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
} printf("n");
}
int main() {
enqueue(1);
enqueue(2);
717821E101
enqueue(3);
display();
printf("Dequeued %d from the queuen", dequeue());
printf("Dequeued %d from the queuen", dequeue());
display();
enqueue(4);
enqueue(5);
display();
printf("Dequeued %d from the queuen", dequeue());
printf("Dequeued %d from the queuen", dequeue());
printf("Dequeued %d from the queuen", dequeue());
display();
return 0;
}
OUTPUT:
RESULT:
Thus the program to implement the Queue Data Structure using Singly Linked List is compiled and
executed successfully.
717821E101
Ex. No: 2.4(c)
Merge Two Sorted Linked Lists
Date:
AIM:
To write a C program to Merge Two Sorted Linked Lists.
PSEUDO CODE:
• Traverse both the linked lists simultaneously.
• Compare the data of the nodes of the two linked lists.
• If the data of the node in the first linked list is smaller, append it to the merged linked list and move
to the next node in the first linked list.
• If the data of the node in the second linked list is smaller, append it to the merged linked list and
move to the next node in the second linked list.
• Repeat steps 3 and 4 until the end of either linked list is reached, and append the remaining nodes to
the merged linked list.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
struct Node* mergeLists(struct Node* head1, struct Node* head2) {
if (head1 == NULL)
return head2;
else if (head2 == NULL)
return head1;
if (head1->data < head2->data) {
head1->next = mergeLists(head1->next, head2);
return head1;
} else {
717821E101
head2->next = mergeLists(head1, head2->next);
return head2;
}
}
void display(struct Node* head) {
if (head == NULL) {
printf("List is empty!n");
return;
}
printf("List: ");
while (head != NULL) {
printf("%d ", head->data);
head = head->next;
}
printf("n");
}
int main() {
int N, M;
printf("Enter the size of first list: ");
scanf("%d", &N);
struct Node* head1 = NULL;
struct Node* tail1 = NULL;
printf("Enter the elements of first list in ascending order: ");
for (int i = 0; i < N; i++) {
int data;
scanf("%d", &data);
struct Node* newNode = createNode(data);
if (head1 == NULL)
head1 = tail1 = newNode;
else {
tail1->next = newNode;
tail1 = newNode;
}
717821E101
}
printf("Enter the size of second list: ");
scanf("%d", &M);
struct Node* head2 = NULL;
struct Node* tail2 = NULL;
printf("Enter the elements of second list in ascending order: ");
for (int i = 0; i < M; i++) {
int data;
scanf("%d", &data);
struct Node* newNode = createNode(data);
if (head2 == NULL)
head2 = tail2 = newNode;
else {
tail2->next = newNode;
tail2 = newNode;
}
}
struct Node* mergedHead = mergeLists(head1, head2);
display(mergedHead);
return 0;
}
OUTPUT:
RESULT:
Thus the program to Merge Two Sorted Linked Lists is compiled and executed successfully.
PREPARATION 30
LAB PERFORMANCE 30
REPORT 40
TOTAL 100
INITIAL OF FACULTY

More Related Content

Similar to Lab-2.4 101.pdf

C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked listSourav Gayen
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
C code on linked list #include stdio.h #include stdlib.h.pdf
 C code on linked list #include stdio.h #include stdlib.h.pdf C code on linked list #include stdio.h #include stdlib.h.pdf
C code on linked list #include stdio.h #include stdlib.h.pdfdeepua8
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfhadpadrrajeshh
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfanton291
 
Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdfsudhirchourasia86
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxtienlivick
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfrohit219406
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxteyaj1
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfvishalateen
 
c++ Computational Complexity filling in the following three .pdf
c++ Computational Complexity filling in the  following three .pdfc++ Computational Complexity filling in the  following three .pdf
c++ Computational Complexity filling in the following three .pdfamitbagga0808
 
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.pdffootstatus
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfarchgeetsenterprises
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdffortmdu
 
Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03Nasir Mehmood
 

Similar to Lab-2.4 101.pdf (20)

C program to insert a node in doubly linked list
C program to insert a node in doubly linked listC program to insert a node in doubly linked list
C program to insert a node in doubly linked list
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
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
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdf
 
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdfA)B) C++ program to create a Complete Binary tree from its Lin.pdf
A)B) C++ program to create a Complete Binary tree from its Lin.pdf
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
Program to insert in a sorted list #includestdio.h#include.pdf
 Program to insert in a sorted list #includestdio.h#include.pdf Program to insert in a sorted list #includestdio.h#include.pdf
Program to insert in a sorted list #includestdio.h#include.pdf
 
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docxcoding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
 
Data Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdfData Structures in C++I am really new to C++, so links are really .pdf
Data Structures in C++I am really new to C++, so links are really .pdf
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdfInspect the class declaration for a doubly-linked list node in Node-h-.pdf
Inspect the class declaration for a doubly-linked list node in Node-h-.pdf
 
c++ Computational Complexity filling in the following three .pdf
c++ Computational Complexity filling in the  following three .pdfc++ Computational Complexity filling in the  following three .pdf
c++ Computational Complexity filling in the following three .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
 
hi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdfhi i have to write a java program involving link lists. i have a pro.pdf
hi i have to write a java program involving link lists. i have a pro.pdf
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
Singly Linked List
Singly Linked ListSingly Linked List
Singly Linked List
 
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdfAssignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
Assignment isPage 349-350 #4 and #5 Use the Linked List lab.pdf
 
Data structures cs301 power point slides lecture 03
Data structures   cs301 power point slides lecture 03Data structures   cs301 power point slides lecture 03
Data structures cs301 power point slides lecture 03
 

Recently uploaded

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
 
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一F La
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...Boston Institute of Analytics
 
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
 
SWOT Analysis Slides Powerpoint Template.pptx
SWOT Analysis Slides Powerpoint Template.pptxSWOT Analysis Slides Powerpoint Template.pptx
SWOT Analysis Slides Powerpoint Template.pptxviniciusperissetr
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 217djon017
 
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxMike Bennett
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queensdataanalyticsqueen03
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Seán Kennedy
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
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
 
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
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...GQ Research
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsVICTOR MAESTRE RAMIREZ
 
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degreeyuu sss
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Colleen Farrelly
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Seán Kennedy
 

Recently uploaded (20)

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
 
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一
办理(UC毕业证书)英国坎特伯雷大学毕业证成绩单原版一比一
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
 
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...
 
SWOT Analysis Slides Powerpoint Template.pptx
SWOT Analysis Slides Powerpoint Template.pptxSWOT Analysis Slides Powerpoint Template.pptx
SWOT Analysis Slides Powerpoint Template.pptx
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2
 
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
办美国加州大学伯克利分校毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptx
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queens
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
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)
 
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
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
Biometric Authentication: The Evolution, Applications, Benefits and Challenge...
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business Professionals
 
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024
 
Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...
 

Lab-2.4 101.pdf

  • 1. 717821E101 QUESTIONS 2.4: a) Write a C program to implement the Stack Data Structure using Singly Linked List. b) Write a C program to implement the Queue Data Structure using Singly Linked List. c) Given two sorted linked lists consisting of N and M nodes respectively. The task is to merge both of the list(in-place) and return head of the merged list. Example 1: Input: N = 4, M = 3 Value N[] = {5,10,15,40} Value M[] = {2,3,20} Output: 2 3 5 10 15 20 40 Explanation: After merging the two linked lists, we have merged list as 2, 3, 5, 10, 15, 20, 40. Example 2: Input: N = 2, M = 2 Value N[] = {1,1} Value M[] = {2,4} Output:1 1 2 4 Explanation: After merging the given two linked list, we have 1, 1, 2, 4 as output.
  • 2. 717821E101 Ex. No: 2.4(a) Stack using Singly Linked List Date: AIM: To write a C program to implement the Stack data structure using Singly Linked List. PSEUDO CODE: Structure Node: data next top = null Function createNode(data): newNode = new Node newNode.data = data newNode.next = null return newNode Function push(data): newNode = createNode(data) newNode.next = top top = newNode Function pop(): if top is null: print "Stack is empty!" return -1 data = top.data temp = top top = top.next free(temp) return data Function display(): if top is null: print "Stack is empty!" return temp = top print "Stack: " while temp is not null: print temp.data temp = temp.next Main function:
  • 3. 717821E101 push(1) push(2) push(3) display() print "Popped", pop(), "from the stack" print "Popped", pop(), "from the stack" display() push(4) push(5) display() print "Popped", pop(), "from the stack" print "Popped", pop(), "from the stack" print "Popped", pop(), "from the stack" display() SOURCE CODE: #include <stdio.h> #include <stdlib.h> // Node structure struct Node { int data; struct Node* next; }; // Global pointer for top of stack struct Node* top = NULL; // Function to create a new node struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; } // Function to push an element onto the stack void push(int data) { struct Node* newNode = createNode(data); newNode->next = top; top = newNode; }
  • 4. 717821E101 // Function to pop an element from the stack int pop() { if (top == NULL) { printf("Stack is empty!n"); return -1; } int data = top->data; struct Node* temp = top; top = top->next; free(temp); return data; } // Function to display the stack void display() { if (top == NULL) { printf("Stack is empty!n"); return; } struct Node* temp = top; printf("Stack: "); while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("n"); } int main() { push(1); push(2); push(3); display(); printf("Popped %d from the stackn", pop()); printf("Popped %d from the stackn", pop());
  • 5. 717821E101 display(); push(4); push(5); display(); printf("Popped %d from the stackn", pop()); printf("Popped %d from the stackn", pop()); printf("Popped %d from the stackn", pop()); display(); return 0; } OUTPUT: RESULT: Thus the program to implement Stack data structure using Singly Linked List is compiled and executed successfully.
  • 6. 717821E101 Ex. No: 2.4(b) Queue using Singly Linked List Date: AIM: To write a C program to implement the Queue Data Structure using Singly Linked List. PSEUDO CODE: • Create a Node structure with data and next pointer. • Create front and rear pointers and initialize them to NULL. • Implement functions to enqueue an element at rear and dequeue an element from front. • Display the elements of the queue using front and rear pointers. • Terminate the program. SOURCE CODE: #include <stdio.h> #include <stdlib.h> // Node structure struct Node { int data; struct Node* next; }; // Global pointers for front and rear of the queue struct Node* front = NULL; struct Node* rear = NULL; // Function to create a new node struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; } void enqueue(int data) { struct Node* newNode = createNode(data); if (rear == NULL) { front = rear = newNode; return; }
  • 7. 717821E101 rear->next = newNode; rear = newNode; } int dequeue() { if (front == NULL) { printf("Queue is empty!n"); return -1; } int data = front->data; struct Node* temp = front; front = front->next; free(temp); if (front == NULL) { rear = NULL; } return data; } // Function to display the queue void display() { if (front == NULL) { printf("Queue is empty!n"); return; } struct Node* temp = front; printf("Queue: "); while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("n"); } int main() { enqueue(1); enqueue(2);
  • 8. 717821E101 enqueue(3); display(); printf("Dequeued %d from the queuen", dequeue()); printf("Dequeued %d from the queuen", dequeue()); display(); enqueue(4); enqueue(5); display(); printf("Dequeued %d from the queuen", dequeue()); printf("Dequeued %d from the queuen", dequeue()); printf("Dequeued %d from the queuen", dequeue()); display(); return 0; } OUTPUT: RESULT: Thus the program to implement the Queue Data Structure using Singly Linked List is compiled and executed successfully.
  • 9. 717821E101 Ex. No: 2.4(c) Merge Two Sorted Linked Lists Date: AIM: To write a C program to Merge Two Sorted Linked Lists. PSEUDO CODE: • Traverse both the linked lists simultaneously. • Compare the data of the nodes of the two linked lists. • If the data of the node in the first linked list is smaller, append it to the merged linked list and move to the next node in the first linked list. • If the data of the node in the second linked list is smaller, append it to the merged linked list and move to the next node in the second linked list. • Repeat steps 3 and 4 until the end of either linked list is reached, and append the remaining nodes to the merged linked list. SOURCE CODE: #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = data; newNode->next = NULL; return newNode; } struct Node* mergeLists(struct Node* head1, struct Node* head2) { if (head1 == NULL) return head2; else if (head2 == NULL) return head1; if (head1->data < head2->data) { head1->next = mergeLists(head1->next, head2); return head1; } else {
  • 10. 717821E101 head2->next = mergeLists(head1, head2->next); return head2; } } void display(struct Node* head) { if (head == NULL) { printf("List is empty!n"); return; } printf("List: "); while (head != NULL) { printf("%d ", head->data); head = head->next; } printf("n"); } int main() { int N, M; printf("Enter the size of first list: "); scanf("%d", &N); struct Node* head1 = NULL; struct Node* tail1 = NULL; printf("Enter the elements of first list in ascending order: "); for (int i = 0; i < N; i++) { int data; scanf("%d", &data); struct Node* newNode = createNode(data); if (head1 == NULL) head1 = tail1 = newNode; else { tail1->next = newNode; tail1 = newNode; }
  • 11. 717821E101 } printf("Enter the size of second list: "); scanf("%d", &M); struct Node* head2 = NULL; struct Node* tail2 = NULL; printf("Enter the elements of second list in ascending order: "); for (int i = 0; i < M; i++) { int data; scanf("%d", &data); struct Node* newNode = createNode(data); if (head2 == NULL) head2 = tail2 = newNode; else { tail2->next = newNode; tail2 = newNode; } } struct Node* mergedHead = mergeLists(head1, head2); display(mergedHead); return 0; } OUTPUT: RESULT: Thus the program to Merge Two Sorted Linked Lists is compiled and executed successfully. PREPARATION 30 LAB PERFORMANCE 30 REPORT 40 TOTAL 100 INITIAL OF FACULTY