Class 7

1 / 57

# Class 7 - PowerPoint PPT Presentation

Class 7. Objectives. Differentiate between a static data structure and a dynamic data structure Create a dynamic data structure class using a linked list Describe how nodes are inserted and remove from a linked list. What is a linked list?.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Class 7' - dacia

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Class 7

Objectives
• Differentiate between a static data structure and a dynamic data structure
• Create a dynamic data structure class using a linked list
• Describe how nodes are inserted and remove from a linked list
• It is a collection of items created dynamically (at run-time)
• Each item (node) is connected through a pointer
• Linked lists are differentiated from an array which is created at compile time and is accessed via a subscript
• The elements in a linked list are accessed via a pointer
• A linked list node consists of a data portion and at least one pointer

25

Null

3. The programmer can then place data in the data portion of the node

4. The programmer should then set nextPtr to Null or connect it to another node if there is one

Data

nextPtr

At run-time

• A node (consisting of a data portion and a pointer) is created
• It’s address is placed in a pointer

firstptr

• It’s address is placed in a pointer

Existing node

Data

nextPtr

25

Null

currentPtr

45

firstptr

3. The programmer can then place data in the data portion of the node

The two nodes need to be connected

• Should the node with the 45 be placed before or after the node with the 25?

Data

nextPtr

25

Null

currentPtr

45

firstptr

• To the left would be a stack

Data

nextPtr

25

Null

currentPtr

45

firstptr

To add the new node to the end of the linked list means connecting the pointer in the node with the 25 to the beginning of the node with the 45 and setting the node with the 45 pointer to Null

Data

nextPtr

25

Null

currentPtr

45

firstptr

To add the new node to the end of the linked list means connecting the pointer in the node with the 25 to the beginning of the node with the 45 and setting the node with the 45 pointer to Null

Data

nextPtr

25

currentPtr

45

Null

firstptr

To add the new node to the beginning of the linked list means setting the “45” node’s nextPtr to point to the “25” node and then setting firstptr the value of currentPtr so that it points to the “45” node and then

Data

nextPtr

25

Null

currentPtr

45

firstptr

// Specification file for the List class

#ifndef NUMBERLIST_H

#define NUMBERLIST_H

typedef int ListItemType;

class List

{ private:

struct ListNode

{

ListItemType value;

struct ListNode *next;

};

ListNode *find(int index) const;

public:

List() // Constructor

List(const List & )

{ head = NULL; size =0; }

~List(); // Destructor

void appendNode(ListItemType);

void insert(int , const ListItemType &);

void remove(int index);

void retrieve(int , const ListItemType &);

bool isEmpty() const;

int getLength() const;

}; #endif

List::List(const List& aList)

: size(aList.size)

{

head = NULL; // original list is empty

else

{ // copy first node

// copy rest of list

ListNode *newPtr = head; // new list pointer

// newPtr points to last node in new list

// origPtr points to nodes in original list

origPtr != NULL;

origPtr = origPtr->next)

{ newPtr->next = new ListNode;

newPtr = newPtr->next;

newPtr->item = origPtr->item;

} // end for

newPtr->next = NULL;

} // end if

} // end copy constructor

List::~List()

{

while (!isEmpty())

remove(1);

} // end destructor

bool List::isEmpty() const

{

return size == 0;

} // end isEmpty

int List::getLength() const

{

return size;

} // end getLength

List::ListNode *List::find(int index) const

{

if ( (index < 1) || (index > getLength()) )

return NULL;

else // count from the beginning of the list.

for (int skip = 1; skip < index; ++skip)

cur = cur->next;

return cur;

} // end if

} // end find

void List::retrieve(int index,

ListItemType& dataItem) const

{

if ( (index < 1) || (index > getLength()) ) cout <<

"ListIndexOutOfRangeException : retrieve index out of range");

else

{ // get pointer to node, then data in node

ListNode *cur = find(index);

dataItem = cur->item;

} // end if

} // end

void List::insert(int index, const ListItemType& newItem)

{

int newLength = getLength() + 1;

if ( (index < 1) || (index > newLength) )

cout << "ListIndexOutOfRangeException: insert index out of range");

else

{ ListNode *newPtr = new ListNode;

size = newLength;

newPtr->item = newItem;

// attach new node to list

if (index == 1)

{ // insert new node at beginning of list

}

else

{ ListNode *prev = find(index-1);

// insert new node after node

// to which prev points

newPtr->next = prev->next;

prev->next = newPtr;

} // end if

} // end if

} // end insert

