C++ Linked List Implementation
Motivation
As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation.
Problem Statement
For this assignment, you will implement a linked list class using pointers and object-oriented programming. Although the C++ STL (Standard Template Library) offers a linked list implementation, you must implement this program "from scratch" and cannot simply utilize the existing STL offerings (<list> or <forward_list>).
Your linked list will be designed to contain signed integers of type int.
Required Classes
You must implement the classes shown below (as well as the exact
member functions that are listed).
Note: It is okay to add additional functions or variables as
desired. You cannot add extra parameters to the functions that are
listed.
class Node { public: int val; // the value that this node stores Node *next; // a pointer to the next node in the list // you can add constructors or other functionality if you find it useful or necessary }
Note: Node is being used akin to a struct (with public member variables). This is intentional so that you can easily modify the member variables from within the Linked_List class.
class Linked_List { private: unsigned int length; // the number of nodes contained in the list Node *head; // a pointer to the first node in the list // anything else you need... public: int get_length(); // note: there is no set_length(unsigned int) (the reasoning should be intuitive) void print(); // output a list of all integers contained within the list void clear(); // delete the entire list (remove all nodes and reset length to 0) unsigned int push_front(int); // insert a new value at the front of the list (returns the new length of the list) unsigned int push_back(int); // insert a new value at the back of the list (returns the new length of the list) unsigned int insert(int val, unsigned int index); // insert a new value in the list at the specified index (returns the new length of the list) void sort_ascending(); // sort the nodes in ascending order. You must implement the recursive Merge Sort algorithm // Note: it's okay if sort_ascending() calls a recursive private function to perform the sorting. void sort_descending(); // sort the nodes in descending order // you can add extra member variables or functions as desired }
Note that the sort_ascending() function must be implemented using the recursive Merge Sort algorithm (Links to an external site.). sort_descending() can utilize Merge Sort or a different algorithm (see extra credit).
You are also required to implement a function that counts the number of prime numbers within a Linked_List. This can be written as part of the Linked_List class or in some other class. For our purposes, a negative number is never considered to be prime.
Application Description
Once you have implemented the fundamental building blocks of a linked list you will use this functionality to build a simple application. Implement a program to replicate the following behavior:
Note: All of the program functionality must be implemented using your linked list. You should not be storing or copying the user input into an array, vector, or other types of data structures.
Please enter a number: 146 Do you want another num (y or n): y Enter a number: 30 Do you want another num (y or n): y Enter a number: 73 Do you want another num (y or n): y Enter a number: 10 Do you want another num (y or n): y Enter a number: -31 Do you want another num (y or n): n Sort ascending or descending (a or d)? a Your linked list is: -31 10 30 73 146 You have 1 prime number(s) in your list. Do you want to do this again (y or n)? n
Other Program Requirements
Implementation Details
For this assignment you have additional freedom to design your own implementation. There are certain baseline requirements as specified in this document. However, any additional member functions and member variables are up to you, as long as the spirit of the assignment is followed.
Here are both the problem solution>>
Here is the code for merge sort and linked list class and node class>>
#include<iostream>
using namespace std;
// Node structure
struct node
{
int data;
node *next; //pointer that points to the next
node
};
//create node function
node* newNodeImpl(int d)
{
struct node *temporary = new node;
temporary->data = d;
temporary->next = NULL;// return temporary as
node
return temporary;
}
// Function to add data to the list
node* AddDataToList(node *tailNode, int data)
{
struct node *newnode;
newnode = newNodeImpl(data);
if(tailNode == NULL)
{
tailNode = newnode; // check if
tail is not null
}
else
{
tailNode->next = newnode;
tailNode = tailNode->next;//
Shift the pointer to the next node
}
return tailNode;
}
node* MergeSort(node* h0, node* h1)
{
node *t0 = new node;
node *t1 = new node;
node *temporary = new node;
if(h0 == NULL)// if list is empty firstNode list
return h1;
if(h1 == NULL)//secondNode list check
return h0;
t0 = h0;
//loop to merge the data
while (h1 != NULL)
{
t1 = h1;
h1 = h1->next;
t1->next = NULL;
if(h0->data >
t1->data)
{
t1->next =
h0;
h0 = t1;
t0 = h0;
continue;
}
// go through the firstNode
list
flag:
if(t0->next == NULL)
{
t0->next =
t1;
t0 =
t0->next;
}
else if((t0->next)->data
<= t1->data)
{
t0 =
t0->next;
goto flag;
}
else
{
temporary =
t0->next;
t0->next =
t1;
t1->next =
temporary;
}
}
// return the headNode element of the sorted
list
return h0;
}
// implementation of the merge sort
void MergeSortSort(node **headNode)
{
node *firstNode = new node;
node *secondNode = new node;
node *temporary = new node;
firstNode = *headNode;
temporary = *headNode;
if(firstNode == NULL || firstNode->next ==
NULL)
{
return;
}
else
{
//break the list into two
parts
while(firstNode->next !=
NULL)
{
firstNode =
firstNode->next;
if(firstNode->next != NULL)
{
temporary = temporary->next;
firstNode = firstNode->next;
}
}
secondNode =
temporary->next;
temporary->next = NULL;
firstNode = *headNode;
}
// start the divide and conquor and recursion
MergeSortSort(&firstNode);
MergeSortSort(&secondNode);
//then merge the two parts
*headNode = MergeSort(firstNode, secondNode);
}
//Testing the methods
int main()
{
int n, i, num;
struct node *headNode = new node;
struct node *tailNode = new node;
headNode = NULL;
tailNode = NULL;
cout<<"\nEnter the total number of numbers
:";
cin>>n;
// Now create the linkedlist
for(i = 0; i < n; i++)
{
cout<<"Enter elements of the
list one by one: "<<i+1<<": ";
cin>>num;
tailNode = AddDataToList(tailNode,
num);
if(headNode == NULL)
headNode =
tailNode;
}
MergeSortSort(&headNode);
// Printing the sorted data.
cout<<"\nAfter sorting Data: ";
while(headNode != NULL)
{
cout<<"
"<<headNode->data;
headNode=headNode->next;
}
return 0;
}
Here is the code snippet>>
Here is the code output>>
Here is the prime number finder fnction>>
bool isPrimeOrNot(int number){
bool flagPointer=true;
for(int i = 2; i <= number / 2; i++) {
if(number % i == 0) {
flagPointer = false;
break;
}
}
return flagPointer;
}
<<Do comment for any queries>>
C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use...
****find_last_node.c
#include <stdio.h>
#include "linked_list.h"
int main(){
struct node *linked_list = NULL;
linked_list = add_to_list(linked_list, 5,
'a');
linked_list = add_to_list(linked_list, 10, 'b');
linked_list = add_to_list(linked_list, 4, 'c');
linked_list = add_to_list(linked_list, 10, 'd');
linked_list = add_to_list(linked_list, 5, 'e');
linked_list = add_to_list(linked_list, 7, 'f');
linked_list = add_to_list(linked_list, 5, 'g');
linked_list = add_to_list(linked_list, 3, 'h');
int search_number;
printf("Enter number you want to search for:");
scanf("%d", &search_number);
struct node *last_node = find_last(linked_list,
search_number);
if (last_node != NULL)
{
printf("Node found: value = %d and...
I need this in C++. This is all
one question
Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...
Linked Lists implementation on Lists Executive Summary: Linked list implementation on lists includes two parts: Data part - stores an element of the list Next part- stores link/pointer to next element You are asked to implement Lists through Linked Lists to perform basic ADT functions. Project Objective: in completing this project, you will Enhance your ability to understand Lists Familiar with the idea of linked list Enable yourself to perform Linked Lists programming skills Due Dates and Honor: This project...
c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node { T data;//data field Node * next;//link field Node(T data) { this->data = data; } }; template<class T> class linked_list{ private: Node<T> *head,*current; public: linked_list(){//constructor, empty linked list head = NULL; current = NULL; } ~linked_list(){ current = head; while(current != NULL) { ...
Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions become private in the...
Please use C++
CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...
You are required to write the following functions using this class:
class Doubly_linked_list // Use a class Doubly_linked_list to represent an object
{
public:
// constructor initialize the nextPtr
Doubly_linked_list()
{
prevPtr = 0; // point to null at the beginning
nextPtr = 0; // point to null at the beginning
}
// get a number
int GetNum()
{
return number;
}
// set a number
void SetNum(int num)
{
number = num;
}
// get the prev pointer
Doubly_linked_list ...
Write a C++implementation of a doubly linked list class using a template class representation for a node and using pointers.In its public API provide functions to insert,find, delete, get size and get position of an element. Write C++ code to show those functions work as expected. (MUST COMPILE AND RUN)
Part I: Create a doubly linked circular list class named LinkedItemList that implements the following interface: /** * An ordered list of items. */ public interface ItemList<E> { /** * Append an item to the end of the list * * @param item – item to be appended */ public void append(E item); /** * Insert an item at a specified index position * * @param item – item to be...
Using C++ language, Design and implement a class representing a doubly linked list. The class must have the following requirements: The linked list and the nodes must be implemented as a C++ templates The list must be generic – it should not implement arithmetic/logic functions. (template class) It must include a destructor and a copy constructor It must include methods to insert at the front and at the back of the list It must include a method to return the...