Explain the importance of pointers

Assignment Help C/C++ Programming
Reference no: EM13936838

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 T>
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 T>
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<T>*: The node that will reference the new node
// created in this function.
void add(T, Node<T>*);

// Finds the first node containing the specified value.
// parameter T: The value to find in the list.
Node<T>* 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<T>* _front;
// The last element of the list
Node<T>* _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<int>* list = new LinkedList<int>;
// 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<int>* nodeWith20 = list->find(20);
list->add(25, nodeWith20);
displayInfo(list);

// Size: 5, list: 10 -> 15 -> 20 -> 25 -> 30 -> x
Node<int>* 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

Reference no: EM13936838

Questions Cloud

What factors should sido consider before choosing a supplier : Which supplier should Sido choose? Show all calculations. What other factors should Sido consider before choosing a supplier?
Graded for technical content and from a grammar perspective : Write about Wal-Mart. It will be graded for technical content and from a grammar perspective. The paper should be approximately 2,000 words in length. References need to be cited throughout the paper and in the reference section
Is this a strand of dna or rna : If DNA and a base were inserted at the beginning of the 5' end of the molecule, how would that affect protein synthesis and the resultant protein? (Hint: think about the code), What would happen to the reading frame if three bases were inserted/del..
Two network security software tools : They must be of a type listed on page 22. Other types are not acceptable unless explicitly agreed by the module leader. c. They must be agreed with your module leader. The means of reaching this agreement are described below.
Explain the importance of pointers : 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.:
What are the criteria for good primers in a pcr reaction : What are the criteria for "good" primers in a PCR reaction? Describe how you would use site-directed mutagenesis to change a BamHI restriction site into an EcoRI site.
Factors that influence buying behavior : Factors that influence buying behavior - Cultural, social, personal and psychological. Include consumers buying decision process.
Enthusiasm and perseverance in gaining new skills : Enthusiasm and perseverance in gaining new skills and knowledge over the years, makes me a suitable candidate for astrophysics. I look forward to the challenges and the moment when most of the things
Write an explanatory essay - the monks tale : For this activity, you'll write an explanatory essay examining the ways Chaucer's "The Monk's Tale" reflected what was seen as sinful or illegal at the time of its writing

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Create program that uses functions and reference parameters

Create program that uses functions and reference parameters, and asks user for the outside temperature.

  Write a program using vectors and iterators

Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles

  Write the code required to analyse and display the data

Calculate and store the average for each row and column. Determine and store the values for the Average Map.

  Write a webservices application

Write a webservices application that does a simple four function calculator

  Iimplement a client-server of the game

Iimplement a client-server version of the rock-paper-scissors-lizard-Spock game.

  Model-view-controller

Explain Model-View-Controller paradigm

  Design a nested program

How many levels of nesting are there in this design?

  Convert celsius temperatures to fahrenheit temperatures

Write a C++ program that converts Celsius Temperatures to Fahrenheit Temperatures.

  Evaluate and output the value in the given base

Write C program that will input two values from the user that are a Value and a Base with which you will evaluate and output the Value in the given Base.

  Design a base class shape with virtual functions

Design a base class shape with virtual functions

  Implementation of classes

Implementation of classes Chart and BarChart. Class barChart chould display a simple textual representation of the data

  Technical paper: memory management

Technical Paper: Memory Management, The intent of this paper is to provide you with an in depth knowledge of how memory is used in executing, your programs and its critical support for applications.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd