CS32 - Week 2. Umut Oztok. July 1, Umut Oztok CS32 - Week 2

Similar documents
CS32 Discussion Week 3

Ashish Gupta, Data JUET, Guna

.:: UNIT 3 ::. LIST AND LINKED LIST

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

! A data structure representing a list. ! A series of nodes chained together in sequence. ! A separate pointer (the head) points to the first

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Dynamic Data Structures

lecture09: Linked Lists

Data Structures. Alice E. Fischer. Lecture 4, Fall Alice E. Fischer Data Structures L4... 1/19 Lecture 4, Fall / 19

Ch. 17: Linked Lists. Introduction to Linked Lists

Implementation. Learn how to implement the List interface Understand the efficiency trade-offs between the ArrayList and LinkedList implementations

Linked List using a Sentinel

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

Queues. A queue is a special type of structure that can be used to maintain data in an organized way.

DOWNLOAD PDF LINKED LIST PROGRAMS IN DATA STRUCTURE

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

CS 171: Introduction to Computer Science II. Linked List. Li Xiong

CS32 Discussion Sec.on 1B Week 2. TA: Zhou Ren

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

(More) Fun with Pointers and Linked Lists! CS 16: Solving Problems with Computers I Lecture #17

CS 103 Unit 11. Linked Lists. Mark Redekopp

CS 2113 Software Engineering

Solution for Data Structure

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

COL106: Data Structures and Algorithms. Ragesh Jaiswal, IIT Delhi

Linked Lists and other Dynamic Data Structures

C Programming, Autumn 2013, Exercises for the Second Week

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Introduction to Programming in C Department of Computer Science and Engineering

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

! A data type for which: ! An ADT may be implemented using various. ! Examples:

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board)

Data Structures in C. C Programming and Software Tools. N.C. State Department of Computer Science

Data Structures and Algorithms

CS32 - Week 4. Umut Oztok. Jul 15, Umut Oztok CS32 - Week 4

CS 261 Data Structures. AVL Trees

BBM 201 DATA STRUCTURES

CS 261 Data Structures. AVL Trees

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Introduction to Computer Science II CS S-20 Linked Lists III

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

malloc(), calloc(), realloc(), and free()

Robust Programming. Style of programming that prevents abnormal termination and unexpected actions

CMSC 341 Lecture 7 Lists

CS 261: Data Structures. Dynamic Arrays. Introduction

[CSE10200] Programming Basis ( 프로그래밍기초 ) Chapter 9. Seungkyu Lee. Assistant Professor, Dept. of Computer Engineering Kyung Hee University

Self-referential Structures and Linked List. Programming and Data Structure 1

PROGRAMMAZIONE I A.A. 2017/2018

Introduction to Linked List: Review. Source:

Introduction to Programming in C Department of Computer Science and Engineering

You have this page and the next page to present your solution.

CP2 Revision. theme: dynamic datatypes & data structures

Implementing an abstract datatype. Linked lists and queues

CS132 Algorithm. Instructor: Jialiang Lu Office: Information Center 703

CS61, Fall 2012 Section 2 Notes

lecture14: Tree Traversals

Exercises with Linked Lists CS 16: Solving Problems with Computers I Lecture #15

Chapter 17: Linked Lists

Midterm Practice TA: Brian Choi Section Webpage:

List: Tree: Students are expected to pay attention and fill in the details. Fundamental dynamic memory structure is: list

Data Structures and Algorithms

Consider the program...

CS 103 Unit 11. Linked Lists. Mark Redekopp

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Functional Programming. Pure Functional Programming

18. Dynamic Data Structures I. Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

Chapter 17: Linked Lists

CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees. CS 135 Winter 2018 Tutorial 7: Accumulative Recursion and Binary Trees 1

Data Structures & Algorithms

We'll dive right in with an example linked list. Our list will hold the values 1, 2, and 3.!

Linked Lists. Contents. Steven J. Zeil. July 31, Linked Lists: the Basics 3

ADT: Design & Implementation

APS105. Structures 11/18/2013. Example A struct of stock items at a store: Structures. Lists. Arrays allow a collection of elements

Programming. Lists, Stacks, Queues

ENCM 339 Fall 2017 Tutorial for Week 8

