Docsity

Linked Lists Part B - Problems with Solutions | CECS 274, Assignments of Computer Science

Toggle sidebar
bg1
CHAPTER 4 -- PART B
LINKED LISTS
Problem: What if we create lots of dynamic variables at
execution time? How do we manage them all?
Solution: Create linked structures made up of dynamic
variables.
We will discuss two flavors of linked lists:
1. simple (or singly) linked lists
2. two-way (or doubly) linked lists
and two mix-ins for linked lists:
1. dummy nodes
2. head and tail pointers
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Linked Lists Part B - Problems with Solutions | CECS 274 and more Assignments Computer Science in PDF only on Docsity!

CHAPTER 4 -- PART B

LINKED LISTS

Problem: What if we create lots of dynamic variables at

execution time? How do we manage them all?

Solution: Create linked structures made up of dynamic

variables.

We will discuss two flavors of linked lists :

1. simple (or singly) linked lists

2. two-way (or doubly) linked lists

and two mix-ins for linked lists:

1. dummy nodes

2. head and tail pointers

A. Simple Linked Lists

Simple linked lists store dynamic variables in a single, one-

directional chain.

List L; data (^1) data 2 data 3

We want to create an ADT for simple linked lists with the

following operations:

 Create : Start a new list with nothing in it.

 Insert : Add a record to the list.

 Remove: Delete a record from the list.

 Retrieve : Find and return information from a record in the

list.

Creating the Empty List

Default constructor:

List::List() : size(0), head(NULL) { // end default constructor }

To instantiate a linked list:

List L;

To be initially empty , the list pointer head must have an initial value of

null.

Inserting into a Simple Linked List

Insertion has three steps:

1. Create a new element to add to the list

2. Find the record preceding the place it should go in the

list

3. Do the insertion

C++ code for insertion:

// returns a pointer to the desired node List::ListNode* List::find(int index) const { if ((index < 1) || (index > getLength())) return NULL; else { // count from the beginning of the list ListNode* cur = head; for (int skip = 1; skip < index; ++skip) cur = cur->next; return cur; } }

Deleting from a Simple Linked List

Deletion has two steps:

1. Find the record preceding the one to be deleted from the

list

2. Do the deletion

C++ code for deletion:

void List::remove(int index) throw(ListException) { ListNode* cur; if ((index < 1) || (index > getLength())) throw ListException( "ListException: remove index out of range"); else { --size; if (index == 1) { // delete the first node from the list cur = head; // save the pointer to node head = head->next; } 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; } // return node to system cur->next = NULL; delete cur; cur = NULL; } }

Getting Information from a Simple Linked List

Retrieve has two steps:

1. Find the record with the key you want

2. Return the data

C++ code for ListRetreive:

void List::retrieve(int index, ListItemType& dataItem) const throw(ListException) { if ((index < 1) || (index > getLength())) throw ListException( "ListException: retrieve index out of range"); else { // get pointer to node, then data in node ListNode* cur = find(index); dataItem = cur->item; } }

Assignment operator for a Simple Linked List

Similar to a copy constructor, an assignment operator should be

coded to prevent shallow copy:

List& List::operator=(const List& rhs) { // check for assignment to self if (this != &rhs) { // deallocate left-hand side while (!isEmpty()) remove(1); if (rhs.head == NULL) head = NULL; else { // copy first node head = new ListNode; assert(head != NULL); // check allocation head->item = rhs.head->item; // 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 for (ListNode* origPtr = rhs.head->next; origPtr != NULL; origPtr = origPtr->next) { newPtr->next = new ListNode; assert(newPtr->next != NULL); newPtr = newPtr->next; newPtr->item = origPtr->item; } newPtr->next = NULL; } size = rhs.size; // copy size of list } return *this; }

Destructor for a Simple Linked List

Space that has been allocated from the heap by the constructor

needs to be returned to the heap by the destructor. A memory

leak occurs when space is not returned to the heap.

List::~List() { while (!isEmpty()) remove(1); }

C++ code for insertion into a Simple Linked List with a

dummy node :

Default constructor:

List::List() : size(0) { head = new ListNode; // Create the Dummy Node head->next = NULL; // end default constructor } void List::insert(int index, ListItemType newItem) throw (ListException) { int newLength = getLength() + 1; if ((index < 1) || (index > newLength)) throw ListException( "ListException: insert index out of range"); else { // create new node and place newItem in it ListNode* newPtr = new ListNode; if (newPtr == NULL) throw ListException( "ListException: insert cannot allocate memory"); else { size = newLength; newPtr->item = newItem; ListNode* prev = find(index); // insert new node after node // to which prev points newPtr->next = prev->next; prev->next = newPtr; } } }

C. Using Head and Tail Pointers

Head and tail pointers can be used when it is desirable to have

fast access to both the front and back of a list.

Example: A simple linked list with a dummy node and head

and tail pointers

dummy List L;

The empty list looks like:

dummy List L;

Default constructor:

List::List() : size(0) { head = new ListNode; head->next = NULL; tail = head; }

E. Two-way Linked Lists

Two-way linked lists make it easier to move backward (in addition to

forward) in the list.

Two-way linked lists usually have a dummy node (called head ), with

head and tail pointers already built-in to the structure.

dummy data1 data2 (^) data Two_Way_Linked_List T; head

The empty list looks like:

dummy Two_Way_Linked_List T; head

typedef desired-type-of-list-item listItemType; class List { public: // constructors and destructor List(); // default constructor List(const List& aList); // copy constructor ~List(); // deletes all nodes in list // list operations List& operator=(const List& aList); bool isEmpty() const; int getLength() const; void insert(int index, ListItemType newItem) throw(ListException); void remove(int index) throw(ListException); void retrieve(int index, ListItemType& dataItem) const throw(ListException); private: struct ListNode { ListItemType item; ListNode* next; ListNode* precede; }; int size; // number of items in list ListNode* head; // pointer to linked list of items ListNode* find(int index) const; // returns a pointer to the // position-th node in the // linked list }; // Default constructor: List::List() : size(0) { head = new ListNode; head->next = head; head->precede = head; }