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

  What is an abstract data type

1. What is an Abstract Data Type (ADT) for? Illustrate answer with any particular ADT. 2. write out in pseudocode an algorithm to perform a depth-first search for a directed, unweighted graph.

  Print out contents of the vector

Write C++ program to provide the subsequent functionality - Ask users to enter 10 integer numbers and Print out contents of the vector.

  What is the estimated net operating income

What is the estimated net operating income for July, if the company always uses an estimated predetermined plantwide overhead rate of $7 per direct labor-hour?

  Generate a multiplication table

Write a C program (a pseudo code) that will generate a 5 X 5 multiplication table starting with the number of the users choice.

  Program prompts the user to input the height and the radius

Rearrange the statements so that the program prompts the user to input the height and the radius of the base of a cylinder and outputs the volume and surface area of the cylinder. Format the output to two decimal places.

  Perform ticket bookings for various flights

The development team of SoftSols Inc. analyzes the source code of the existing software and notes the following observations: The software contains a private class, named bookTickets, that contains the methods used to perform ticket bookings for va..

  Solution to the naur text-processing problem

Design and implement a solution to the Naur text-processing problem using the language specified by your instructor. Execute it against test data and record the number of faults you find and the cause of each fault

  Solution to the quadratic equation

The program should allow the user to input the particular integer coefficients of the quadratic equation and properly output either real or complex number solutions for the roots of the equation

  Write a c program that display the hexadecimal equivalent

Write a C program that will accept a hexadecimal number as input, and then display a menu that will permit any of the following operations to be carried out: (a) Display the hexadecimal equivalent of the one"s complement.

  Ask the user to enter the desired character

Write a very simple c program which will Ask the user to enter the desired character- Repeat the asking part until the user types a desired letter. For each even number of attempts

  Produce a top-level design for your program

You are required to code the program using the C++ Programming Language. Your program should be properly laid out and should be modular, making sure that software engineering aspects of modularity and reusability as fully considered.

  The counter each time it''s clicked

Write a simple script that contains a button and a counter in a div. The button's event handler should increment the counter each time it's clicked.

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