Reference: Vinu V Das, Principles of Data Structures using C and C++

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Stacks, Queues, and Linked Lists
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
CHP-5 LinkedList.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSE Lecture 12 – Linked Lists …
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Review Learn about linked lists
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Linked Lists
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Data Structures & Algorithms
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.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
UNIT-2. Singly Linked List Doubly Linked List Circular Linked List Representing Stack with Linked List. Representing Queue with Linked List.
D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists, Stacks and Queues in C Yang Zhengwei CSCI2100B Data Structures Tutorial 4.
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
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,
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
2005MEE Software Engineering Lecture 7 –Stacks, Queues.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
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:
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 List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
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.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
  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.
LINKED LISTS.
© 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.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
STACKS & QUEUES for CLASS XII ( C++).
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,
Chapter 12 – Data Structures
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Linked Lists Chapter 6 Section 6.4 – 6.6
Review Deleting an Element from a Linked List Deletion involves:
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
Linked lists.
Stacks and Queues.
Stack and Queue APURBO DATTA.
Queues Mohammad Asad Abbasi Lecture 5
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
CMSC 341 Lecture 5 Stacks, Queues
Chapter 18: Linked Lists.
Linked List (Part I) Data structure.
Review & Lab assignments
Chapter 17: Linked Lists.
Linked lists.
LINEAR DATA STRUCTURES
Presentation transcript:

Reference: Vinu V Das, Principles of Data Structures using C and C++ Linked Lists Namiq Sultan Reference: Vinu V Das, Principles of Data Structures using C and C++

Linked Lists

Linked Lists START = NULL if there is no list (i.e.; NULL list or empty list).

ADVANTAGES OF LINKED LIST Linked list have many advantages and some of them are: 1. Linked list are dynamic data structure. That is, they can grow or shrink during the execution of a program. 2. Efficient memory utilization: Memory is allocated whenever it is required. And it is deallocated when it is not needed. 3. Insertion and deletion are easier and efficient. 4. Many complex applications can be easily carried out with linked list.

OPERATIONS ON LINKED LISTS The primitive operations performed on the linked list are as follows 1. Creation 2. Insertion 3. Deletion 4. Traversing 5. Searching 6. Concatenation

OPERATIONS ON LINKED LISTS Creation operation is used to create a linked list. Insertion operation is used to insert a new node at any specified location in the linked list. A new node may be inserted. (a) At the beginning of the linked list (b) At the end of the linked list (c) At any specified position in between in a linked list Deletion operation is used to delete a node from the linked list. A node may be deleted from the (a) Beginning of a linked list (b) End of a linked list (c) Specified location of the linked list Traversing is the process of going through all the nodes from one end to another end of a linked list. Concatenation is the process of appending the second list to the end of the first list.

TYPES OF LINKED LIST 1. Singly linked list 2. Doubly linked list 3. Circular linked list

SINGLY LINKED LIST

SINGLY LINKED LIST

SINGLY LINKED LIST

SINGLY LINKED LIST

ALGORITHM FOR INSERTING A NODE Suppose START is the first position in linked list. DATA be the element to be inserted in the new node. POS is the position where the new node is to be inserted. TEMP is a temporary pointer to hold the node address.

ALGORITHM FOR INSERTING A NODE Insert a Node at the beginning 1. Input DATA to be inserted 2. Create a NewNode 3. NewNode → DATA = DATA 4. If (SATRT equal to NULL) (a) NewNode → Next = NULL 5. Else (a) NewNode → Next = START 6. START = NewNode 7. Exit

ALGORITHM FOR INSERTING A NODE Insert a Node at the end 1. Input DATA to be inserted 2. Create a NewNode 3. NewNode → DATA = DATA 4. NewNode → Next = NULL 8. If (SATRT equal to NULL) (a) START = NewNode 9. Else (a) TEMP = START (b) While (TEMP → Next not equal to NULL) (i) TEMP = TEMP → Next 10. TEMP → Next = NewNode 11. Exit

