Algorithms and Data Structures

Slides:



Advertisements
Similar presentations
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Advertisements

Stacks, Queues, and Linked Lists
Linear Lists – Linked List Representation
DATA STRUCTURES USING C++ Chapter 5
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
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.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
Linked List Variations
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
M180: Data Structures & Algorithms in Java
Circular Linked List. COMP104 Circular Linked List / Slide 2 Circular Linked Lists * A Circular Linked List is a special type of Linked List * It supports.
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.
CS 206 Introduction to Computer Science II 09 / 17 / 2008 Instructor: Michael Eckmann.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
CS 206 Introduction to Computer Science II 09 / 19 / 2008 Instructor: Michael Eckmann.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 CSC 211 Data Structures Lecture 21 Dr. Iftikhar Azim Niaz 1.
UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Department of Computer Science Data Structures Using C++ 2E Chapter 5 Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Data Structures Using C++1 Chapter 5 Linked Lists.
Data Structures Doubly and Circular Lists Lecture 07: Linked Lists
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
Department of Computer Science 1 Some Practice Let’s practice for the final a little bit. OK?
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
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.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
 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.
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
Data Structure & Algorithms
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Programming Circular Linked List.
Unit 3 Linked Lists King Fahd University of Petroleum & Minerals
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
UNIT – I Linked Lists.
Data Structure and Algorithms
Doubly Linked List Review - We are writing this code
UNIT-3 LINKED LIST.
Linked lists.
Algorithms and Data Structures
Algorithms and Data Structures
Sequences 9/18/ :20 PM C201: Linked List.
Programmazione I a.a. 2017/2018.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Chapter 18: Linked Lists.
Stack and Queues Stack implementation using Array
Linked Lists Chapter 4.
Chapter 17: Linked Lists.
LINKED LISTS.
Linked Lists.
CS148 Introduction to Programming II
Linked Lists.
Linked lists.
Presentation transcript:

Algorithms and Data Structures AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Double Linked List Course No.: 0511363 Fall 2014

Double Linked List (DLL) Every node contains three fields: Data field to store the relevant data. pointer field, called after, to store the address of the next node in the list. pointer field, called before, to store the address of the before node in the list. When we delete from end in single linked list we do a loop because we don’t have the pointer to the previous node

Implement DLL Operations We will create the program that does the following: Initialization of the Linked List Adding a new node at the beginning of the Linked List Adding a new node at the end of the Linked List Adding a new node at any place of the Linked List Deleting a node from the beginning of the Linked List Deleting a node from the end of the Linked List Deleting a node from any place of the Linked List Destroying the Linked List

Create 5 double linked list # include<iostream.h> struct node{ int Data ; node * after ; node *before ; }; class SLL { int size; nod *head, *cura, *curb, *last; public: SLL(int s){size = s; } //function for initialization of the DLL void initila_D( ) { head = last = NULL ; }

Insert data to the double linked list void write( ) { cura = head ; while( cura) cout<< " Enter the values of the items : " ; cin>> cura -> Data ; cura = cura -> after; };

Traversing a Double Linked List Traverse mean visit each node and read its data To Traverse list from the head node to the last void read( ) { Int i=0; cura = head ; while( cura) cout<< "Data of node [ " <<i++<<" ] = " Cout<<cur -> Data ; cura = cura -> after; }; How To traverse the double linked list from the last node to the head?

Double Linked list Operation Insertion for inserting a new item into the list Deletion for deleting an item from a linked list. The operation of both insertion and deletion will be: At the beginning of the double linked list At a required place At the end of the double linked list

Inserting a node at the beginning of the double linked list void Addfirst (int NewData) { if(!head) { head = new node; head->Data = NewData; head -> after = NULL; last = head; last -> before = NULL; } else node *temp = new node; temp -> Data = NewData; temp -> after =head; temp -> before = NULL; head -> before = temp ; head = temp; } }

Inserting a new node at the end of double linked list void AddLast(int NewData){ If ( !head ) { last = new node; Last -> Data = NewData; last -> after = NULL ; last -> before = NULL ; head = last ; } else node *temp = new node ; temp -> Data = NewData ; last -> after = temp ; temp -> before = last ; temp -> after = NULL: last = temp ; } }

Inserting a new node in any place in double linked list void adany_D (int m) { nod *temp; temp = new nod ; temp -> Data = m ; cura = head ; while (cura -> Data < m ) cura = cura -> after ; } temp -> after = cura ; temp -> before = cura -> before; cura -> before -> after = temp; cura -> before = temp;

Deleting a node at the beginning of the doubly linked list void delFirst( ) { cura = head; head = head -> after; delete cura; head -> before = NULL ; }

Deleting a node at the end of the doubly linked list void delLast( ) { curb = last ; last = last -> before ; delete curb; last -> after = NULL; }

Deleting a node from the linked list void deany_D(int me ) { assert(head -> Data != me); assert(last -> Data != me); cura = head ; while (cura -> Data != me) cura = cura -> after ; } cura -> before -> after =cura ->after; cura -> after -> before =cura ->before; delete cura ;

Destroy Doubly Linked List void destroy_D() { if ( ! head ) exit(1) ; cura = head ; while(cura) head = cura -> after; delete cura; cura = head; } cout<<" No linked List exist \n";

Use Doubly Linked list main( ) { SLL x(4); x.create_D( ) ; x.read_D(); x.write_D(); //using other operation x. destroy_D(); }

Advantages of Double Linked List The creation of double linked list have many advantages: add data from the last node in addition to first node. traverse the list from the end of the list also. easier to add nodes from both sides. easier to delete a node from both sides.