void List::remove(int index)

{

ListNode *cur;

if ( (index < 1) || (index > getLength()) )

cout <<

"ListIndexOutOfRangeException: remove index out of range");

else

{ --size;

if (index == 1)

{ // delete the first node from the list

cur = head; // save pointer to node

}

else

{ ListNode *prev = find(index - 1);

// delete the node after the node to which prev points

cur = prev->next; // save pointer to node

prev->next = cur->next;

} // end if

// return node to system

cur->next = NULL;

delete cur;

cur = NULL;

} // end if

} // end remove

// Saves a linked list's data in a text file of

// fileName is a string that names an external text

// file to be created

ofstream outFile(fileName);

// traverse the list to the end, writing each item

for (Node *cur = head; cur != NULL; cur = cur->next)

outFile << cur->item << endl;

outFile.close();

// Assertion: The text file contains the linked

// list's data in its original order.

// Specification file for the LinkedList class

template <class T>

{

private:

// Declare a structure for the list

struct ListNode

{

T value;

struct ListNode *next;

};

public:

void appendNode(T);

void insertNode(T);

void deleteNode(T);

void displayList();

};

#endif

// Implementation file for the LinkedList class

#include <iostream> // For cout and NULL

using namespace std;template <class T>

{

ListNode *newNode, *nodePtr;

// Allocate a new node & store num

newNode = new ListNode;

newNode->value = num;

newNode->next = NULL;

// If there are no nodes in the list

// make newNode the first node

else // Otherwise, insert newNode at end

{

// Initialize nodePtr to head of list

// Find the last node in the list

while (nodePtr->next)

nodePtr = nodePtr->next;

// Insert newNode as the last node

nodePtr->next = newNode;

}

}

{

ListNode *nodePtr;

while (nodePtr)

{

cout << nodePtr->value << endl;

nodePtr = nodePtr->next;

}

}

{

ListNode *newNode, *nodePtr, *previousNode = NULL;

// Allocate a new node & store num

newNode = new ListNode;

newNode->value = num;

// If there are no nodes in the list

// make newNode the first node

newNode->next = NULL;

}

else

{

previousNode = NULL;

while (nodePtr != NULL && nodePtr->value < num)

{

previousNode = nodePtr;

nodePtr = nodePtr->next;

}

if (previousNode == NULL)

{

newNode->next = nodePtr;

}

else // Otherwise, insert it after the prev. node.

{

previousNode->next = newNode;

newNode->next = nodePtr;

}

}

}

template <class T>

{

ListNode *nodePtr, *previousNode;

// If the list is empty, do nothing.

return;

// Determine if the first node is the one.

{

}

else

{

// Initialize nodePtr to head of list

// Skip all nodes whose value member is

// not equal to num.

while (nodePtr != NULL && nodePtr->value != num)

{

previousNode = nodePtr;

nodePtr = nodePtr->next;

}

// If nodePtr is not at the end of the list,

// link the previous node to the node after

// nodePtr, then delete nodePtr.

if (nodePtr)

{

previousNode->next = nodePtr->next;

delete nodePtr;

}

}

}

NumberList::~NumberList()

{

ListNode *nodePtr, *nextNode;

while (nodePtr != NULL)

{

nextNode = nodePtr->next;

delete nodePtr;

nodePtr = nextNode;

}

}

template <class T> class List; // a forward decl.

template<class T>class ListNode {

friend class List< T >; // make List a friend

public:

ListNode( const T & ); // constructor

T getData() const; // return data in the node

private:

T data; // data

ListNode< T > *nextPtr; // next node in the list

};

// Constructor

template<classT>

ListNode< T >::ListNode( const T &info )

: data( info ), nextPtr( 0 ) { }

// Return a copy of the data in the node

template< class T >

T ListNode< T >::getData() const { return data; }

#endif

Create a List class to do the following
• Create a new node
• Insert a node into the list, front and back
• Delete a node from the list, front and back
• Display the contents of the data portion of each node, until the end of the list

template< class T >class List {public: List(); // constructor ~List(); // destructor void insertAtFront( const T & ); void insertAtBack( const T & ); bool removeFromFront( T & ); bool removeFromBack( T & ); bool isEmpty() const; void print() const;private: ListNode< T > *firstPtr; // pointer to first node ListNode< T > *lastPtr; // pointer to last node

// PrivateUtility function to allocate a new node

ListNode< T > *getNewNode( const T & );

};

