0% found this document useful (0 votes)
53 views7 pages

Step 1: Create Class For Nodetype: Linked List Implementation - Mohsin Abbas

The document discusses the implementation of a linked list data structure in C++. It involves 4 main steps: 1) Creating a Node class to store data and pointer to next node 2) Creating an Iterator class to iterate through the linked list 3) Creating an abstract LinkedListType class with common linked list functions 4) Creating an UnorderedLinkedList class that inherits from LinkedListType and implements linked list functions like insertion, deletion, and searching.

Uploaded by

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

Step 1: Create Class For Nodetype: Linked List Implementation - Mohsin Abbas

The document discusses the implementation of a linked list data structure in C++. It involves 4 main steps: 1) Creating a Node class to store data and pointer to next node 2) Creating an Iterator class to iterate through the linked list 3) Creating an abstract LinkedListType class with common linked list functions 4) Creating an UnorderedLinkedList class that inherits from LinkedListType and implements linked list functions like insertion, deletion, and searching.

Uploaded by

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

Linked List Implementation – Mohsin Abbas

Step 1: Create class for NodeType


#pragma once
#include <iostream>
#include <string>
using namespace std;
template <class T>
class nodeType
{
public:
T info;
nodeType *link;
};
Step 2: Create a class for linkedListIterator:
An iterator is an object that produces each element of a container, such as a linked list, one element at a time.

#pragma once Type operator*()


#include "nodeType.h" {
return current->info;
template <class T> }
class linkedListIterator
{ linkedListIterator<T> operator++()
nodeType<T> *current; {
public: current = current->link;
return *this;
linkedListIterator() }
{
current = NULL; bool operator==(const
} linkedListIterator<T>&obj) const
{
linkedListIterator(nodeType<T> *ptr) return (current == obj.current);
{ }
current = ptr;
} bool operator!=(const
linkedListIterator<T>&obj) const
{
return (current != obj.current);
}
};

Page 1 of 7
Linked List Implementation – Mohsin Abbas

Step 3: Create Abstract Class linkedListType


This class specifies the members to implement the basic properties of a linkedlist.

#pragma once void print()


#include "nodeType.h"; {
#include "linkedListIterator.h"; nodeType<T> *current;
current = first;
template <class T> while (current != NULL)
class linkedListType {
{ cout << current->info
protected: << " ";
int count; current = current-
nodeType<T> *first; >link;
nodeType<T> *last; }
public: }
virtual void insertFirst(const T&
newItem) = 0; int length() const
virtual void insertLast(const T& {
newItem) = 0; return count;
virtual bool search(const T& }
searchItem) const = 0;
virtual void deleteNode(const T& T front()
deleteItem) = 0; {
return first->info;
bool isEmptyList() const }
{
return first == NULL; T back()
} {
return last->info;
linkedListType() }
{
first = NULL; linkedListIterator<T> begin()
last = NULL; {
count = 0; linkedListIterator<T>
} temp(first);
return temp;
void destroyList() }
{
nodeType<T> *temp; linkedListIterator<T> end()
while (first != NULL) {
{ linkedListIterator<T>
temp = first; temp(NULL);
first = first->link; return temp;
delete temp; }
}
last = NULL;
count = 0; ~linkedListType()
} {
destroyList();
void initializeList() }
{ };
destroyList();
}

Page 2 of 7
Linked List Implementation – Mohsin Abbas

Step 4: Create unorderedLinkedList


UML class diagram of the class unorderedLinkedList and the inheritance hierarchy

Page 3 of 7
Linked List Implementation – Mohsin Abbas

#pragma once
#include "nodeType.h"
#include "linkedListType.h"

template <class T>


class unsortedLinkedList :public linkedListType<T>
{
public:
void deleteNode(const T& deleteItem)
{
nodeType<T> *current;
nodeType<T> *trailCurrent;
bool found = false;

if (isEmptyList())
{
cout << "The list is empty" << endl;
}

else if (first->info == deleteItem)


{
current = first;
first = first->link;
if (first == NULL)
last = NULL;
delete current;
count--;
}

else
{
trailCurrent = first;
current = first->link;

while (1)
{
if (current->info == deleteItem)
{
found = true;
break;
}

else if (current->link == NULL)


break;

else
{
trailCurrent = current;
current = current->link;
}
}

if (found)
{
trailCurrent->link = current->link;
if (last == current)
last = trailCurrent;
delete current;
count--;
}
else
{
cout << "Item not found in the list" << endl;
}

}
}

Page 4 of 7
Linked List Implementation – Mohsin Abbas

void insertLast(const T& newItem)


{
nodeType<T> *newNode;
newNode = new nodeType<T>;
if (first == NULL)
{
newNode->info = newItem;
newNode->link = NULL;
first = newNode;
last = newNode;
}
else
{
newNode->info = newItem;
last->link = newNode;
last = newNode;
last->link = NULL;
}
count++;
}

void insertFirst(const T& newItem)


{
nodeType<T> *newNode;
newNode = new nodeType<T>;
if (first == NULL)
{
newNode->info = newItem;
newNode->link = NULL;
first = newNode;
last = newNode;
}
else
{
newNode->info = newItem;
newNode->link = first;
first = newNode;
}
count++;
}

bool search(const T& searchItem) const


{
bool found = false;
nodeType<T> *temp;
temp = first;
while (!found)
{
if (temp->info == searchItem)
{
found = true;
break;
}
else
temp = temp->link;

if (temp->link == NULL)
{
if (temp->info == searchItem)
{
found = true;
break;
}
}
}
return found;
}
};

Page 5 of 7
Linked List Implementation – Mohsin Abbas

Step 5: Create orderedLinkedList


void insert(const T& newItem)
{
nodeType<T> *current;
nodeType<T> *trailCurrent;
nodeType<T> *newNode;

newNode = new nodeType<T>;


newNode->info = newItem;
newNode->link = NULL;

bool found = false;

if (isEmptyList())
{
first = newNode;
last = newNode;
count++;
}

else if (count == 1)
{
if (newItem > first->info)
{
newNode->link = first;
first = newNode;
}
else
{
first->link = newNode;
}

count++;
}
else
{
if (newItem > first->info)
{
newNode->link = first;
first = newNode;
count++;
}
else
{
current = first->link;
trailCurrent = first;
while (1)
{
if (newItem > current->info)
found = true;

else
{
trailCurrent = trailCurrent->link;
current = current->link;
}
if (found||current==NULL)
{
newNode->link = current;
trailCurrent->link = newNode;
count++;
break;
}
}
}
}
}

Page 6 of 7
Linked List Implementation – Mohsin Abbas

Page 7 of 7

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy