0% found this document useful (0 votes)
16 views6 pages

Unsorted Linked List

The document describes the implementation of an unsorted linked list in C++, detailing the structure and methods for managing the list, including insertion, deletion, retrieval, and iteration. It includes a class definition for the linked list and provides example usage in the main function. Additionally, it outlines an exercise to merge two unsorted lists into a third list without duplicates.

Uploaded by

ibrahemshaar03
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)
16 views6 pages

Unsorted Linked List

The document describes the implementation of an unsorted linked list in C++, detailing the structure and methods for managing the list, including insertion, deletion, retrieval, and iteration. It includes a class definition for the linked list and provides example usage in the main function. Additionally, it outlines an exercise to merge two unsorted lists into a third list without duplicates.

Uploaded by

ibrahemshaar03
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/ 6

Lab Session No.

Unsorted Linked list

Unsorted Linked list


 Allocate memory for each new element dynamically
 Link the list elements together
 Use two pointers, currentPos and listData,
to mark the current position in the list and
the beginning of the list
 Use an integer variable, length, to store the
current length of the list.

Example:

#include <iostream>
using namespace std;

template<class ItemType>
struct NodeType {
ItemType info;
NodeType<ItemType>* next;
};

template<class ItemType>
class UnsortedType {
public:
UnsortedType(); // Constructor
~UnsortedType(); // Destructor
void MakeEmpty();
bool IsFull() const;
int LengthIs() const;
void RetrieveItem(ItemType& item, bool& found);
void InsertItem(ItemType item);
void DeleteItem(ItemType item);
void ResetList();
bool IsLastItem() const;
void GetNextItem(ItemType& item);

private:
int length;
NodeType<ItemType>* listData;
NodeType<ItemType>* currentPos;
};

// Constructor
template<class ItemType>
UnsortedType<ItemType>::UnsortedType() {
length = 0;
listData = NULL;
}

// Destructor
template<class ItemType>
UnsortedType<ItemType>::~UnsortedType() {
MakeEmpty();
}

// MakeEmpty: Empties the list


template<class ItemType>
void UnsortedType<ItemType>::MakeEmpty() {
NodeType<ItemType>* tempPtr;
while (listData != NULL) {
tempPtr = listData;
listData = listData->next;
delete tempPtr;
}
length = 0;
}

// IsFull: Checks if the list is full


template<class ItemType>
bool UnsortedType<ItemType>::IsFull() const {
NodeType<ItemType>* ptr;
ptr = new NodeType<ItemType>;
if (ptr == NULL)
return true;
else {
delete ptr;
return false;
}
}

// LengthIs: Returns the length of the list


template<class ItemType>
int UnsortedType<ItemType>::LengthIs() const {
return length;
}

// ResetList: Resets the current position for iteration


template<class ItemType>
void UnsortedType<ItemType>::ResetList() {
currentPos = listData;
}

// GetNextItem: Gets the next item in the list during iteration


template<class ItemType>
void UnsortedType<ItemType>::GetNextItem(ItemType& item) {
item = currentPos->info;
currentPos = currentPos->next;
}

// IsLastItem: Checks if the current item is the last in the list


template<class ItemType>
bool UnsortedType<ItemType>::IsLastItem() const {
return (currentPos == NULL);
}
// RetrieveItem: Retrieves an item from the list
template<class ItemType>
void UnsortedType<ItemType>::RetrieveItem(ItemType& item, bool& found) {
NodeType<ItemType>* location = listData;
found = false;

while (location != NULL && !found) {


if (item == location->info) {
found = true;
item = location->info;
} else {
location = location->next;
}
}
}

// InsertItem: Inserts an item into the list


template<class ItemType>
void UnsortedType<ItemType>::InsertItem(ItemType item) {
NodeType<ItemType>* location = new NodeType<ItemType>;
location->info = item;
location->next = listData;
listData = location;
length++;
}

// DeleteItem: Deletes an item from the list


template<class ItemType>
void UnsortedType<ItemType>::DeleteItem(ItemType item) {
NodeType<ItemType>* location = listData;
NodeType<ItemType>* tempLocation;

if (item == listData->info) {
tempLocation = location;
listData = listData->next; // delete the first node
} else {
while (!(item == (location->next)->info)) {
location = location->next;
}
tempLocation = location->next; // node to be deleted
location->next = (location->next)->next;
}
delete tempLocation;
length--;
}

int main() {
UnsortedType<int> list;

// Insert items into the list


list.InsertItem(5);
list.InsertItem(3);
list.InsertItem(8);
list.InsertItem(1);

// Retrieve an item
int item = 3;
bool found;
list.RetrieveItem(item, found);
if (found) {
cout << "Item " << item << " found in the list." << endl;
} else {
cout << "Item " << item << " not found in the list." << endl;
}

// Delete an item
list.DeleteItem(5);

// Display the list


list.ResetList();
cout << "List items: ";
while (!list.IsLastItem()) {
list.GetNextItem(item);
cout << item << " ";
}
cout << endl;

return 0;
}

Exercise1: Write a client function that merges two instances of the Unsorted List
ADT using the following specification.
template<class ItemType>
MergeLists(UnsortedType<ItemType> list1,
UnsortedType<ItemType> list2,
UnsortedType<ItemType>& result)
Function: Merges two unsorted lists into a third unsorted
list (no duplicates).
Preconditions: list1 and list2 have been initialized.
Postconditions: result is an unsorted list that contains all
of the items from list1 and list2

Solution:

You might also like