CSC-335 Data Structures and Algorithms
CSC-335 Data Structures and Algorithms
(Chapter 6 Lists)
The content of this power point lecture has been originally created by Christos Kolonis and modified by Dr. Ahmad R Hadaegh
1
Chapter Contents
6.1 List as an ADT
Chapter Objectives
To study list as an ADT
Build a static-array-based implementation of lists and note strengths, weaknesses Build a dynamic-array-based implementation of lists, noting strengths and weaknesses See need for destructor, copy constructor, assignment methods
Dean's list
etc
4
Properties of Lists
Can have a single element Can have no elements
Basic Operations
Construct an empty list Determine whether or not empty Insert an element into the list Delete an element from the list Traverse (iterate through) the list to
Modify Output Search for a specific value Copy or save Rearrange
Implementation involves
Defining data members Defining function members from design phase
Implementing Operations
Constructor
Static array allocated at compile time
56 is inserted
Empty
Check if size == 1
Traverse
Use a loop from 0th element to size 1
Insert
Shift elements to right of insertion point
25 is deleted
Delete
Shift elements back
For specific implementation of our class we simply fill in desired type for Some_Specific_Type
10
11
Dynamic-Allocation for List Class Now possible to specify different sized lists
cin >> maxListSize; List aList1 (maxListSize); List aList2 (500);
12
14
15
An assignment operator
16
Future Improvements to Our List Class Problem 1: Array used has fixed capacity Solution:
If larger array needed during program execution Allocate, and then copy smaller array to the new one
17
dynamic lists
Those that change frequently Those with many insertions and deletions
So
18
Linked List
For the array-based implementation: 1. First element is at location 0 2. Successor of item at location i is at location i + 1 3. End is at location size 1
Fix: 1) Remove requires that that list elements be stored in consecutive location
2) But then need a "link" that connects each element to its successor
19
Linked Lists !!
Linked List
Linked list nodes contain
Data part stores an element of the list
Next part stores link/pointer to next element (when no next element, null value)
20
Traverse
Initialize a variable ptr to point to first node
Operations: Insertion
predptr
newptr
20
Insertion
To insert 20 after 17 Need address of item before point of insertion
predptr points to the node containing 17 Get a new node pointed to by newptr and store 20 in it
Set the next pointer of this new node equal to the next pointer in its predecessor, thus making it point to its successor. Reset the next pointer of its predecessor to point to this new node
23
Operations: Insertion
Note: insertion also works at end of list
pointer member of new node set to null
24
Operations: Deletion
predptr
ptr
To free space
Do a bypass operation:
Set the next pointer in the predecessor to point to the successor of the node (node 26) to be deleted Deallocate the node being deleted.
25
Linked Lists - Advantages Access any item as long as external link to first item maintained Insert new item without shifting Delete existing item without shifting Can expand/contract as necessary
26
27
Linked List
Get a new node; set data part = value next part = null_value If list is empty Set first to point to new node. Else
Traverse list to find last node Set next part of last node to point to new node.
29
Access the data and next part of node (*ptr).data and (*ptr).next ptr->data and ptr->next
30
or
};
This class declaration will be placed inside another class declaration for List
The data members data and next of struct Node will be public inside the class
will accessible to the member and friend functions
A linked list will be characterized by: A pointer to the first node in the list. Each node contains a pointer to the next node in the list The last node contains a null pointer
As a variation first may be a structure also contain a count of the elements in the list
32
Class List
class Node { public: DataType data; Node* next; }; //----------------------------------------Class List { protcted: Node* top; public: List(){top = NULL;} List( List& L); // copy constructor destroy(); ~List(){destroy();}
33
Constructor
Make first a null pointer and set mySize to 0
0
Destructor
Nodes are dynamically allocated by new Default destructor will not specify the delete All the nodes from that point on would be "marooned memory" A destructor must be explicitly implemented to do the delete
34
deep Copy
Shallow Copy