Lecture No.03 Data Structures Dr. Sohail Aslam

Slides:



Advertisements
Similar presentations
1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Advertisements

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.
Linked Lists Linear collections.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
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.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
Linked Lists. Preliminaries Options for implementing an ADT List Array Has a fixed size Data must be shifted during insertions and deletions Dynamic array.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Linked Lists part II. Linked Lists A linked list is a dynamic data structure consisting of nodes and links: 627 start 8 This symbol indicates a null reference.
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
ليست هاي پيوندي Linked Lists ساختمان داده ها و الگوريتم ها.
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 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
QUEUES What are Queues? Creating a Queue Enqueuing and Dequeuing Testing for an Empty Queue.
1 Linked Structures, LinkedSet References as Links Linear Linked Lists and Non-linear Structures Managing Linked Lists Data Encapsulation Separate from.
Chapter 5 Linked Lists II
Lists and Iterators Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Linked Lists Chapter 4. Linked Structures: Motivations Arrays have fixed size –Problematic for data structures of arbitrary size Arrays order items physically.
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.
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
CSC 205 Programming II Lecture 15 Linked List – Other Variations.
The Vector Class. Vector Class Write a Vector class. This will be a container class that will hold objects. It should allow the following operations:
CSCS-200 Data Structure and Algorithms Lecture
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CS1020 L AB 3 (L INKED L IST ) Problem 1: Balls Problem 2: Josephine Problem 3: Keyboard.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Section 3.7 Linked-Based Implementations
Lecture No.04 Data Structures Dr. Sohail Aslam
Section 2.6 Linked List StringLog ADT Implementation
Sequences 5/10/2018 2:01 PM Linked Lists Linked Lists.
Lecture No.09 Data Structures Dr. Sohail Aslam
Java Methods Lists and Iterators Object-Oriented Programming
CS2006- Data Structures I Chapter 5 Linked Lists I.
Linked List.
Lecture No.06 Data Structures Dr. Sohail Aslam
Doubly Linked List Review - We are writing this code
Templates II CMSC 202.
The Class chain.
The Singly-Linked Structure
MyList<T> It’s a generic type, <T> is a type parameter
A Bag Implementation that Links Data
Linked Lists Chris Wright Winter 2006.
Memory Organization Process 1 (browser) Code Process 3 (word)
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
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.
Linked Lists [AJ 15].
Linked Lists.
Ch. 5 Linked List When a list is implemented by an array Linked list
Linked Lists The items in a linked list data structure are linked to one another An item in a linked list references its successor A linked list is able.
A list-size member variable
Introduction to Algorithms and Data Structures
Reference Variables One should be careful about transient objects that are stored by reference in data structures. Consider the following code that stores.
The Class chain.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Lecture No.02 Data Structures Dr. Sohail Aslam
Data Structures & Algorithms
Compiler Construction
CSCS-200 Data Structure and Algorithms
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Data Structures (CS301) Linked List and its implementation
Presentation transcript:

Lecture No.03 Data Structures Dr. Sohail Aslam

Linked List Actual picture in memory: head 2 6 8 7 1 current 1051 6 1052 1063 current 1053 1063 1054 2 head 1055 1051 1056 2 6 8 7 1 1057 7 1058 1060 End of lecture 2 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future. current 1059 1060 1 1061 head 1062 1054 1063 8 1064 1057 1065

Linked List Operations add(9): Create a new node in memory to hold ‘9’ Node* newNode = new Node(9); 9 newNode The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Linked List Operations add(9): Create a new node in memory to hold ‘9’ Node* newNode = new Node(9); Link the new node into the list 9 newNode 2 6 8 7 1 head current size=5 6 9 newNode 3 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 The Node class 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; }; private: int object; Node *nextNode; }; 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 The Node class 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; }; private: int object; Node *nextNode; };  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 The Node class 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; }; private: int object; Node *nextNode; };  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 The Node class 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; }; private: int object; Node *nextNode; };  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 The Node class 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; }; private: int object; Node *nextNode; };  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 The Node class 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; }; private: int object; Node *nextNode; };  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 The Node class 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; }; private: int object; Node *nextNode; };  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 The Node class 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; }; private: int object; Node *nextNode; }; 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 The Node class 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; }; private: int object; Node *nextNode; }; 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 The Node class 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; }; private: int object; Node *nextNode; }; 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 #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; 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  #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; 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 #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; };  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 #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; };  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 #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; };  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 #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; };  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 #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; 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 #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; 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 #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; 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 #include <stdlib.h> #include "Node.cpp" class List { public: // Constructor List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; size = 0; }; 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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; }; 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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; }; 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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; };  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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; };  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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; };  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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; };  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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; };  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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; };  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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; };  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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; };  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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; };  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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; }; 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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; }; 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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; }; 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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; }; 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 add(int addObject) { Node* newNode = new Node(); newNode->set(addObject); if( currentNode != NULL ){ newNode->setNext(currentNode->getNext()); currentNode->setNext( newNode ); lastCurrentNode = currentNode; currentNode = newNode; } else { newNode->setNext(NULL); headNode->setNext(newNode); lastCurrentNode = headNode; currentNode = newNode; size++; }; 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. 

Building a Linked List List list; headNode size=0 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.

Building a Linked List List list; list.add(2); headNode size=0 currentNode headNode 2 list.add(2); size=1 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.

Building a Linked List List list; list.add(2); list.add(6); headNode size=0 currentNode headNode 2 list.add(2); size=1 lastcurrentNode 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. list.add(6); headNode 2 6 size=2 lastcurrentNode

Building a Linked List List.add(8); list.add(7); list.add(1); currentNode headNode 2 6 8 7 1 size=5 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.

C++ Code for Linked List int get() { if (currentNode != NULL) return currentNode->get(); }; 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 bool next() { if (currentNode == NULL) return false; lastCurrentNode = currentNode; currentNode = currentNode->getNext(); if (currentNode == NULL || size == 0) return false; else return true; }; End of lecture 3 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.