Lecture 5 Sept 12, 2011 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES USING C++ Chapter 5
Advertisements

Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
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,
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
Lecture 2 Feb 3, 2009 goals: continue recursion more examples of recursive programs.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
CS 340Chapter 3: Lists, Stacks, and Queues1 Abstract Data Types An ADT is a set of operations upon a set of data. Implementation details are not specified.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
Lecture 2 Aug goals: Introduction to recursion examples of recursive programs.
Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Lecture 5 Feb 14, 2011 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists.
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 17 Linked List.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
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.
Lists ADT (brief intro):  Abstract Data Type  A DESCRIPTION of a data type  The data type can be anything: lists, sets, trees, stacks, etc.  What.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
7.2 Priority Queue ADTs Priority queue concepts
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Lec 15 Oct 18 Binary Search Trees (Chapter 5 of text)
Self-Referential Classes A Self-referential class is a class that contains a reference to an object that has the same class type. –A self-referential class.
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.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists.
COSC 1P03 Data Structures and Abstraction 5.1 Linear Linked Structures A bank is a place where they lend you an umbrella in fair weather and ask for it.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Definition 2 A Dynamic Dictionary is a data structure of item with keys that support the following basic operations: (1) (1)Insert a new item (2) (2)Remove.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Lists CS 3358.
Chapter 4 Linked Lists.
Priority Queue.
Chapter 4 Linked Lists
Chapter 16-2 Linked Structures
Linked Lists.
Chapter 4 Linked Lists.
Lec 12 March 9, 11 Mid-term # 1 (March 21?)
Data Structures ADT List
Data Structures ADT List
CSE 373 Data Structures and Algorithms
Chapter 4 Linked Lists.
Data Structures ADT List
Lists.
List Iterator Implementation
An Introduction to Programming though C++
Presentation transcript:

Lecture 5 Sept 12, 2011 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists

Overview of list vs. array list can be incrementally grown. (dynamic array resizing is expensive.) inserting next to a given node takes constant time. (In arrays, this takes O(n) time where n = size of the array.) searching for a given key even in a sorted list takes O(n) time. (in sorted array, it takes O(log n) time by binary search.) accessing the k-th node takes O(k) time. (in array, this takes O(1) time.)

Abstract Data Type (ADT) oList ADT insert, find, delete (primary operations) successor, merge, reverse, … (secondary) oVariations of linked lists oCircular lists oDoubly linked lists Review of ADT List

A node class class Node { public: Node(int x) { key = x; next = 0; } void set_key(int x) { key = x; } void set_next(Node* n) { next = n; } int get_key() { return key; } Node* get_next() { return next; } static Node* copy(Node* in) { Node* tmp; if (in == 0) return 0; else { Node* tmp1 = copy(in->next); tmp = new Node(in->key); tmp->next = tmp1; return tmp; } private: int key; Node* next; };

Some list functions Consider the standard singly-linked list class: class list { private: Node* first; list(int k) { first = new Node(k);} void insert(int k, int posn); // insert k at posn Node* find(int k); // return first node containing k void delete(int k); // remove first node containing k int length(); // return the length of the list void delete_after(Node* n);// remove node after n, if it // exists void get_key(int posn); // return key in a given position void print_list(); // print the list } etc. Other functions: delete a key in a position.

Implementing some of the functions void insert(int k, int posn) { // insert k at a given position // write recursively. }

Implementing some of the functions void insert(int k, int posn) { // insert k at a given position // write recursively. if (posn length()) return; // wrong value of posn; ignore if (posn == 1) { list* temp = new list(k); temp->next = first; first = temp; } else first->next = first->next.insert(k, posn-1); }

Reverse the list We want to reverse the list using the existing nodes of the list, i.e., without creating new nodes.

Reverse the list We want to reverse the list using the existing node of the list, i.e., without creating new nodes. void reverse() { if (head == NULL || head -> next == NULL) return; Node* p = head; Node* q = p-> next; p->next = NULL; while (q != NULL) { Node* temp = q -> next; q->next = p; p = q; q = temp; } head = p; }

Remove negative items from the list Example: List: -3, 4, 5, -2, 11 becomes 4, 5, 11 We will write this one recursively.

Remove negative items from the list Example: List: -3, 4, 5, -2, 11 becomes 4, 5, 11 We will write this one recursively. void remove_negative() { // removes all the negative items from a list // Example input: ; output: if (head == NULL) return; else if (head->key >= 0) { List nList = List(head->next); nList.remove_negative(); head->next = nList.head; } else { List nList = List(head->next); nList.remove_negative(); head = nList.head; }

A problem similar to Project # 1 Generate all the subsets of a given set of numbers. Thus, if the input is {1, 2, 4} the output is: {} {1} {2} {4} {1, 2} {1, 4} {2, 4} {1, 2, 4} Our program treats all the input symbols as distinct so a pre-condition is: the input array elements are distinct.

Data structure used Array or vector of lists: Each member of the vector is a pointer to a list containing one of the subsets. null A

subsets(A, j) will generate all the subsets of the set {A[0], A[1], …, A[j – 1]}. Thus, if A = [2, 4, 6, 1], then subsets(A, 1) will generate all the subsets of {2}, build(A, 2) will generate all the subsets of {2, 4} etc. subsets(A,1) returns: null 2 subsets(A,2) returns: null

subsets (A, k) calls subsets(A,k–1). Let temp be the result from the call. It creates a copy of temp, say temp1. It inserts A[k-1] into each list of temp1. It merges temp and temp1 and returns the merged array of lists. Example: A = {1, 3, 2}. Suppose k = 2. Build(A,2) returns the collection of lists [ ], [1], [3], [1, 3]. Now inserting 2 into each of the lists gives [2], [2,1], [2,3], [2,1,3]. Merging the two lists we get: [ ], [1], [3], [1, 3], [2], [2,1], [2,3], [2,1,3]. This is the set of all subsets of {1, 3, 2}.

classes List and Node class List { public: List() { head = 0; } List(int x) { head = new Node(x); } void insert(int k) { // insert k as the first // item of the list Node* tmp = new Node(k); tmp->setNext(head); head = tmp; } void print() { Node* temp = head; cout << "{"; while (temp != NULL) { cout getKey(); if (temp->getNext()!= 0) cout << ", "; temp = temp->getNext(); } cout << "}"; cout << endl; } private: class Node { public: Node(int x) { key = x; next = NULL; } void setNext(Node* p) {next = p;} void setKey(int k) {key = k;} int getKey() { return key;} Node* getNext() { return next;} private: int key; Node* next; }; Node* head; };

The class subsets class subsets { private: vector v; public: subsets(); subsets(vector & A, int k); void insert(int k; void merge(subsets c); void subsetsPrint(); };

Algorithm for permutation problem permutation(input array A, int n, int k) { if (n == 1) // same as for combination else { temp = permutation(A, n-1, k); temp1 = permutation(A, n-1, k-1); for j from 1 to k-1 do { temp2 = copy(temp1); insert A[n-1] at position j of each list in temp2; temp = merge(temp, temp2); } return temp; }