ALGORITHM FOR INSERTING A NODE Insert a Node at any specified position (POS=1 means insert after first node. POS=0 not allowed) 1. Input DATA and POS to be inserted 2. initialise TEMP = START; and k = 1 3. Repeat the step 3 while( k is less than POS) (a) TEMP = TEMP  Next (b) If (TEMP is equal to NULL) (i) Display “Node in the list less than the position” (ii) Exit (c) k = k + 1 4. Create a New Node 5. NewNode  DATA = DATA 6. NewNode  Next = TEMP → Next 7. TEMP  Next = NewNode 8. Exit

ALGORITHM FOR DELETING A NODE Suppose START is the first position in linked list. Let DATA be the element to be deleted. TEMP, HOLD is a temporary pointer to hold the node address.

ALGORITHM FOR DELETING A NODE 1. Input the DATA to be deleted 2. if ((START → DATA) is equal to DATA) // Delete first node (a) TEMP = START (b) START = START  Next (c) Set free the node TEMP, which is deleted (d) Exit 3. HOLD = START 4. while ((HOLD  Next  Next) not equal to NULL)) // Between first & last (a) if ((HOLD  NEXT  DATA) equal to DATA) (i) TEMP = HOLD  Next (ii) HOLD  Next = TEMP  Next (iii) Set free the node TEMP, which is deleted (iv) Exit (b) HOLD = HOLD  Next

ALGORITHM FOR DELETING A NODE 5. if ((HOLD  next  DATA) = = DATA) // last node (a) TEMP = HOLD → Next (b) Set free the node TEMP, which is deleted (c) HOLD  Next = NULL (d) Exit 6. Disply “DATA not found” 7. Exit

ALGORITHM FOR SEARCHING A NODE 1. Input the DATA to be searched 2. Initialize TEMP = START; POS = 1; 3. Repeat the step 4, 5 and 6 until (TEMP is equal to NULL) 4. If (TEMP  DATA is equal to DATA) (a) Display “The data is found at POS” (b) Exit 5. TEMP = TEMP  Next 6. POS = POS+1 7. If (TEMP is equal to NULL) (a) Display “The data is not found in the list” 8. Exit

ALGORITHM FOR DISPLAY ALL NODES 1. If (START is equal to NULL) (a) Display “The list is Empty” (b) Exit 2. Initialize TEMP = START 3. Repeat the step 4 and 5 until (TEMP = = NULL ) 4. Display “TEMP  DATA” 5. TEMP = TEMP  Next 6. Exit

