LAB#4 Linked List.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Arrays: pluses and minuses + Fast element access. -- Impossible to resize.
Linked Lists.
Double linked list Lai Ah Fur. The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
3 May Linked Lists CSE 2011 Winter Linked Lists2 Singly Linked Lists (3.2) A singly linked list is a concrete data structure consisting of.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are crated using the class definition. Programming techniques.
LAB#4. Linked List : A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
IntNode Class class IntNode { public: int info; class IntNode *next; IntNode(int el, IntNode *ptr = 0) { info = el; next = ptr; } }; diagrammatic representation:
CS212D : DATA STRUCTURES 1 Week 5-6 Linked List. Outline 2  Singly Linked Lists  Doubly Linked Lists  Recursions.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2014 Goodrich, Tamassia, Goldwasser Singly Linked Lists1 Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
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.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Linked List, Stacks Queues
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Lecture 6 of Computer Science II
Data Structure By Amee Trivedi.
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Lectures linked lists Chapter 6 of textbook
Sequences 6/3/2018 9:11 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Review Deleting an Element from a Linked List Deletion involves:
Lists CS 3358.
Data Structure Dr. Mohamed Khafagy.
Linked Lists Linked Lists 1 Sequences Sequences 07/25/16 10:31
Doubly Linked List Review - We are writing this code
Sequences 8/1/2018 4:38 AM Linked Lists Linked Lists.
Chapter 4 Linked Lists.
Sequences 8/2/ :13 AM Linked Lists Linked Lists.
LAB#4#5 Linked List.
Sequences 8/2/ :16 AM Linked Lists Linked Lists.
EEL 4854 IT Data Structures Linked Lists
Data Structures Linked list.
Chapter 4 Linked Lists
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Linked Lists.
Chapter 4 Linked Lists.
Arrays and Linked Lists
Linked Lists.
Sequences 12/8/2018 3:02 AM Linked Lists Linked Lists.
CS212D: Data Structures Week 5-6 Linked List.
CS2013 Lecture 4 John Hurley Cal State LA.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Linked Lists in C and C++
Problem Understanding
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
Linked Lists & Iterators
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Linked Lists.
Data Structures & Programming
Problem Understanding
Presentation transcript:

LAB#4 Linked List

Overview Before we go to our lesson we must know about : data structure . Algorithms . data structure is an arrangement of data in a computer’s memory (or sometimes on a disk). Data structures include linked lists, arry , stacks, binary trees, and hash tables. Algorithms is manipulate the data in these structures in various ways, such as inserting a new data item, searching for a particular item, or sorting the items.

Overview of Data Structures A data structure is an arrangement of data in a computer’s memory (or sometimes on a disk). Data structures include linked lists, stacks, binary trees, and hash tables. Overview of Algorithms Algorithms manipulate the data in these structures in various ways, such as inserting a new data item, searching for a particular item, or sorting the items.

Arrays: pluses and minuses + Fast element access. -- Impossible to resize. Many applications require resizing! Required size not always immediately available. All of these problem in array we can fix it we linked list Linked Lists

Q: What is the meaning of this symbol -> It mean Accessing Class Members Through an object of the class using (.) Through a pointer to the class using (->) For example: Student S1; S1. setstnum(1234); Student *S1ptr; Or (*S1ptr).setstnum(1234); S1ptr-> setstnum(1234);

Linked List Linked List : A B C Head Tail A linked list is a series of connected nodes. Each node contains at least: A piece of data (any type) Pointer to the next node in the list Head: pointer to the first node. The last node points to NULL. node A data pointer

Linked List Singly Linked List : We use two classes: Node and List Declare IntSLLNode class for the nodes class IntSLLNode { public: IntSLLNode() { next = 0; } IntSLLNode(int i, IntSLLNode *ptr = 0) { info = i; next = ptr; } int info; IntSLLNode *next; };

Linked List Singly Linked List : Declare IntSLList which contains : class IntSLList { public: IntSLList() {head = tail =0; } void AddToHead(int); void AddToTail(int); void DeleteFromHead(); void DeleteFromTail(); void DeleteNode(int); bool isInList(int) const; void DisplayList(); private: IntSLLNode *head, *tail; };

Inserting at the Head Allocate a new node Insert new element Make new node point to old head Update head to point to new node Linked Lists

