Doubly / Two Way Linked List It is linear collection of data elements, called nodes, where each node N is divided into three parts: – An information field.

Slides:



Advertisements
Similar presentations
EENG212 Algorithms and Data Structures
Advertisements

Linked Lists Linked Lists Representation Traversing a Linked List
LINKED LIST, STACKS AND QUEUES Saras M Srivastava, PGT – Comp. Sc. Kendriya Vidyalaya TengaValley.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Linked List Variations
Doubly linked list concept Node structure Insertion sort Insertion sort program with a doubly linked list.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures: Doubly Linked List1 Doubly linked list l The linear linked list is accessed from the first node each time we want to insert or delete.
Data Structures Queue Namiq Sultan 1. Queue A queue is an ordered collection of items into which items may be added at one end (rear) and from which items.
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.
WELCOME TO Linked List, Stack & Queue By Sumit Kumar PGT(CS) KV, Samba.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
Programming Linked Lists. COMP104 Linked Lists / Slide 2 Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use.
Concept of lists and array implementations of lists.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
Review on linked lists. Motivation * A “List” is a useful structure to hold a collection of data. n Currently, we use arrays for lists * Examples: List.
List, (dynamic) linked list Let’s first forget about ‘classes’, but only a dynamic list. We make lists with ‘classes’ afterwards.
Reference: Vinu V Das, Principles of Data Structures using C and C++
Implementation of Linked List For more notes and topics visit: eITnotes.com.
UNIT 1 Data Structures Using C Linked List By Rohit Khokher Department of Computer Science, Vidya College of Engineering, Meerut, India.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
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.
+ struct Node { + int x; + Node * next; + }; + Node *mylist; + Node *mylist = NULL; head 121.
Data Structures Stack Namiq Sultan 1. Data Structure Definition: Data structures is a study of different methods of organizing the data and possible operations.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 16. Linked Lists.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 11 – Data Structures.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Algorithms and Data Structures
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 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.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
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.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
CSCE 210 Data Structures and Algorithms
Programming Circular Linked List.
What is a Queue? Queue is a linear data structure in which the insertion and deletion operations are performed at two different ends. In a queue data structure,
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Linking in double direction
Elementary Data Structures
Prepared by, Jesmin Akhter, Lecturer, IIT, JU
Linked List.
Doubly Linked List Review - We are writing this code
Linked-list.
Linked lists.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCE 210 Data Structures and Algorithms
Chapter 4 Linked Lists
Programmazione I a.a. 2017/2018.
Dynamic Memory Allocation Reference Variables
Chapter 16-2 Linked Structures
Dummy Nodes, Doubly Linked Lists and Circular Linked Lists
Stack and Queues Stack implementation using Array
Stack and Queues Stack implementation using Array
Stacks Data structure Elements added, removed from one end only
CS1201: Programming Language 2
General List.
Linked lists.
Data Structures & Programming
Presentation transcript:

Doubly / Two Way Linked List It is linear collection of data elements, called nodes, where each node N is divided into three parts: – An information field INFO which contains data. – A pointer field FORW which contains the location of next node. – A pointer field BACK which contains the location of preceding node.

Algorithm for Insertion If AVAIL = NULL, then Write: OVERFLOW, and Exit. Set New : = AVAIL, AVAIL : = FORW[AVAIL], INFO[NEW] : = ITEM. Set FORW[LOCA] : = NEW, FORW[NEW] : = LOCB, BACK[LOCB] : = NEW, BACK[NEW] : = LOCA. EXIT.

Algorithm for Deletion Set FORW[BACK[LOC]] : = FORW[LOC] and BACK[FORW[LOC]] : = BACK[LOC]. Set FORW[LOC] : = AVAIL and AVAIL : = LOC. Exit.

Doubly Linked List Implementation in C++ #include using namespace std; struct list { struct list *prev; int data; struct list *next; } *node = NULL, *first = NULL, *last = NULL, *node1 = NULL, *node2 = NULL; class linkedlist { public: void insert_beginning() { list *addBeg = new list; cout << "Enter value for the node:" << endl; cin >> addBeg->data; if(first == NULL) { addBeg->prev = NULL; addBeg->next = NULL; first = addBeg; last = addBeg; cout << "Linked list Created!" << endl; }

Doubly Linked List Implementation in C++ else { addBeg->prev = NULL; first->prev = addBeg; addBeg->next = first; first = addBeg; cout << "Data Inserted at the beginning of the Linked list!" << endl; } void insert_end() { list *addEnd = new list; cout << "Enter value for the node:" << endl; cin >> addEnd->data; if(first == NULL) { addEnd->prev = NULL; addEnd->next = NULL; first = addEnd; last = addEnd; cout << "Linked list Created!" << endl; }

Doubly Linked List Implementation in C++ else { addEnd->next = NULL; last->next = addEnd; addEnd->prev = last; last = addEnd; cout << "Data Inserted at the end of the Linked list!" << endl; } void display() { node = first; cout << "List of data in Linked list in Ascending order!" << endl; while(node != NULL) { cout data << endl; node = node- >next; }

Doubly Linked List Implementation in C++ node = last; cout << "List of data in Linked list in Descending order!" << endl; while(node != NULL) { cout data << endl; node = node- >prev; } void del() { int count = 0, number, i; node = node1 = node2 = first; for(node = first; node != NULL; node = node->next) cout << "Enter value for the node:" << endl; count++; display();

Doubly Linked List Implementation in C++ cout << count << " nodes available here!" << endl; cout << "Enter the node number which you want to delete:" << endl; cin >> number; if(number != 1) { if(number 0) { for(i = 2; i <= number; i++) node = node->next; for(i = 2; i <= number-1; i++) node1 = node1->next; for(i = 2; i <= number+1; i++) node2 = node2->next; node2->prev = node1; node1->next = node2; node->prev = NULL; node->next = NULL; node = NULL; }

Doubly Linked List Implementation in C++ else if(number == count) { node = last; last = node->prev; last->next = NULL; node = NULL; } else cout << "Invalid node number!" << endl; } else { node = first; first = node->next; first->prev = NULL; node = NULL; } cout << "Node has been deleted successfully!" << endl; } };

Doubly Linked List Implementation in C++ int main() { int op = 0; linkedlist llist = linkedlist(); while(op != 4) { cout << "1. Insert at the beginning\n2. Insert at the end\n3. Delete\n4. Display\n5. Exit" << endl; cout << "Enter your choice:" << endl; cin >> op; switch(op) { case 1: llist.insert_beginning(); break; case 2: llist.insert_end(); break; case 3: llist.del(); break; case 4: llist.display(); break;

Doubly Linked List Implementation in C++ case 5: cout << "Bye Bye!" << endl; return 0; break; default: cout << "Invalid choice!" << endl; } return 0; }

Circular Linked List Implementation in C++ #include using namespace std; struct node { int data; node *next; }; typedef node *list; int main() { int dat; char ch; list first, last; first=NULL; last=NULL;

Circular Linked List Implementation in C++ cout<<"Do you want to enter data?(y/n)"<<endl; cin>>ch; while(ch=='y'||ch=='Y') { cout<<"Enter data"<<endl; cin>>dat; if(last==NULL) { last=new node; last->data=dat; last->next=last; first=last; }

Circular Linked List Implementation in C++ else { last->next=new node; last->next->data=dat; last->next->next=first; } cout<<"Enter more data?(y/n)"<<endl; cin>>ch; } cout<<"Displaying the ciruclar linked list"<<endl; while(first!=last) { cout data next<<endl; last=last->next; } return 0; }