Lecture No.04 Data Structures Dr. Sohail Aslam

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Inserting a Node into a Specified Position of a Linked List To create a node for the new item newNode = new Node(item); To insert a node between two nodes.
Linked Lists Chapter 4.
CS 367 – Introduction to Data Structures
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
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.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 3: Arrays, Linked Lists, and Recursion
Reference: Vinu V Das, Principles of Data Structures using C and C++
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Chapter 5 Linked Lists II
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
CSCS-200 Data Structure and Algorithms Lecture
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
Lecture No.05 Data Structures Dr. Sohail Aslam.  Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;
List data structure This is a new data structure. The List data structure is among the most generic of data structures. In daily life, we use shopping.
CS1020 L AB 3 (L INKED L IST ) Problem 1: Balls Problem 2: Josephine Problem 3: Keyboard.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
Programming Circular Linked List.
Lecture 6 of Computer Science II
C++ Programming:. Program Design Including
Java Methods Lists and Iterators Object-Oriented Programming
Lectures linked lists Chapter 6 of textbook
Review Deleting an Element from a Linked List Deletion involves:
CS2006- Data Structures I Chapter 5 Linked Lists I.
Lists CS 3358.
Lecture No.06 Data Structures Dr. Sohail Aslam
Lecture No.03 Data Structures Dr. Sohail Aslam
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
EEL 4854 IT Data Structures Linked Lists
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Data Structures and Algorithms IT12112
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists Chapter 4.
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Linked Lists.
Circularly Linked Lists
Chapter 18: Linked Lists.
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Linked Lists.
Reference Variables The symbol “&” has a few different purposes depending on where it occurs in code. When it appears in front of a variable name, it is.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Problem Understanding
Operations on Binary Tree
LINKED LISTS.
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Linked Lists.
Lecture No.02 Data Structures Dr. Sohail Aslam
Data Structures & Algorithms
List Iterator Implementation
Programming II (CS300) Chapter 07: Linked Lists
BY PROF. IRSHAD AHMAD LONE.
Chapter 9 Linked Lists.
Linked Lists.
Data Structures & Programming
Problem Understanding
Presentation transcript:

Lecture No.04 Data Structures Dr. Sohail Aslam

C++ Code for Linked List // position current before the first // list element void start() { lastCurrentNode = headNode; currentNode = headNode; }; The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