Programming Linked List class LinkedList{ //Structure declaration for the node struct node{ int info; node *next; }; //private structure variable declared node *start; public: LinkedList()//Constructor defined { start = NULL; } //public fucntion declared void Create_List(int); void AddAtBeg(int); void AddAfter(int, int); void Delete(); void Count(); void Search(int); void Display(); };

Programming Linked List //This function will create a new linked list of elements void LinkedList::Create_List(int data) { node *q, *newNode; //New node is created with new operator newNode = new node; newNode->info = data; newNode->next = NULL; if (start == NULL) //If list is empty start = newNode; else { //Element inserted at the end */ q = start; while(q->next != NULL) q = q->next; q->next = newNode; } }/*End of create_list()*/

Programming Linked List //following function will add new element at the beginning void LinkedList::AddAtBeg(int data) { node *newNode; newNode = new node; newNode->info = data; newNode->next = start; start = newNode; }/*End of addatbeg()*/

Programming Linked List //This function will add new element at any specified position void LinkedList::AddAfter(int data, int pos) { node *newNode, *q; int i; q = start; //Finding the position in the linked list to insert for(i = 0; i<pos-1; i++){ q = q->next; if(q == NULL) { cout<<"\n\nThere are less than "<<pos<<" elements" << endl; return; } }/*End of for*/ newNode = new node; newNode->next = q->next; newNode->info = data; q->next = newNode; }/*End of addafter()*/

Programming Linked List void LinkedList::Delete() { node *tmp, *q; int data; if(start == NULL) { cout<<"\n\nList is empty"<<endl; return; } cout<<"\n\nEnter the element for deletion : "; cin>>data; if(start->info == data){ tmp = start; start = start->next; //First element deleted delete(tmp);

Programming Linked List q = start; while(q->next->next ! = NULL) { if(q->next->info == data) { //Element deleted in between tmp = q->next; q->next = tmp->next; delete(tmp); return; } q = q->next; }/*End of while */ if(q->next->info == data) { //Last element deleted q->next = NULL; cout<<"\n\nElement "<<data<<" not found"<<endl; }/*End of del()*/

Programming Linked List void LinkedList::Display() { // Fill in the code that displays the contents of the linked list }/*End of display() */ void LinkedList::Count() // Fill in the code that counts the nodes of the linked list }/*End of count() */ void LinkedList::Search(int data) // Fill in the code that will find the position of a node that holds the ‘data’ }

Programming Linked List int main() { int choice, n, m, position, i; LinkedList po; while(1) cout<<"1.Create List\n"; cout<<"2.Add at beginning\n"; cout<<"3.Add after \n"; cout<<"4.Delete\n"; cout<<"5.Display\n"; cout<<"6.Count\n"; cout<<“7.Search\n"; cout<<“8.Quit\n"; cout<<"\nEnter your choice:"; cin>>choice;

Programming Linked List switch(choice) { case 1: cout<<"\n\nHow many nodes you want:"; cin>>n; for(i = 0;i<n;i++) { cout<<"\nEnter the element:"; cin>>m; po.Create_List(m); } break; case 2: cout<<"\n\nEnter the element:"; po.AddAtBeg(m);

Programming Linked List case 3: cout<<"\n\nEnter the element:"; cin>>m; cout<<"\nEnter the position after the inserted element :"; cin>>position; po.AddAfter(m,position); break; case 4: po.Delete(); case 5: po.Display(); case 6: po.Count(); case 7: cout<<"\n\nEnter the element to be searched:";

Programming Linked List cin>>m; po.Search(m); break; case 8: exit(0); default: cout<<"\n\nWrong choice"; }/*End of switch */ }/*End of while */ }

STACK USING LINKED LIST PUSH

STACK USING LINKED LIST POP

STACK USING LINKED LIST ALGORITHM FOR PUSH OPERATION TOP is NULL when the stack is empty. DATA is the data item to be pushed. 1. Input the DATA to be pushed 2. Creat a New Node 3. NewNode → DATA = DATA 4. NewNode → Next = TOP 5. TOP = NewNode 6. Exit

STACK USING LINKED LIST ALGORITHM FOR POP OPERATION 1. if (TOP is equal to NULL) (a) Display “The stack is empty” 2. Else (a) TEMP = TOP (b) Display “The popped element TOP → DATA” (c) TOP = TOP → Next (d) TEMP → Next = NULL (e) Free the TEMP node 3. Exit

QUEUE USING LINKED LIST ADD to queue

QUEUE USING LINKED LIST REMOVE from queue

QUEUE USING LINKED LIST ALGORITHM FOR PUSHING AN ELEMENT TO A QUEUE 1. Input the DATA element to be pushed 2. Create a New Node 3. NewNode → DATA = DATA 4. NewNode → Next = NULL 5. If(REAR not equal to NULL) // if it is not empty (a) REAR → next = NewNode (b) REAR = NewNode 6. Else REAR = FRONT = NewNode 7. Exit

QUEUE USING LINKED LIST ALGORITHM FOR POPPING AN ELEMENT FROM A QUEUE 1. If (FRONT is equal to NULL) (a) Display “The Queue is empty” 2. Else (a) Display “The popped element is FRONT → DATA” (b) If(FRONT is not equal to REAR) (i ) FRONT = FRONT → Next (c) Else (i ) FRONT = NULL (ii) REAR = NULL 3. Exit

DOUBLY LINKED LIST Every nodes in the doubly linked list has three fields: LeftPointer, RightPointer and DATA.

REPRESENTATION OF DOUBLY LINKED LIST A node in the doubly linked list can be represented with the following declarations: struct Node { int DATA; Node * next; Node * prev; }; Node * START; All operations performed on singly linked list can also be performed on doubly linked list.

REPRESENTATION OF DOUBLY LINKED LIST

CIRCULAR LINKED LIST A circular linked list is one, which has no beginning and no end. A singly linked list can be made a circular linked list by simply storing the address of the very first node in the linked field of the last node.

CIRCULAR DOUBLY LINKED LIST A circular doubly linked list has both the successor pointer and predecessor pointer in circular manner.