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

  1 implement the tronomino tiling algorithm your program

1. implement the tronomino tiling algorithm. your program should take an arbitrary input positive integer k in the

  Program reads a number and prints all of its binary digit

Write a program that reads a number and prints all of its binary digits. Print the remainder number % 2, thenreplace the number with number / 2. Keep going until the number is 0.

  Dimensional array of integers and fill it with data

Create a 2-by-3 two-dimensional array of integers and fill it with data. Loop through the array and locate the smallest value stored. Print out the smallest value as well as its row and column position in the array

  Creating a part number

Write C++ program that asks a user to enter a part number to order. The rules for creating a part number are as follows:

  Convert code c to c++

I don't know where to start and how I should go about doing this and am looking for some advice - the code just has to compile in g++, it does not need to be completely rewritten in C++.

  How does the choice affect the user of the class

Write a partial C++ class definition not implementation that contains the public interface of the Date class described in Exercise R9.2.

  Write the code to test the queue class

Write the code to test the queue class by determining if the following strings are char-by-char palindromes:

  Write a function which takes a c string

Write a function which takes a C string as an input and counts the number of non-alphabetic characters in the C string. Non-alphabetic characters include anything outside the ranges 'a' thru 'z' and 'A' thru 'Z'.

  Prepare a program that uses at least two functions

Prepare a program that uses at least two functions that can be called from your main.

  Write a program to decide an integer value is power of two

Write a program to decide whether an integer value is power of 2 or not. Your function should return true if the value is power of 2, otherwise return false.

  What can you say about the lift force on the airfoil

What would be one major and obviously visible change to the velocity and pressure coefficient curves if you were to redo this problem with the airfoil at a non-zero angle of attack?

  Terms of the difference

A typical password is about 8 characters long (and so can be stored in 8 bytes, or 64 bits). However, a typical key for encryption/decryption is much longer, and a key of 64 bits would not be considered secure. Explain this in terms of the differe..

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