Assignment 6 Lists CSSE111 Aser Data Structure
Assignment 6 Lists CSSE111 Aser Data Structure
1. Write a program that creates a list, inserts the integers 1 through 10, and
then iterates through the list twice, printing its contents.
#include <iostream>
struct Node {
int data;
Node* next;
};
class LinkedList {
public:
LinkedList() : head(nullptr) {}
void insert(int value) {
Node* node = new Node;
node->data = value;
node->next = head;
head = node;
}
void print() const {
for (Node* current = head; current != nullptr; current = current->next) {
std::cout << current->data << " ";
}
std::cout << std::endl;
}
private:
Node* head;
};
int main() {
LinkedList list;
for (int i = 1; i <= 10; ++i) {
list.insert(i);
}
for (int i = 0; i < 2; ++i) {
list.print();
}
return 0;
}
2. Write a program that creates two list L1 and L2, puts the integers 1 through
25 into L1, iterates through list L1 and copies its contents into list L2, and then
iterates through list L2 and prints its contents.
#include <iostream>
struct Node {
int data;
Node* next;
};
class LinkedList {
public:
LinkedList() : head(nullptr) {}
private:
Node* head;
};
int main() {
LinkedList L1;
for (int i = 1; i <= 25; ++i) {
L1.insert(i);
}
LinkedList L2;
L1.copyTo(L2);
L2.print();
return 0;
}
3.Write a C++ function as a member of the list class to insert an item after
specific item in the list and if not exist then print at the end of the list.
#include <iostream>
struct Node {
int data;
Node* next;
};
class LinkedList {
public:
LinkedList() : head(nullptr) {}
// If we reach here, it means the value was not found in the list
std::cout << "The value " << value << " was not found in the list. The
new value " << newValue << " was inserted at the end." << std::endl;
node->next = head;
head = node;
}
private:
Node* head;
};
int main() {
LinkedList L1;
for (int i = 1; i <= 5; ++i) {
L1.insert(i);
}
L1.insertAfter(3, 6);
L1.insertAfter(7, 8);
L1.print();
return 0;
}
4.Write a C++ function as a member of the list class to
insert an item in an ordered list.
#include <iostream>
struct Node {
int data;
Node* next;
};
class LinkedList {
public:
LinkedList() : head(nullptr) {}
// If we reach here, it means the value was not found in the list
std::cout << "The value " << value << " was not found in the list. The new
value " << newValue << " was inserted at the end." << std::endl;
node->next = head;
head = node;
}
node->next = current->next;
current->next = node;
}
private:
Node* head;
};
int main() {
LinkedList L1;
for (int i = 1; i <= 5; ++i) {
L1.insertInOrder(i);
}
L1.insertInOrder(0);
L1.insertInOrder(6);
L1.print();
return 0;
}
if (head == nullptr) {
return;
if (head->data == value) {
Node* temp = head;
head = head->next;
delete temp;
return;
if (current->next->data == value) {
current->next = current->next->next;
delete temp;
return;
current = current->next;
std::cout << "The value " << value << " was not found in the list." <<
std::endl;
}
Implement the following tasks for the linked list:
1.Write isEmpty() function to check for empty list, and makeEmpty()
function to make the linked list empty.
1.isEmpty() function
The isEmpty() function checks if the linked list is empty. If the head node is nullptr, it
means the list is empty.
2.makeEmpty() function
The makeEmpty() function makes the linked list empty. It does this by setting the head
node to nullptr.
void makeEmpty() {
while (!isEmpty()) {
deleteItem(head->data);
}
}
3.Adding the following code to the main() function to demonstrate the use of these
functions:
int main() {
LinkedList list;
return 0;
}
When you run this program, it will print the following output:
class LinkedList {
// ... (same as before)
public:
Node* search(int key) const {
Node* current = head;
while (current != nullptr) {
if (current->data == key) {
return current;
}
current = current->next;
}
return nullptr;
}
};
To use this search function, you can call it on a LinkedList object with the key to search
for. For example:
int main() {
LinkedList list;
return 0;
}
When you run this program, it will print the following output:
class LinkedList {
// ... (same as before)
public:
Node* search(int key) const {
Node* current = head;
while (current != nullptr) {
if (current->data == key) {
return current;
}
current = current->next;
}
return nullptr;
}
};
Copy one linked list to another
void copy(Node* head, LinkedList& newList) {
while (head != nullptr) {
newList.insert(head->data);
head = head->next;
}
}
Modify the main function to demonstrate the copy functionality:
int main() {
LinkedList list1;
LinkedList list2;
return 0;
}
When you run this program, it will print the following output:
The item 20 was found in the second list.
class LinkedList {
// ... (same as before)
public:
void printLots(const LinkedList& P) const {
Node* currentL = head;
Node* currentP = P.head;
int pos = 1;
while (currentL != nullptr && currentP != nullptr) {
if (pos == currentP->data) {
std::cout << currentL->data << " ";
currentP = currentP->next;
}
currentL = currentL->next;
pos++;
}
std::cout << std::endl;
}
};
To use this function, you can call it with two linked lists as arguments. For example:
int main() {
LinkedList list1;
LinkedList list2;
// Print the elements in the first list that are in positions specified by the second list
list1.printLots(list2);
return 0;
}
When you run this program, it will print the following output:
10 20 30 40 50
5. Given two sorted lists, L1 and L2, write a procedure to compute L1 ∩ L2.
In python:
def intersect_sorted_lists(L1, L2):
intersection = []
i=j=0
while i < len(L1) and j < len(L2):
if L1[i] < L2[j]:
i += 1
elif L1[i] > L2[j]:
j += 1
else: # L1[i] == L2[j]
intersection.append(L1[i])
i += 1
j += 1
return intersection
Here is an example of how to use this function:
L1 = [1, 3, 5, 7, 9]
L2 = [2, 3, 4, 5, 6]
print(intersect_sorted_lists(L1, L2)) # Output: [3, 5]
6. Given two sorted lists, L1 and L2, write a procedure to compute L1 ∪ L2.
7. Assume that a singly linked list is implemented with a header node, but no
tail node, and that it maintains only a pointer to the header node. Write a class
that includes methods to:
class LinkedList:
def __init__(self):
self.header = ListNode(None)
self.size = 0
def get_size(self):
return self.size
def print_list(self):
node = self.header.next
while node is not None:
print(node.value, end=" ")
node = node.next
print()
class ListNode:
def __init__(self, value):
self.value = value
self.next = None
This class represents a linked list with a header node. It also includes a
nested ListNode class to represent the nodes of the linked list. The LinkedList class has
the following methods:
• __init__: Initializes the linked list.
• add: Adds a value to the linked list if it is not already present.
• contains: Checks if a value is contained in the linked list.
• get_size: Returns the size of the linked list.
• print_list: Prints the linked list.
L = LinkedList()
L.add(5)
L.add(3)
L.add(7)
L.add(9)
L.add(5)
print("Size of the linked list:", L.get_size()) # Output: Size of the linked list: 5
L.print_list() # Output: 3 5 7 9
print("Does the linked list contain 7?", L.contains(7)) # Output: Does the linked list contain 7?
True
print("Does the linked list contain 11?", L.contains(11)) # Output: Does the linked list contain
11? False
def reverse(self):
prev = None
current = self.header.next
while current is not None:
next_node = current.next
current.next = prev
prev = current
current = next_node
self.header.next = prev
def copy(self):
new_list = LinkedList()
node = self.header.next
while node is not None:
new_list.add(node.value)
node = node.next
return new_list
def is_circular(self):
slow = self.header.next
fast = self.header.next
while fast is not None and fast.next is not None:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
def delete(self):
self.header.next = None
self.size = 0
def remove_duplicates(self):
if self.get_size() == 0:
return
current = self.header.next
while current.next is not None:
if current.value == current.next.value:
current.next = current.next.next
else:
current = current.next