Double linked list Lai Ah Fur. The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

Linked Lists Geletaw S..
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.
Stacks, Queues, and Linked Lists
CS 367 – Introduction to Data Structures
Linear Lists – Linked List Representation
Data Structures ADT List
Linked Lists Linear collections.
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.
LIST PROCESSING.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
Doubly-linked list library.
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Ics202 Data Structures. hh tail head (b) LinkedList head tail Element datum next 3 Integer Element datum next 1 Integer Element datum next 4 Integer.
Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
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.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Data Structures and Applications Hao Jiang Computer Science Department Boston College.
Circularly-Linked Lists Cmput Lecture 17 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Stacks, Queues & Deques CSC212.
Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.
IntNode Class class IntNode { public: int info; class IntNode *next; IntNode(int el, IntNode *ptr = 0) { info = el; next = ptr; } }; diagrammatic representation:
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
CS 2430 Day 35. Agenda Introduction to linked lists Bag as linked list Stack as linked list.
CMSC 341 Linked Lists, Stacks and Queues. 8/3/2007 UMBC CMSC 341 LSQ 2 Implementing Your Own Linked List To create a doubly linked list as seen below.
Lab 7 Queue ADT. OVERVIEW The queue is one example of a constrained linear data structure. The elements in a queue are ordered from least recently added.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Linked Lists. A linear linked list is a collection of objects, called nodes, each of which contains a data item and a pointer to the next node in the.
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.
Week 4 - Monday.  What did we talk about last time?  Queues  Implementing queues with circular arrays.
Introduction to Data Structures and Algorithms
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
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 Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
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 List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
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.
Lists List Implementations. 2 Linked List Review Recall from CMSC 201 –“A linked list is a linear collection of self- referential structures, called nodes,
Queue ADT for lining up politely. COSC 2006 queue2 Queue – simple collection class  first-in first-out (FIFO) structure insert new elements at one end.
CS212: Data Structures and Algorithms Lecture # 4 Linked List.
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.
Linked Data Structures
CSCE 210 Data Structures and Algorithms
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Linked List.
Doubly Linked List Review - We are writing this code
LAB#4#5 Linked List.
Algorithm for deleting a node from a singly linked list
Priority Queue.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Chapter 4 Linked Lists
Linked Lists, Stacks and Queues Textbook Sections
Queues.
Data Structures ADT List
ADT list.
CMSC 341 Lists 3.
CMSC 341 Lists 3.
Data Structures ADT List
LAB#4 Linked List.
Circular Queues: Implemented using Arrays
LAB#3 Stacks Nora Albabtin nora albabtin.
Linked Lists Dr. Jose Annunziato.
Presentation transcript:

Double linked list Lai Ah Fur

The structure of node class IntDLLNode { int info; IntDLLNode next = null, prev = null; IntDLLNode() { } IntDLLNode(int el) { info = el; } IntDLLNode(int el, IntDLLNode n, IntDLLNode p) { info = el; next = n; prev = p; } }

The class of double linked list public class IntDLList { private IntDLLNode head, tail; public IntDLList() { head = tail = null; } ………… }

Insert (1) public void addToDLListHead(int el) { if (!isEmpty()) { head = new IntDLLNode(el,head,null); head.next.prev = head; } Else head = tail = new IntDLLNode(el); }

Insert (2) public void addToDLListTail(int el) { if (!isEmpty()) { tail = new IntDLLNode(el,null,tail); tail.prev.next = tail; } else head = tail = new IntDLLNode(el); }

Delete (1) public int deleteFromDLListHead() { if (!isEmpty()) { // if at least one node in the list; int el = head.info; if (head == tail) // if only one node in the list; head = tail = null; else { // if more than one node in the list; head = head.next; head.prev = null; } return el; } //if (head==tail) else return 0; }

Delete (2) public int deleteFromDLListTail() { if (!isEmpty()) { int el = tail.info; if (head == tail) // if only one node on the list; head = tail = null; else { // if more than one node in the list; tail = tail.prev; tail.next = null; } return el; } else return 0; }

search public int find(int el) { IntDLLNode tmp; for (tmp = head; tmp != null && tmp.info != el; tmp = tmp.next); if (tmp == null) return 0; else return tmp.info; }

Other methods public boolean isEmpty() { return head == null; } public void setToNull() { head = tail = null; } public int firstEl() { if (!isEmpty()) return head.info; else return 0; }

Circular double linked list class CircularDLList{ CircularDLList(){ } boolean isEmpty(){ return head==null;} private IntDLLNode head=null;

void addToHead(int el){ if (isEmpty()){ head=new IntDLLNode(el); head.next=head.prev=head;} else { head=new IntDLLNode(el,head,head.prev); head.next.prev=head.prev.next=head;} } void addToTail(int el){ addToHead(el); head=head.next; }

int deleteFromHead(){ int el=head.info; if (head.next= =head) //only one node head=null; else { head.prev.next=head.next; head.next.prev=head.prev; head=head.next;} return el; }

int deleteFromTail(){ int el=head.prev.info; if (head.next= =head) //only one node head=null; else { head.prev.prev.next=head; head.prev=head.prev.prev; }

void deleteNode(int el) { if (!isempty()) if (el= =head.info) deleteFormHead(); else { IntDLLNode tmp; for(tmp=head.next; tmp!=head&&tmp.info!=el; tmp=tmp.next); if (tmp.info= =el){ tmp.next.prev=tmp.prev; tmp.prev.next=tmp.next; }

boolean isInList(int el){ if (isEmpty()) return false; else if (head.info= =el) return true; else { IntDLLNode tmp; for(tmp=head.next; tmp!=head && tmp.info!=el; tmp=tmp.next); return tmp.info= =el; }

void printAll(){ if (!isEmpty()) { IntDLLNode tmp; System.out.print(head.info+ ); for(tmp=head.next;tmp!=head;tmp=tmp.next) System.out.print(head.info+ ); }

Your homework Write a simple editor by means of double linked-list –I I ( row) I 2 ( 2 th row row) –New (clear all the content) –L cat (locate and list the row number) –R cat,dog,a (search all cat and replace it with dog) –R cat,dog (search the current row which contains cat and replace it with dog) –D D (delete current row) D 3 (delete 3 th row) D 3,5 (delete from 3 th to 5 th ) –M M 4 aaaaaaaaaaaaaa (modify row 4th, its new content is aaaaaa ) –S a1.txt (save file) –L a2.txt (load old file) –A pig.txt (load and append into the current content) –Print Print all Print (print the content of the current row)