Part 2 For the Singly-Linked List class: #pragma once #include using namespace s
ID: 3814063 • Letter: P
Question
Part 2
For the Singly-Linked List class:
#pragma once
#include
using namespace std;
template class SLinkedList; // forward declaration to be used when declaring SNode
template
class SNode { // singly linked list node
private:
E elem; // linked list element value
SNode *next; // next item in the list
friend class SLinkedList; // provide SLinkedList access
};
template
class SLinkedList { // a singly linked list
public:
SLinkedList(); // empty list constructor
~SLinkedList(); // destructor
bool empty() const; // is list empty?
E& front(); // return front element
void addFront(const E& e); // add to front of list
void removeFront(); // remove front item list
int size() const; // list size
private:
SNode* head; // head of the list
int n; // number of items
};
template
SLinkedList::SLinkedList() // constructor
: head(NULL), n(0) { }
template
bool SLinkedList::empty() const // is list empty?
{
return head == NULL; // can also use return (n == 0);
}
template
E& SLinkedList::front() // return front element
{
if (empty()) throw length_error("empty list");
return head->elem;
}
template
SLinkedList::~SLinkedList() // destructor
{
while (!empty()) removeFront();
}
template
void SLinkedList::addFront(const E& e) { // add to front of list
SNode* v = new SNode; // create new node
v->elem = e; // store data
v->next = head; // head now follows v
head = v; // v is now the head
n++;
}
template
void SLinkedList::removeFront() { // remove front item
if (empty()) throw length_error("empty list");
SNode* old = head; // save current head
head = old->next; // skip over old head
delete old; // delete the old head
n--;
}
template
int SLinkedList::size() const { // list size
return n;
}
.
.
Add a method void SLinkedList::printReverse() that prints every element in the linked list in reverse (i.e., the first node is printed last). Do not use recursion but instead use a (array-based) stack as a temporary data structure.
.
.
Show only this new method:
#include "ArrayStack.h"
template
void SLinkedList::printReverse() { //cout elements in reverse
}
Explanation / Answer
/* Program to remove duplicates in an unsorted
linked list */
#include<bits/stdc++.h>
using namespace std;
/* A linked list node */
struct Node
{
int data;
struct Node *next;
};
// Utility function to create a new Node
struct Node *newNode(int data)
{
Node *temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
/* Function to remove duplicates from a
unsorted linked list */
void removeDuplicates(struct Node *start)
{
struct Node *ptr1, *ptr2, *dup;
ptr1 = start;
/* Pick elements one by one */
while (ptr1 != NULL && ptr1->next != NULL)
{
ptr2 = ptr1;
/* Compare the picked element with rest
of the elements */
while (ptr2->next != NULL)
{
/* If duplicate then delete it */
if (ptr1->data == ptr2->next->data)
{
/* sequence of steps is important here */
dup = ptr2->next;
ptr2->next = ptr2->next->next;
delete(dup);
}
else /* This is tricky */
ptr2 = ptr2->next;
}
ptr1 = ptr1->next;
}
}
/* Function to print nodes in a given linked list */
void printList(struct Node *node)
{
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
}
}
/* Druver program to test above function */
int main()
{
/* The constructed linked list is:
10->12->11->11->12->11->10*/
struct Node *start = newNode(10);
start->next = newNode(12);
start->next->next = newNode(11);
start->next->next->next = newNode(11);
start->next->next->next->next = newNode(12);
start->next->next->next->next->next =
newNode(11);
start->next->next->next->next->next->next =
newNode(10);
printf("Linked list before removing duplicates ");
printList(start);
removeDuplicates(start);
printf(" Linked list after removing duplicates ");
printList(start);
return 0;
}