Linked List Singly Linked List : void IntSLList::AddToHead(int el) { Declare IntSLList Class member function: 1- void AddToHead(int); void IntSLList::AddToHead(int el) { head = new IntSLLNode(el,head); if (tail == 0) tail = head; }

Inserting at the Tail Allocate a new node Insert new element Have new node point to null Have old last node point to new node Update tail to point to new node Linked Lists

Linked List Singly Linked List : void IntSLList::AddToTail(int el) { Declare IntLList Class member function: 2- void AddToTail(int); void IntSLList::AddToTail(int el) { if (tail != 0) // if list not empty; { tail->next = new IntSLLNode(el); tail = tail->next; } else head = tail = new IntSLLNode(el); }

Removing at the Head Update head to point to next node in the list Allow garbage collector to reclaim the former first node Linked Lists

Linked List Singly Linked List : void IntSLList::DeleteFromHead(){ Declare IntLList Class member function: 3- void DeleteFromHead(); void IntSLList::DeleteFromHead(){ if(head !=0) { IntSLLNode *tmp =head; if (head == tail) //if only one node in the list head = tail = 0; else head = head ->next; delete tmp;} }

Removing at the Tail Removing at the tail of a singly linked list cannot be efficient! There is no constant-time way to update the tail to point to the previous node Linked Lists

Linked List Singly Linked List : 4- void DeleteFromTail(); void IntSLList::DeleteFromTail() {if(head != 0) {if (head == tail) //if only one node in the list {delete head; head=tail=0;} else { IntSLLNode *tmp; //find the predecessor of tail for(tmp=head; tmp->next != tail; tmp = tmp->next); delete tail; tail=tmp; tail->next=0;}} }

Linked List Singly Linked List : Declare IntLList Class member function: 5- int DeleteNode(int el);

Linked List

Linked List Singly Linked List : Declare IntLList Class member function: 6- bool isInList(int) const; bool IntSLList:: isInList(int el) const { IntSLLNode *tmp; for (tmp=head; tmp != 0 && !(tmp->info == el); tmp = tmp->next); return tmp !=0; }

Linked List Singly Linked List : void IntSLList::DisplayList() Declare IntSLList Class member function: 7- void DisplayList(); void IntSLList::DisplayList() {IntSLLNode *current; current = head; cout << "head = " << head << "\n"; while(current != 0) {cout << current->info << " " << current << "\n"; current=current->next;} cout << "tail = " << tail << "\n"; cout << "----------------------" << "\n";}

Linked List Singly Linked List : Using List : void main() {IntSLList myllist; myllist.AddToHead(50); myllist.AddToHead(90); myllist.AddToHead(60); myllist.AddToHead(68); myllist.DisplayList(); myllist.DeleteFromHead(); myllist.DeleteNode(60); if (myllist.isInList(60)== 0) cout<<"60 isn't in the list" << endl; cout<<"60 is in the list" << endl; myllist.DisplayList();}

Doubly Linked List A doubly linked list is often more convenient! Nodes store: element link to the previous node link to the next node Special trailer and header nodes prev next elem node trailer header nodes/positions elements Linked Lists

Linked List Doubly Linked List : Declare IntDLLNode which contains : class IntDLLNode{ public: IntDLLNode() {next=prev=0;} IntDLLNode(int el,IntDLLNode *n=0,IntDLLNode *p=0){ info = el; next=n; prev =p; } Int info; IntDLLNode *next, *prev; };

Linked List Doubly Linked List : Declare IntDLList which contains : class IntDLList{ public: IntDLList(){ Head=tail=0;} void addToDLLTail(int el); void deleteFromDLLTail(); void IntDLList::DisplayFromHead(); protected: IntDLLNode *head ,*tail; };

Insertion We visualize operation insertAfter(p, X), which returns position q p A B C p q A B C X p q A B X C Linked Lists

Insertion Algorithm Algorithm insertAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p) {link v to its predecessor} v.setNext(p.getNext()) {link v to its successor} (p.getNext()).setPrev(v) {link p’s old successor to v} p.setNext(v) {link p to its new successor, v} return v {the position for the element e} Linked Lists

Linked List Doubly Linked List : void IntDLList:: addToDLLTail(int el) Declare IntDLList Class member function: 1- void addToDLLTail(int el); void IntDLList:: addToDLLTail(int el) {if (tail!=0){ tail=new IntDLLNode(el,0,tail); tail->prev->next=tail;} else head=tail=new IntDLLNode(el); }

Deletion We visualize remove(p), where p == last() A B C D p A B C p D Linked Lists

Deletion Algorithm Algorithm remove(p): t = p.element {a temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()) {linking out p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null) {invalidating the position p} p.setNext(null) return t Linked Lists

Linked List Doubly Linked List : void IntDLList:: deleteFromDLLTail() Declare IntDLList Class member function: 2- void deleteFromDLLTail() void IntDLList:: deleteFromDLLTail() {if(tail !=0){ if(head==tail) { //if only one node in the list delete head; head = tail =0;} else { tail = tail->prev; delete tail->next; tail->next = 0;}}}

Linked List Doubly Linked List : void IntDLList::DisplayFromHead() { Declare IntDLList Class member function: 2- void IntDLList::DisplayFromHead(); void IntDLList::DisplayFromHead() { IntDLLNode *current; for( current = head ; current != 0 ; current = current->next ) cout<<current->info<<endl; cout<<"--------------------------------"<<endl; }