Linked Lists in C and C++

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

Advanced Programming. Lists. A list is a data structure based on usage of pointers and dynamic allocation of memory.

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

Introduction to Linked Lists

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #10

An Introduction to Trees

ESc101: (Linear, Circular, Doubly) Linked Lists, Stacks, Queues, Trees. Introduction to Linked Lists

Cpt S 122 Data Structures. Data Structures

CMSC Introduction to Algorithms Spring 2012 Lecture 7

CS31 Discussion. Jie(Jay) Wang Week6 Nov.4

1 Short Answer (7 Points Each)

Data Structures and Algorithms. Roberto Sebastiani

CSCE 110 PROGRAMMING FUNDAMENTALS

Introduction to Computer Systems. Exam 2. April 10, Notes and calculators are permitted, but not computers.

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

Linked List. April 2, 2007 Programming and Data Structure 1

Data Structures. Topic #7

Data Structures and Algorithms

CS S-05 Abstract Data Types and Lists 1

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Transcription:

CS32 - Week 2 Umut Oztok July 1, 2016

Arrays A basic data structure (commonly used). Organize data in a sequential way.

Arrays A basic data structure (commonly used). Organize data in a sequential way. Pros:

Arrays A basic data structure (commonly used). Organize data in a sequential way. Pros: Cons: Easy access to elements, i.e., array[5] gets 6 th element. Easy to debug.

Arrays A basic data structure (commonly used). Organize data in a sequential way. Pros: Cons: Easy access to elements, i.e., array[5] gets 6 th element. Easy to debug. Fixed size. Not easy to insert new elements to the front/middle.

Linked Lists Another data structure. Like arrays, organizes data in a sequential way. In general, linked lists are strong when arrays are weak, and vice versa.

Linked Lists Another data structure. Like arrays, organizes data in a sequential way. In general, linked lists are strong when arrays are weak, and vice versa. Basic component is a node: value *next

Linked Lists Another data structure. Like arrays, organizes data in a sequential way. In general, linked lists are strong when arrays are weak, and vice versa. Basic component is a node: value *next Self-referential structures: typedef int DataType; struct Node { DataType value; Node* next; };

Linked Lists A linked list is a series of nodes, each pointing to next one. Node* head 1 5 3 Nil Head pointer to point the first element. Last node s next pointer is NULL.