C++ Code for Linked List void remove() { if( currentNode != NULL && currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; currentNode The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. headNode 2 6 8 7 1 size=5 lastcurrentNode

C++ Code for Linked List void remove() { if( currentNode != NULL && currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; 1 currentNode The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. headNode 1 2 6 8 7 1 size=5 lastcurrentNode

C++ Code for Linked List void remove() { if( currentNode != NULL && currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; 1 2 currentNode The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. headNode 1 2 8 7 1 size=5 2 lastcurrentNode

C++ Code for Linked List void remove() { if( currentNode != NULL && currentNode != headNode) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode->getNext(); size--; } }; 1 2 3 4 3 currentNode The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. headNode 1 4 2 8 7 1 size=4 2 lastcurrentNode

C++ Code for Linked List int length() { return size; }; private: int size; Node *headNode; Node *currentNode, *lastCurrentNode; The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Lecture No.04 Data Structures Dr. Sohail Aslam

Example of List Usage #include <iostream> #include <stdlib.h> #include "List.cpp" int main(int argc, char *argv[]) { List list; list.add(5); list.add(13); list.add(4); list.add(8); list.add(24); list.add(48); list.add(12); list.start(); while (list.next()) cout << "List Element: "<< list.get()<<endl; } The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. remove remove is also a one-step operation The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. remove remove is also a one-step operation find worst-case: may have to search the entire list The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Analysis of Linked List add we simply insert the new node after the current node. So add is a one-step operation. remove remove is also a one-step operation find worst-case: may have to search the entire list back moving the current pointer back one node requires traversing the list from the start until the node whose next pointer points to current node. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Doubly-linked List Moving forward in a singly-linked list is easy; moving backwards is not so easy. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Doubly-linked List Moving forward in a singly-linked list is easy; moving backwards is not so easy. To move back one node, we have to start at the head of the singly-linked list and move forward until the node before the current. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Doubly-linked List Moving forward in a singly-linked list is easy; moving backwards is not so easy. To move back one node, we have to start at the head of the singly-linked list and move forward until the node before the current. To avoid this we can use two pointers in a node: one to point to next node and another to point to the previous node: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. element next prev

Doubly-Linked List Node class Node { public: int get() { return object; }; void set(int object) { this->object = object; }; Node* getNext() { return nextNode; }; void setNext(Node* nextNode) { this->nextNode = nextNode; }; Node* getPrev() { return prevNode; }; void setPrev(Node* prevNode) { this->prevNode = prevNode; }; private: int object; Node* nextNode; Node* prevNode; };  The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 

Doubly-linked List Need to be more careful when adding or removing a node. Consider add: the order in which pointers are reorganized is important: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. head 2 6 8 7 1 size=5 current

Doubly-linked List newNode->setNext( current->getNext() ); head 2 6 8 7 1 size=5 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. newNode 9 1

Doubly-linked List newNode->setNext( current->getNext() ); newNode->setprev( current ); current head 2 6 8 7 1 size=5 2 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. newNode 9 1

Doubly-linked List newNode->setNext( current->getNext() ); newNode->setprev( current ); (current->getNext())->setPrev(newNode); current head 2 6 8 7 1 size=5 2 3 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. newNode 9 1

Doubly-linked List newNode->setNext( current->getNext() ); newNode->setprev( current ); (current->getNext())->setPrev(newNode); current->setNext( newNode ); current head 2 6 8 7 1 size=5 4 2 3 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. newNode 9 1

Doubly-linked List newNode->setNext( current->getNext() ); newNode->setprev( current ); (current->getNext())->setPrev(newNode); current->setNext( newNode ); current = newNode; size++; head 2 6 8 7 1 size=6 4 2 3 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. newNode 9 1 current

Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL. Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL. Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL. Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Circularly-linked lists The next field in the last node in a singly-linked list is set to NULL. Moving along a singly-linked list has to be done in a watchful manner. Doubly-linked lists have two NULL pointers: prev in the first node and next in the last node. A way around this potential hazard is to link the last node with the first node in the list to create a circularly-linked list. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Cicularly Linked List Two views of a circularly linked list: 2 6 8 7 1 head current size=5 current 6 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 8 size=5 head 2 7 1

Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like to choose a leader. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. Eventually, a single person remains. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem A case where circularly linked list comes in handy is the solution of the Josephus Problem. Consider there are 10 persons. They would like to choose a leader. The way they decide is that all 10 sit in a circle. They start a count with person 1 and go in clockwise direction and skip 3. Person 4 reached is eliminated. The count starts with the fifth and the next person to go is the fourth in count. Eventually, a single person remains. The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem N=10, M=3 4 3 5 2 6 1 7 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 8 9

Josephus Problem N=10, M=3 eliminated 4 3 5 2 6 1 7 10 8 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 8 9

Josephus Problem N=10, M=3 eliminated 4 3 5 8 2 6 1 7 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 3 5 8 6 2 1 7 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 3 5 8 6 2 7 1 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 5 8 6 2 7 1 3 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 5 8 6 2 7 1 3 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 5 8 6 2 7 1 3 10 9 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9

Josephus Problem N=10, M=3 eliminated 4 5 8 6 2 7 3 10 9 1 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9 1

Josephus Problem N=10, M=3 eliminated 4 5 8 2 7 3 10 9 1 6 The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 10 9 1 6

Josephus Problem  #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << "remove: " << list.get() << endl; list.remove(); } cout << "leader is: " << list.get() << endl; The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << "remove: " << list.get() << endl; list.remove(); } cout << "leader is: " << list.get() << endl;  The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << "remove: " << list.get() << endl; list.remove(); } cout << "leader is: " << list.get() << endl;  The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << "remove: " << list.get() << endl; list.remove(); } cout << "leader is: " << list.get() << endl;  The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << "remove: " << list.get() << endl; list.remove(); } cout << "leader is: " << list.get() << endl;  The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << "remove: " << list.get() << endl; list.remove(); } cout << "leader is: " << list.get() << endl;  The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << "remove: " << list.get() << endl; list.remove(); } cout << "leader is: " << list.get() << endl;  The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << "remove: " << list.get() << endl; list.remove(); } cout << "leader is: " << list.get() << endl; The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 

Josephus Problem #include "CList.cpp" void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << "remove: " << list.get() << endl; list.remove(); } cout << "leader is: " << list.get() << endl; End of lecture 4. The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. 