Ask C/C++ Expert


Home >> C/C++

The purpose of this assignment is to practice pointers. To this end, we will study a data structure, Linked Lists. When studying arrays, we found some limitations on arrays:

* Arrays have fixed size. Once an array is created, the size cannot be changed.

* Assuming an array has to be sorted, inserting a new element required shifting, in the worst case, n elements, where n is the number of elements in the array, i.e.:

The figure below shows an array. A request is received to insert 1:

5
10
15
18
20

Since array needs to be sorted, all elements must be shifted to make room for the new element:

5
10
15
18
20

Once the first spot is cleared, then 1 can be inserted:

1
5
10
15
18
20

* Removing an element from an array having n elements requires, in the worst case, shifting n-1 elements assuming the array is sorted.

A Linked List overcomes these limitations. Conceptually, a Linked List is a collection of nodes where each nodes points to the next node in the list. The following figure depicts a linked list:

In order to insert a new element to the linked list, it is not necessary shifting the elements. It suffices to rearrange a few pointers. For instance, suppose that we need to insert 12 into the above linked list:

To insert 12 into the list, we need have to change the pointer of 10 to make it point to 12 and make 12 point to 15:

The shape is irrelevant. The above linked list represents the same linked list as:

Therefore, inserting an element into a linked list requires changing two pointers provided it is known the preceding node.

From the above figures, we can identify the classes needed to implement a linked list. A class Node is needed. A node contains the value and a pointer to the next node. In order to make this class reusable, it will be implemented as a template:

#pragma once
template
class Node
{
public:

// Creates a new instance of class Node.
// parameter nodeValue: The value of the node.
Node(T nodeValue): value(nodeValue), next(NULL) { }

// Creates a new instance of class Node.
// parameter nodeValue: The value of the node.
// parameter nextNode: The node this instance points to.
Node(T nodeValue, Node* nextNode): value(nodeValue), next(nextNode) { }

// Disposes the instance.
~Node(void) { }

// The node referenced by this instance.
Node* next;

// The value stored in this instance.
T value;
};

The constructors show an alternate syntax to initialize data members. For instance, the second constructor is equivalent to:

Node(T nodeValue, Node* nextNode)
{
value = nodeValue;
next = nextNode;
}

The following source code shows the interface and the implementation of one of the methods of class LinkedList:

#pragma once

#include "Node.h"

template
class LinkedList
{
public:

// Creates a new instance of class LinkedList.
LinkedList(void): _front(NULL), _back(NULL), _size(0) {};

// Disposes this instance.
~LinkedList(void) { }

// Adds the specified element to the front of the linked list.
// parameter T: The value of the node to be added.
void addToFront(T);

// Adds the specified element to the back of the linked list.
// parameter T: The value of the node to be added.
void addToBack(T);

// Adds the specified element after the specified node.
// parameter T: The value of the node to be added.
// parameter Node*: The node that will reference the new node
// created in this function.
void add(T, Node*);

// Finds the first node containing the specified value.
// parameter T: The value to find in the list.
Node* find(T);

// Removes the first node containing the specified list.
void remove(T);

// Returns the size of the linked list.
int getSize();

// Displays to the screen the all the elements of the list.
void displayList();

private:
// The first of the list
Node* _front;
// The last element of the list
Node* _back;
// The size of the list.
int _size;
};

Implement the rest of the functions in class LinkedList. The following main function displays some test cases and the expected output. Note the arrow syntax used to access members from an object. This syntax is used when objects are created using the new keyword.

int main()
{
LinkedList* list = new LinkedList;
// Size: 0, list: x
displayInfo(list);

// Size: 1, list: 10 -> x
list->addToFront(10);
displayInfo(list);

// Size: 2, list: 10 -> 20 -> x
list->addToBack(20);
displayInfo(list);

// Size: 3, list: 10 -> 20 -> 30 -> x
list->addToBack(30);
displayInfo(list);

// Size: 4, list: 10 -> 20 -> 25 -> 30 -> x
Node* nodeWith20 = list->find(20);
list->add(25, nodeWith20);
displayInfo(list);

// Size: 5, list: 10 -> 15 -> 20 -> 25 -> 30 -> x
Node* nodeWith10 = list->find(10);
list->add(15, nodeWith10);
displayInfo(list);

// Size: 4, list: 10 -> 15 -> 25 -> 30 -> x
list->remove(20);
displayInfo(list);

// Size: 3, list: 10 -> 15 -> 25 -> x
list->remove(30);
displayInfo(list);

system("pause");
return 0;
}

Attachment:- LinkedList.docx

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M91607721
  • Price:- $20

Priced at Now at $20, Verified Solution

Have any Question?


Related Questions in C/C++

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As