LAB#4#5 Linked List.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

COMP171 Fall 2005 Lists.
Double linked list Lai Ah Fur. The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int.
CS 367 – Introduction to Data Structures
Linked Lists Contents 1.LinkedList implements the Interface List 2.Doubly Linked List implementation of common methods a.boolean add( Object x) -- append.
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
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 “Linked Lists” SHINTA P STMIK MDP April 2011.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Linked List
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.
Linked Lists 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.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
IntNode Class class IntNode { public: int info; class IntNode *next; IntNode(int el, IntNode *ptr = 0) { info = el; next = ptr; } }; diagrammatic representation:
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.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece.
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.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
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.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
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.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
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.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
CSCE 210 Data Structures and Algorithms
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
CS505 Data Structures and Algorithms
Lists CS 3358.
Doubly Linked List Review - We are writing this code
Traversing a Linked List
Linked lists.
Algorithm for deleting a node from a singly linked list
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Chapter 4 Linked Lists
Doubly linked lists.
Linked Lists Chapter 4.
Linked Lists.
Circularly Linked Lists
Chapter 4 Linked Lists.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Doubly Linked Lists Lecture 21 Tue, Mar 21, 2006.
Chapter 4 Linked Lists.
Chapter 16 Linked Structures
LINKED LISTS.
CMSC 341 Lists 3.
CMSC 341 Lists 3.
LAB#4 Linked List.
Linked Lists.
LAB#3 Stacks Nora Albabtin nora albabtin.
Linked lists.
More on Linked List Yumei Huo Department of Computer Science
16 – Sequential Containers
Linked Lists.
Data Structures & Programming
Presentation transcript:

LAB#4#5 Linked List

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; };

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; }

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); }

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;} }

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; Else cout<<"60 is in the list" << endl; myllist.DisplayList();}

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 DisplayFromHead(); protected: IntDLLNode *head ,*tail; };

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); }

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; }