Data Structures & Programming

Slides:



Advertisements
Similar presentations
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Chapter 3: Arrays, Linked Lists, and Recursion
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 Writing a Good Program 8. Elementary Data Structure.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
The List ADT Reading: Sections 3.2, 3.3, 3.5.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
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 CS 132 Spring 2008 Chapter 5 Linked Lists p
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Linked List, Stacks Queues
CSCE 210 Data Structures and Algorithms
Programming Circular Linked List.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
COMP 53 – Week Eight Linked Lists.
Popping Items Off a Stack Using a Function Lesson xx
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Doubly Linked List Review - We are writing this code
CSE 143 Linked Lists [Chapter , 8.8] 3/30/98.
Chapter 4 Linked Lists.
EEL 4854 IT Data Structures Linked Lists
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
The Bag and Sequence Classes with Linked Lists
LINKED LISTS CSCD Linked Lists.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists Chapter 4.
Chapter 16-2 Linked Structures
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Linked Lists.
Object Oriented Programming COP3330 / CGS5409
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 4 Linked Lists.
Chapter 18: Linked Lists.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
Linked Lists.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Doubly Linked List Implementation
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
CMSC 341 Lists 3.
CMSC 341 Lists 3.
Pointers & Dynamic Data Structures
Containers: Queue and List
Linked Lists.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Doubly Linked List Implementation
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 11 - Templates Outline Introduction Function Templates Overloading Function Templates Class Templates Class.
16 – Sequential Containers
21 Data Structures.
Data Structures & Programming
Presentation transcript:

Data Structures & Programming A Reminder on C++ Part 3 Golnar Sheikhshab

Singly Linked List (header file) class intSLinkedList { public: intSLinkedList(); ˜intSLinkedList(); bool empty() const; const int& front() const; void addFront(const int& e); void removeFront(); void print(); private: intSNode* head; }; class intSNode { private: int elem; intSNode* next; friend class intSLinkedList; };

Singly Linked List Implementation (source file) intSLinkedList::intSLinkedList() // constructor : head(NULL) { } bool intSLinkedList::empty() const // is list empty? { return head == NULL; } const int& SLinkedList::front() const // return front element { return head->elem; } intSLinkedList::~intSLinkedList() // destructor { while (!empty()) removeFront(); }

Singly Linked List Implementation (source file) void intSLinkedList::addFront(const int& e) { // add to front of list intSNode* v = new intSNode; // create new node v->elem = e; // store data v->next = head; // head now follows v head = v; // v is now the head }

Singly Linked List Implementation (source file) void intSLinkedList::removeFront() { // remove front item intSNode* old = head; // save current head head = old->next; // skip over old head delete old; // delete the old head }

Singly Linked List Implementation (source file) void intSLinkedList::print() { // print the list intSNod* v = head; // put a cursor in front of the list while (v!=Null){ // if the cursor is pointing to anything cout << v->elem << " "; // write what the cursor is pointing to v = v->next; // send the cursor forward } cout << endl; // go to the new line }

A source file with a main function #include <iostream> #include "intSLinkedList.h" using namespace std; int main(){ intSLinkedList int_sll; int_sll.addFront(5); int_sll.addFront(12); int_sll.addFront(6); int_sll.print(); }

How to compile and run g++ -c intSLinkedList.cpp will generate an object file for us: intSLinkedList.o g++ -o test_intSLinkedList.o test_intSLinkedList.cpp intSLinkedList.o will generate test_intSLinkedList.o that is executable and links the intSLinkedList.o object file as well. ./test_intSLinkedList.o outputs the following line 6 12 5

A Generic Singly Linked List class We put the following line before the class definition template <typename E> We'll change all int data types in the list to E We'll put the implementation of functions into the header file (inline) This is one way to resolve linking issues that may arise when implementing a template class in a cpp source file. There are other ways too. Check out the following accompanying files intSLinkedList.h & intSLinkedList.cpp SLinkedList.h test_intSLinkedList.cpp & test_SLinkedList.cpp What kind of new data types (classs) can we put in the generic SLinkedList class?

Destructors and Copy constructors When there are pointer member variables there is usually need for customised destructors The memory allocated to the variable must be released It's the responsibility of the class to do so at destruction time Not doing so, creates a memory leakage problem There may also be need for copy constructor When we copy a pointer variable to another pointer variable, only the address is copied Changing what the first pointer points to will also change what the copied pointer points to If we remove the content of the one pointer, the other pointer will point to invalid memory (dangling pointer) - Try copying the intSLinkedList somewhere in the middle of test_intSLinkedList.cpp and printing the content of the copy right after it's created as well as at the end. What's different from test_SLinkedList.cpp ? Why?

Insertion Sort with Singly Linked List? How to find the right place for the new node? follow the next links How to insert a new node in the right place? We don't have a pointer to the previous node

There must be a better way! Doubly Linked Lists

Doubly Linked List template <typename E> class DLinkedList { public: DLinkedList(); bool empty() const; ~DLinkedList(); void add(DNode<E>* right_place, const E& e); void remove(DNode<E>* pos); DNode<E>* getRightPlace(const E& e); void print(); private: DNode<E>* head; DNode<E>* tail; }; template <typename E> class DNode { private: E elem; DNode* next; DNode* prev; friend class DLinkedList<E>; };

Doubly Linked List // Default Constructor DLinkedList(){ head = new DNode<E>; tail = new DNode<E>; head->next = tail; head->prev = NULL; tail->prev = head; tail->next = NULL; }

Doubly Linked List // add right before right_place in the list void add(DNode<E>* right_place, const E& e) { DNode<E>* v = new DNode<E>; v->elem = e; v->next = right_place; right_place->prev->next = v; v->prev = right_place->prev; right_place->prev = v; }

Doubly Linked List // remove from position pos in the list void remove(DNode<E>* pos) { DNode<E>* u = pos->prev; DNode<E>* w = pos->next; u->next = w; w->prev = u; delete pos; }

Doubly Linked List // returns a pointer to a node before which elem should be inserted assuming that the list is sorted ascendingly and must remain so. DNode<E>* getRightPlace(const E& e){ DNode<E>* Cursor = head->next; while ((Cursor!=tail)&&(Cursor->elem < e )) Cursor = Cursor->next; return Cursor; } bool empty() const{ return head->next == tail; } ~DLinkedList(){ while (!empty()) remove(head->next); }

Doubly Linked List void print() { DNode<E>* v = head->next; while (v != tail){ cout << v->elem << " "; v = v->next; } cout << endl;

Sorting with Doubly Linked List void sort_string(){ DLinkedList<string> sorted; string x = "<start>"; while (x != "<stop>"){ cin >> x; if (x == "<stop>") break; sorted.add(sorted.getRightPlace(x), x); sorted.print(); }

Advantages and Disadvantages Advantages of linked list No need for max_num_items Inserting an item is much less time consuming (No need for shifting) Disadvantages of linked list Confusing Could be dangerous (Dangling pointers, memory leaks, ...)

Final Notes We saw only some useful concepts and syntax in action Read the first chapter for a guide on what you need to know Here is a good book for expanding your knowledge of C++ and staying up-to-date: Professional C++, Marc Gregoire Gregoire, Wiley 2014 (or even older versions)