// Default constructor

template< class T >List< T >::List() : firstPtr( 0 ), lastPtr( 0 ) { }

// Is the List empty?

template< class T >

bool List< T >::isEmpty() const

{ return firstPtr == 0; }

// Return a pointer to a newly allocated node

template< class T >

ListNode< T > *List< T >:: getNewNode( const T &value )

{ ListNode< T > *ptr = new ListNode< T >( value ); assert( ptr != 0 ); return ptr; }

// Display the contents of the Listtemplate< class NODETYPE >void List< T >::print() const

{ if ( isEmpty() ) {cout << "The list is empty\n\n"; return; }

ListNode< T > *currentPtr = firstPtr;cout << "The list is: ";

while ( currentPtr != 0 ) { cout << currentPtr->data << ‘ ‘; currentPtr = currentPtr->nextPtr; }

}

// Destructor// FOR THE WHOLE LINKED LISTtemplate< class T >List< T >::~List()

{ if ( !isEmpty() ) { // List is not empty cout << "Destroying nodes ...\n";

ListNode< T > *currentPtr = firstPtr, *tempPtr;

while ( currentPtr != 0 ) { tempPtr = currentPtr; cout << tempPtr->data << '\n'; currentPtr = currentPtr->nextPtr; delete tempPtr; }

}

cout << "All nodes destroyed\n\n";

}

// Insert a node at the front of the listtemplate< class T >void List< T >::insertAtFront( const T &value )

{ListNode< T > *newPtr = getNewNode( value );

if ( isEmpty() ) // List is empty firstPtr = lastPtr = newPtr; else { // List is not empty

newPtr->nextPtr = firstPtr;

firstPtr = newPtr; }

}

// Insert a node at the back of the listtemplate< class T >

void List< T >::insertAtBack( const T &value )

{ ListNode< T > *newPtr = getNewNode( value );

if ( isEmpty() ) // List is empty

firstPtr = lastPtr = newPtr;

else { // List is not empty

lastPtr->nextPtr = newPtr; lastPtr = newPtr; }}

// Delete a node from the front of the listtemplate< class T >bool List< T >::removeFromFront( T &value ){ if ( isEmpty() ) // List is empty return false; // delete unsuccessful else { ListNode<T > *tempPtr = firstPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else firstPtr = firstPtr->nextPtr; value = tempPtr->data; // data being removed delete tempPtr; return true; // delete successful }}

// Delete a node from the back of the listtemplate< class T >bool List< T >::removeFromBack( T &value ){ if ( isEmpty() ) return false; // delete unsuccessful else { ListNode< T > *tempPtr = lastPtr; if ( firstPtr == lastPtr ) firstPtr = lastPtr = 0; else { ListNode< T > *currentPtr = firstPtr; while ( currentPtr->nextPtr != lastPtr ) currentPtr = currentPtr->nextPtr; lastPtr = currentPtr; currentPtr->nextPtr = 0; }

value = tempPtr->data;

delete tempPtr;

return true; // delete successful

}

}

STL list class
• Is a doubly linked list
• Can insert at front or back
• Contains the following member functions:
• back
• list.back() returns a reference to the last element in the list
• erase
• list.erase(iter); causes the list element pointed to by the iterator iter to be removed
• list.erase(firstIter, lastIter); causes all of the list elements from firstIter to lastIter to be removed
• empty
• list.empty() returns true if the list is empty; otherswise returns false
STL list class
• end
• Iter = list.end(); returns a bidirectional iterator to the end of the list
• front
• list.front(); returns a reference to the first element of the list
• insert
• list.insert(iter,x); inserts an element with the value x just before the element pointed to by iter
• pop_back
• list.pop_back(); removes the last element of the list
• pop_front
• list.pop_front(); removes the first element of the list
STL list class
• push_back
• list.push_back(x); inserts element with value x at the end of the list
• push_front
• list.push_front(x); inserts element with value x at the beginning of the list
• reverse
• List.reverse(); reverses the elements in the list
Using the STL list class

#include <list>

#include <iostream>

using namespace std;

int main()

{list<int> myList;

list<int>::interator iter;

for (int x =0; x < 50; x+=5)

myList.push_back(x);

for (iter = myList.begin(); iter!=myList.end(); iter++)

cout << *iter;

myList.reverse();

}