Linked Lists Some operations: Insert: Delete. Search. To the front. To the end. In the middle. Traverse.

Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil

Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() {

Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() { Node* cur = head;

Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() { Node* cur = head; while(cur!= nullptr) {

Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() { Node* cur = head; while(cur!= nullptr) { cout << cur->value << " ";

Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() { Node* cur = head; while(cur!= nullptr) { cout << cur->value << " "; cur = cur->next;

Traversal How to traverse the nodes of a linked list? head 1 5 3 Nil void print() { Node* cur = head; while(cur!= nullptr) { cout << cur->value << " "; cur = cur->next; } } Output: 1 5 3

Insertion [To the front] How to insert a node with value 6 to the front of the list? From: To: Steps: head 1 5 3 Nil head 6 1 5 3 Nil

Insertion [To the front] How to insert a node with value 6 to the front of the list? From: To: Steps: head 1 5 3 Nil head 6 1 5 3 Nil Create a new node. Point the node s next pointer to head. Point head to the node.

Insertion [To the front] How to insert a node with value 6 to the front of the list? From: To: Steps: head 1 5 3 Nil head 6 1 5 3 Nil Create a new node. Point the node s next pointer to head. Point head to the node. void inserttofront(int val) { Node* p = new Node; p->value = val; p->next = head: head = p; }

Insertion [To the end] How to insert a node with value 6 to the end of the list? From: head 1 5 3 Nil To: head 1 5 3 6 Nil Steps:

Insertion [To the end] How to insert a node with value 6 to the end of the list? From: head 1 5 3 Nil To: head 1 5 3 6 Nil Steps: Create a new node. Go to the end of the list. Point last node s next pointer to the new node.

Insertion [To the end] 1 void inserttoend(int val) {

Insertion [To the end] 1 void inserttoend(int val) { 2 Node* p = new Node; 3 p->value = val; 4 p->next = nullptr; 5 6 Node* cur = head;

Insertion [To the end] 1 void inserttoend(int val) { 2 Node* p = new Node; 3 p->value = val; 4 p->next = nullptr; 5 6 Node* cur = head; 7 if(cur == nullptr) head = p; 8 else { 9 while(cur->next!= nullptr) 10 cur = cur->next; 11 cur->next = p; 12 } 13 }

Insertion [To the K th slot] How to insert a node with value 6 to the 1 st slot? (Node with value 1 is located at the 0 th slot.) From: head 1 5 3 Nil To: head 1 6 5 3 Nil Steps:

Insertion [To the K th slot] How to insert a node with value 6 to the 1 st slot? (Node with value 1 is located at the 0 th slot.) From: head 1 5 3 Nil To: head 1 6 5 3 Nil Steps: Find the node N at K 1 st slot. Create a new node. "Add" the node to K th slot. That is: Make new node s next pointer point to the node at K th slot. Make N s next pointer point to the new node.

Insertion [To the K th slot] How to insert a node with value 6 to the 1 st slot? (Node with value 1 is located at the 0 th slot.) From: head 1 5 3 Nil To: head 1 6 5 3 Nil Steps: Find the node N at K 1 st slot. Create a new node. "Add" the node to K th slot. That is: Make new node s next pointer point to the node at K th slot. Make N s next pointer point to the new node. What if K = 0?

Insertion [To the K th slot] 1 void inserttokth(int val, int index) {

Insertion [To the K th slot] 1 void inserttokth(int val, int index) { 2 if(head == nullptr index == 0) inserttofront(val); 3 else {

Insertion [To the K th slot] 1 void inserttokth(int val, int index) { 2 if(head == nullptr index == 0) inserttofront(val); 3 else { 4 Node* cur = head; 5 while(cur->next!= nullptr) { 6 if(--index == 0) break; 7 cur = cur->next; 8 }

Insertion [To the K th slot] 1 void inserttokth(int val, int index) { 2 if(head == nullptr index == 0) inserttofront(val); 3 else { 4 Node* cur = head; 5 while(cur->next!= nullptr) { 6 if(--index == 0) break; 7 cur = cur->next; 8 } 9 //cur points to either the last element 10 //or (index-1)st element 11 Node* p = new Node; 12 p->value = val; 13 p->next= cur->next; 14 cur->next = p; 15 } 16 }

Deletion How to delete the first node containing 5? From: head 1 5 3 Nil To: head 1 3 Nil Steps: (Let N denote the node we want to delete.)

Deletion How to delete the first node containing 5? From: head 1 5 3 Nil To: head 1 3 Nil Steps: (Let N denote the node we want to delete.) Find the node just before N. Have its next pointer point to the node just after N. Delete N. Watch out for special cases. List is empty. List contains a single node. List doesn t have a node containing the given value.

Deletion 1 void delete(int val) {

Deletion 1 void delete(int val) { 2 if(head == nullptr) return;

Deletion 1 void delete(int val) { 2 if(head == nullptr) return; 3 if(head->value == val) { 4 Node* p = head; 5 head = p->next; 6 delete p; 7 } 8 else {

Deletion 1 void delete(int val) { 2 if(head == nullptr) return; 3 if(head->value == val) { 4 Node* p = head; 5 head = p->next; 6 delete p; 7 } 8 else { 9 Node* cur = head; 10 while(cur->next!= nullptr) { 11 if(cur->next->value == val) break; 12 cur = cur->next; 13 }

Deletion 1 void delete(int val) { 2 if(head == nullptr) return; 3 if(head->value == val) { 4 Node* p = head; 5 head = p->next; 6 delete p; 7 } 8 else { 9 Node* cur = head; 10 while(cur->next!= nullptr) { 11 if(cur->next->value == val) break; 12 cur = cur->next; 13 } 14 if(cur->next == nullptr) return; 15 Node* p = cur->next; 16 cur->next = p->next; 17 delete p; 18 }}

Exercise 1 Append one linked list to another. Given two linked lists: head 1 5 3 Nil head2 8 4 Nil Append the second one to the first one: head 1 5 3 8 4 Nil

Exercise 1 Append one linked list to another. Given two linked lists: head 1 5 3 Nil head2 8 4 Nil Append the second one to the first one: head 1 5 3 8 4 Nil Any volunteers? void append(node* head2) { }...

Exercise 1 void append(node* head2) {

Exercise 1 void append(node* head2) { if(head == nullptr) head = head2;

Exercise 1 void append(node* head2) { if(head == nullptr) head = head2; Node* cur = head;

Exercise 1 void append(node* head2) { if(head == nullptr) head = head2; Node* cur = head; while(cur->next!= nullptr) cur = cur->next;

Exercise 1 void append(node* head2) { if(head == nullptr) head = head2; Node* cur = head; while(cur->next!= nullptr) cur = cur->next; } //cur pointing to the last node of the first list cur->next = head2;

Exercise 2 Reverse a linked list. From: head 1 5 3 Nil To: head 3 5 1 Nil

Exercise 2 Reverse a linked list. From: head 1 5 3 Nil To: head 3 5 1 Nil Any volunteers? void reverse() { }...

Exercise 2 void reverse() {

Exercise 2 void reverse() { if (head == nullptr) return;

Exercise 2 void reverse() { if (head == nullptr) return; if (head->next == nullptr) return;

Exercise 2 void reverse() { if (head == nullptr) return; if (head->next == nullptr) return; Node* p = head; Node* q = p->next;

Exercise 2 void reverse() { if (head == nullptr) return; if (head->next == nullptr) return; Node* p = head; Node* q = p->next; p->next = NULL;

Exercise 2 void reverse() { if (head == nullptr) return; if (head->next == nullptr) return; Node* p = head; Node* q = p->next; p->next = NULL; while (q!= NULL) { Node* r = q->next; q->next = p; p = q; q = r; }

Exercise 2 void reverse() { if (head == nullptr) return; if (head->next == nullptr) return; Node* p = head; Node* q = p->next; p->next = NULL; while (q!= NULL) { Node* r = q->next; q->next = p; p = q; q = r; } } head = p;

Check List Things to be careful about while playing with linked lists: Nullity of a pointer before deref ing it. Empty linked lists. Linked lists with one element.

Tail Pointer To insert an element at the end of a list, you need to go over each node, which may be quite inefficient. If you always keep a pointer pointing to the last element, inserting an element at the end can be done immediately. That s what a "tail pointer" is. head 6 1 5 3 Nil tail Note that you have to maintain tail pointer in most functions.

Doubly Linked Lists Linked lists are asymmetric in the sense that you can traverse the list in only one direction. Doubly linked lists allow you to traverse in both directions. Basically, every node keeps track of the elements coming just before and after itself. As you have to deal with more pointers, it is harder to maintain.

Dummy node A method to simplify your code. Create a dummy node when you initialize a linked list. Keep the dummy node until destructing the linked list. So, the head pointer always points to the dummy node. No special case checking for empty linked lists. Be careful, the first actual element is head->next.

Linked List Properties Dynamic allocated memory - flexible. Comes with a cost?

Linked List Properties Dynamic allocated memory - flexible. Comes with a cost? Memory of pointers.

Linked List Properties Dynamic allocated memory - flexible. Comes with a cost? Memory of pointers. Fast insertion (to the front or in the middle).

Linked List Properties Dynamic allocated memory - flexible. Comes with a cost? Memory of pointers. Fast insertion (to the front or in the middle). Also to the end with tail pointers.

Linked List Properties Dynamic allocated memory - flexible. Comes with a cost? Memory of pointers. Fast insertion (to the front or in the middle). Also to the end with tail pointers. Fast deletion (no need to shift elements compared to array).

Linked List Properties Dynamic allocated memory - flexible. Comes with a cost? Memory of pointers. Fast insertion (to the front or in the middle). Also to the end with tail pointers. Fast deletion (no need to shift elements compared to array). Search is almost the same as array.

Linked List Properties Dynamic allocated memory - flexible. Comes with a cost? Memory of pointers. Fast insertion (to the front or in the middle). Also to the end with tail pointers. Fast deletion (no need to shift elements compared to array). Search is almost the same as array. Random access is the weakest point of linked lists.

Slides Slides will be available at http://www.cs.ucla.edu/~umut/cs32