using C++: Close Lab 5: Practicing LinkedList The objective of this close lab is
ID: 3573213 • Letter: U
Question
using C++:
Close Lab 5: Practicing LinkedList
The objective of this close lab is to practice linked list.
Assignment:
Complete the definition of the template linklist class below,
template <typename T> class LinkList
{ private:
class Node
{
public:
T info;
Node *next;
};
typedef Node *nodePtr;
public:
LinkList(); //constructor
LinkList(const LinkList<T> & orig); //copy constructor
bool empty(); //determine if the LinkList is empty T Front(); // returns item at front of LinkList
T Back(); // returns item at back of LinkList
Void PrintSecond();//print the content of the second node
void Push_front(const T & item);
//add item at the begining of LinkList
void pop_back(); //remove the last item of the LinkList
// Search function to search a given value and return the count
// of the given value
_______________________________________________________
// Pop_front function to remove the first item of the LinkList.
___________________________________________________________
// push_back function to add an item at the end of the LinkList
____________________________________________________________
private: // node pointer which points to the front(first node)
____________________________________________________
};
Complete the implementation of the following member functions: Search(), Front(), Back(), PrintSecond(), Pop_front() and Push_back(), and then write a test driver to fully test your linklist.
Explanation / Answer
#include<iostream>
using namespace std;
template <typename T>
class LinkList
{
private:
class Node
{
public:
T info;
Node *next;
};
typedef Node *nodePtr;
public:
LinkList(); //constructor
LinkList(const LinkList<T> & orig); //copy constructor
bool empty(); //determine if the LinkList is empty
T Front(); // returns item at front of LinkList
T Back(); // returns item at back of LinkList
void PrintSecond();//print the content of the second node
void Push_front(const T & item);
//add item at the begining of LinkList
void pop_back(); //remove the last item of the LinkList
// Search function to search a given value and return the count
// of the given value
int search(T num);
// Pop_front function to remove the first item of the LinkList.
T Pop_front();
// push_back function to add an item at the end of the LinkList
void Push_back(T i);
//print all node
void Print();
private: // node pointer which points to the front(first node)
nodePtr front;
};
template <typename T>
LinkList<T>::LinkList()
{
front = NULL;
}
template <typename T>
LinkList<T>::LinkList(const LinkList<T> & orig) //copy constructor
{
front = orig.front;
}
template <typename T>
bool LinkList<T>::empty()//determine if the LinkList is empty
{
if (front == NULL)
return true;
else
return false;
}
template <typename T>
T LinkList<T>:: Front() // returns item at front of LinkList
{
return front->info;
}
template <typename T>
T LinkList<T>::Back() // returns item at front of LinkList
{
nodePtr cur = front;
while (cur->next != NULL)
cur = cur->next;
return cur->info;
}
template <typename T>
void LinkList<T>::PrintSecond()//print the content of the second node
{
if (front->next != NULL)
{
nodePtr cur = front->next;
cout<<"Second node = "<<cur->info<<endl;
}
}
//print all node
template <typename T>
void LinkList<T>::Print()//print the content of the second node
{
nodePtr cur = front;
int i = 1;
while (cur != NULL)
{
cout<<"node"<<i<<" = "<< cur->info<<endl;
cur = cur->next;
++i;
}
cout << endl;
}
template <typename T>
void LinkList<T>::Push_front(const T & item)
{
nodePtr cur;
if (front == NULL)
{
front = new Node;
front->info = item;
front->next = NULL;
}
else
{
cur = new Node;
cur ->info= item;
cur->next = front;
front = cur;
}
}
template <typename T>
void LinkList<T>::pop_back() //remove the last item of the LinkList
{
nodePtr cur = front,prev;
while (cur->next != NULL)
{
prev = cur;
cur = cur->next;
}
prev->next = NULL:
delete cur;
}
template <typename T>
int LinkList<T>::search(T num)
{
nodePtr cur = front;
int index = 1;
while (cur != NULL)
{
if (cur->info == num)
{
break;
}
cur = cur->next;
++index;
}
return index;
}
template <typename T>
// Pop_front function to remove the first item of the LinkList.
T LinkList<T>::Pop_front()
{
T i;
nodePtr cur = front;
front = front->next;
i = cur->info;
delete cur;
return i;
}
template <typename T>
// push_back function to add an item at the end of the LinkList
void LinkList<T>:: Push_back(T i)
{
nodePtr cur = front;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = new Node;
cur->next->info = i;
cur->next->next = NULL;
}
int main()
{
LinkList<int> list;
//push some items on stack
list.Push_front(3);
list.Push_front(5);
list.Push_front(2);
list.Push_front(9);
list.Push_front(10);
//printlist
list.Print();
//test copy constructor
cout << "Test copy constructor, copy list to list1" << endl;
LinkList<int> list1(list);
list1.Print();
//Search(), Front(), Back(), PrintSecond(), Pop_front() and Push_back(),
int i = 2;
cout<<"item "<<i<<" is found at position = "<<list1.search(i)<<endl;
cout<<"Fornt = "<<list1.Front()<<endl;
cout<<"back = "<<list1.Back()<<endl;
list1.PrintSecond();
cout<<"Item Popped at front = "<<list1.Pop_front()<<endl;
cout << "Item 8 is pushed at back " << endl;
list1.Push_back(8);
list1.Print();
}
-----------------------------------------------------------------------
output:
node2 = 9
node3 = 2
node4 = 5
node5 = 3
Test copy constructor, copy list to list1
node1 = 10
node2 = 9
node3 = 2
node4 = 5
node5 = 3
item 2 is found at position = 3
Fornt = 10
back = 3
Second node = 9
Item Popped at front = 10
Item 8 is pushed at back
node1 = 9
node2 = 2
node3 = 5
node4 = 3
node5 = 8
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.