List as an Abstract Data Type

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Advertisements

1 Linked List (II) Ying Wu Electrical Engineering & Computer Science Northwestern University EECS 230 Lectures Series.
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Dynamic Objects. COMP104 Dynamic Objects / Slide 2 Memory Management * Static Memory Allocation n Memory is allocated at compiling time * Dynamic Memory.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class List { public: List(); // constructor List(const.
Classes with dynamic members. int x; void f() { x=10; } main() { x=0; f(); } ‘Class’ matters! int x; void f() { int x; x=10; } main() { x=0; f(); } class.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
Review of pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Concept of lists and array implementations of lists.
List as an Abstract Data Type. struct Node{ public: int data; Node* next; }; typedef Node* Nodeptr; class list { public: list(); // constructor list(const.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
Abstract Data Type: List (optional, not required).
Stacks and Queues.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Stacks and Queues. 2 struct Node{ double data; Node* next; }; class List { public: List(); // constructor List(const List& list); // copy constructor.
Classes with dynamic objects List as a (dynamic) class.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Pointers OVERVIEW.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
CSCE 210 Data Structures and Algorithms
Programming Circular Linked List.
CS505 Data Structures and Algorithms
Linked Lists Chapter 6 Section 6.4 – 6.6
CC 215 Data Structures Stack ADT
CISC181 Introduction to Computer Science Dr
Chapter 4 Linked Lists.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Chapter 4 Linked Lists
LinkedList Class.
Pointers and Dynamic Variables
Linked Lists Chapter 4.
Summary on linked lists
Chapter 16-2 Linked Structures
Linked Lists.
as an abstract data structure and
Chapter 4 Linked Lists.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Lists, Stacks and Queues
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
Dynamic Objects.
Linked Lists.
Linked Lists.
Classes with dynamic members
Lists CMSC 202, Version 4/02.
Chapter 3 Lists, Stacks, and Queues
CS 144 Advanced C++ Programming April 30 Class Meeting
ICOM 4015 Advanced Programming
Linked Lists.
21 Data Structures.
Stacks and Queues. 2 struct Node{ double data; Node* next; }; class List { public: List(); // constructor List(const List& list); // copy constructor.
Data Structures & Programming
Presentation transcript:

List as an Abstract Data Type

struct Node{ public: int data; Node* next; }; class List { List(); // constructor List(const list& list1); // copy constructor ~List(); // destructor bool empty() const; // boolean function int headElement() const; // access functions void addHead(int newdata); // add to the head void delHead(); // delete the head int length() const; // utility function void print() const; // output private: Node* head;

How to use it int i(0); Int j(10); Int k(j); void main(){ List L; // constructor called automatically here for L L.print(); { } L.addHead(30); L.print(); { 30 } L.addHead(13); L.print(); { 13 30 } L.addHead(40); L.print(); { 40 13 30 } L.addHead(50); L.print(); { 50 40 13 30 } List N(L); N.print(); { 50 40 13 30 } List R; R.print(); { } if(R.empty()) cout << "List R empty" << endl; L.delHead(); if(L.empty()) cout << "List L empty" << endl; else{ cout << "List L contains " << L.length() << " nodes" << endl; cout << "Head element of list L is: " << L.headElement() << endl; } } // destructor called automatically here for L

Motivation list using static array list using dynamic array int myArray[1000]; int n; We have to decide (to oversize) in advance the size of the array (list) list using dynamic array int* myArray; cin >> n; myArray = new int[n]; We allocate an array (list) of any specified size while the program is running linked-list (dynamic size) size = ?? The list is dynamic. It can grow and shrink to any size.

Using a static array

Or int head[DIM]; int size; cont int DIM=10000; struct Node{ public: int data; Node* next; }; class List { List(); // constructor List(const List& list1); // copy constructor ~List(); // destructor bool empty() const; // boolean function int headElement() const; // access functions void addHead(int newdata); // add to the head void delHead(); // delete the head int length() const; // utility function void print() const; // output private: int head[10000]; int size; Or int head[DIM]; int size; cont int DIM=10000;

Implementation Some simple member functions: List::List(){ head = NULL;  size = 0; } bool List::empty() const{ if(head==NULL) return true; else return false; int List::headElement() const { if(head != NULL) return head->data; else{ cout << "error: trying to find head of empty list" << endl; exit(1); If (size==0) return true; else return false; If (size!=0) return head[0]; else …;

(explicitly defined) copy constructor: List::List(const list& list1) { head = NULL; Node* cur = list1.head; while(cur != NULL) { // addEnd(cur->data); addHead(cur->data); // inverse list order cur = cur->next; } If (list1.size!=0) for (int i=0; i<list1.size; i++) head[i]=list1.head[i];

 Nothing here as it’s static. Destructor: deallocation function List::~List(){ Node* cur; while(head!=NULL){ cur = head; head = head->next; delete cur; }  Nothing here as it’s static.

Adding an element to the head: void List::addHead(int newdata){ Node* newPtr = new Node; newPtr->data = newdata; newPtr->next = head; head = newPtr; } If (size<10000) { for (int i=size; i>=;i--) head[i+1]=head[i]; head[0]=newdata; size++; }

Deleting the head: void List::delHead(){ if(head != NULL){ Node* cur = head; head = head->next; delete cur; } for (int i=1; i<size;i++) head[i-1]=head[i]; size--;

Print the list: void List::print() const{ cout << "{"; Node* cur = head; while(cur != NULL){ cout << cur->data << " "; cur = cur->next; } cout << "}" << endl; for (int i=0; i<size;i++) cout << head[i];

Computing the number of elements of a given list: int List::length() const{ int n=0; Node* cur = head; while(cur != NULL){ n++; cur = cur->next; } return n;  return size;

Using a dynamic array

struct Node{ public: int data; Node* next; }; class List { List(); // constructor List(const List& list1); // copy constructor ~list(); // destructor bool empty() const; // boolean function int headElement() const; // access functions void addHead(int newdata); // add to the head void delHead(); // delete the head int length() const; // utility function void print() const; // output private: int* head; int size;

Some simple member functions: Implementation Some simple member functions: List::List(){ … } bool List::empty() const{ int List::headElement() const {

(explicitly defined) copy constructor: List::List(const List& list1) { }

Other functions … List::~List(){ delete[] head; }

void main(){ List L; // constructor called automatically here for L L.print(); { } L.addHead(30); L.print(); { 30 } L.addHead(13); L.print(); { 13 30 } L.addHead(40); L.print(); { 40 13 30 } L.addHead(50); L.print(); { 50 40 13 30 } List N(L); N.print(); { 50 40 13 30 } List R; R.print(); { } if(R.empty()) cout << "List R empty" << endl; L.delHead(); if(L.empty()) cout << "List L empty" << endl; else{ cout << "List L contains " << L.length() << " nodes" << endl; cout << "Head element of list L is: " << L.headElement() << endl; } } // destructor called automatically here for L Conclusion: the usage is the same!!! No matter it is linked list, static array or dynamic array!

List as a template

template<typename T> class List { public: List(); // constructor List(const List& list1); // copy constructor ~list(); // destructor bool empty() const; // boolean function int headElement() const; // access functions void addHead(T newdata); // add to the head void delHead(); // delete the head int length() const; // utility function void print() const; // output private: T* head; int size; };

Some simple member functions: Implementation Some simple member functions: template<typename T> List::List(){ head=NULL; size=0; } bool List::empty() const{ T List::headElement() const {

Other functions … template<typename T> List::~List(){ delete T[] head; }