0% found this document useful (0 votes)
18 views5 pages

Ap 3

Advanced Programming

Uploaded by

bansalaman2419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Ap 3

Advanced Programming

Uploaded by

bansalaman2419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 3
Student Name: Ankit Vashisth UID: 22BCS13378
Branch: BE-CSE Section/Group: KPIT-901-B
Semester:5th Date of Performance:14/8/24
Subject Name: Advanced Programming Lab-1 Subject Code: 22CSP-314

1. Aim: To implement the concept of Linked List.

2. Objective:

• Understand the basic structure and components of a Linked List.


• Implement various operations such as insertion, deletion, and traversal on a Linked List.
• Explore the differences between singly, doubly, and circular linked lists.
• Analyze the advantages and disadvantages of Linked Lists compared to other data
structures like arrays.
• Apply Linked List concepts to solve practical problems in data management and memory
allocation.

3. Implementation:

a) Problem 1:

Given the pointer to the head node of a linked list, change the next pointers of the nodes so that
their order is reversed. The head pointer given may be null meaning that the initial list is empty.

b) Code:
#include <iostream> using
namespace std;

struct ListNode {
int val;
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING

ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};

ListNode* reverseList(ListNode* head) {


ListNode* prev = nullptr;
ListNode* current = head;
ListNode* next = nullptr;

while (current != nullptr) {


next = current->next;
current->next = prev;
prev = current; current
= next;
}

return prev;
}

void printList(ListNode* head) {


while (head != nullptr) {
cout << head->val << " -> ";
head = head->next;
}
cout << "nullptr" << endl;
}

int main() {
ListNode* head = new ListNode(1); head-
>next = new ListNode(2); head->next->next =
new ListNode(3); head->next->next->next =
new ListNode(4);

cout << "Original Linked List: ";


printList(head);
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING

ListNode* reversedHead = reverseList(head);

cout << "Reversed Linked List: ";


printList(reversedHead);

while (reversedHead != nullptr) {


ListNode* temp = reversedHead;
reversedHead = reversedHead->next;
delete temp;
}

return 0;
}
c) Output:

a) Problem 2:
To merge two Given Linked List and display them in a sorted manner.

b) Code:
#include <iostream> using
namespace std; struct
ListNode {
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING

ListNode* dummy = new ListNode(0);


ListNode* current = dummy;

while (l1 != nullptr && l2 != nullptr)


{ if (l1->val <= l2->val) {
current->next = l1; l1 = l1->next;
} else {
current->next = l2;
l2 = l2->next;

}
current = current->next;
} if (l1 != nullptr)
{ current->next =
l1;
} else { current-
>next = l2;
}
ListNode* mergedList = dummy-
>next; delete dummy; return
mergedList;
} void printList(ListNode* head)
{ while (head != nullptr) {
cout << head->val << " -> ";
head = head->next;
}
cout << "nullptr" << endl;
} int
main() {
ListNode* l1 = new ListNode(1);
l1->next = new ListNode(3); l1-
>next->next = new ListNode(5);

ListNode* l2 = new ListNode(2); l2->next =


new ListNode(4); l2->next->next = new
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING

ListNode(6); ListNode* mergedList =


mergeTwoLists(l1, l2); cout << "Merged
Linked List: "; printList(mergedList); while
(mergedList != nullptr) { ListNode* temp =
mergedList; mergedList = mergedList->next;
delete temp;
}

return 0;
}

c) Output:

4. Learning Outcomes:

• Demonstrate the ability to create and manipulate Linked Lists in a programming


environment.
• Effectively perform insertion, deletion, and traversal operations on Linked Lists.
• Differentiate between various types of Linked Lists: singly, doubly, and circular.
• Evaluate the efficiency of Linked Lists in terms of memory usage and performance.
• Apply Linked List techniques to develop solutions for complex data management
problems.

You might also like