Unit-1 Data Structures – Introduction to Data Structures, abstract data types, Linear list – singly linked list implementation, insertion, deletion and.

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Stacks, Queues, and Linked Lists
Chapter 24 Lists, Stacks, and Queues
CHP-5 LinkedList.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Review Learn about linked lists
Data Structures: A Pseudocode Approach with C
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 17: Linked Lists.
Data Structures Topic #3. Today’s Agenda Ordered List ADTs –What are they –Discuss two different interpretations of an “ordered list” –Are manipulated.
INTRODUCTION TO DATA STRUCTURE. Topics To Be Discussed………………………. Meaning of Data Structure Classification of Data Structure Data Structure Operations.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title: Overview of Data Structure.
UNIT-2. Singly Linked List Doubly Linked List Circular Linked List Representing Stack with Linked List. Representing Queue with Linked List.
Data Structures -- Data processing often involves in processing huge volumes of data. Many Companies handle million records of data stored in database.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
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.
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.
Lecture by: Prof. Pooja Vaishnav.  Language Processor implementations are highly influenced by the kind of storage structure used for program variables.
Kovács Zita 2014/2015. II. félév DATA STRUCTURES AND ALGORITHMS 26 February 2015, Linked list.
Data Structure and Algorithms
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.
UNIT-II Topics to be covered Singly linked list Circular linked list
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
Linked Lists Source: presentation based on notes written by R.Kay, A. Hill and C.Noble ● Lists in general ● Lists indexed using pointer arrays ● Singly.
1 Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues Jung Soo (Sue) Lim Cal State LA.
Lecture 6 of Computer Science II
Linked List.
COSC160: Data Structures Linked Lists
Data Structure By Amee Trivedi.
Course Developer/Writer: A. J. Ikuomola
Linked List :: Basic Concepts
Top 50 Data Structures Interview Questions
Lectures linked lists Chapter 6 of textbook
Program based on queue & their operations for an application
Data Structure Interview Question and Answers
Data structures and algorithms
Review Deleting an Element from a Linked List Deletion involves:
Data Structure and Algorithms
Data Structure Dr. Mohamed Khafagy.
UNIT-3 LINKED LIST.
Chapter 4 Linked Lists.
Linked list.
Data Structures Interview / VIVA Questions and Answers
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
Lists.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
LINKED LISTS CSCD Linked Lists.
Lists.
Arrays and Linked Lists
Sequences 11/27/2018 1:37 AM Singly Linked Lists Singly Linked Lists.
Further Data Structures
Linked Lists.
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Linked Lists in C and C++
Linked List.
LINKED LIST.
Data Structures & Algorithms
LINKED LIST Dr. T. Kokilavani Assistant Professor
17CS1102 DATA STRUCTURES © 2018 KLEF – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS RESERVED.
BY PROF. IRSHAD AHMAD LONE.
LINEAR DATA STRUCTURES
Presentation transcript:

Unit-1 Data Structures – Introduction to Data Structures, abstract data types, Linear list – singly linked list implementation, insertion, deletion and searching operations on linear list, circular linked list implementation, Double linked list implementation, insertion, deletion and searching operations. Applications of linked lists

Some Common Operations on Data structures -- Data processing often involves in processing huge volumes of data. Many Companies handle million records of data stored in database. Many ways are formulated to handle data efficiently. -- An User-defined data type is a combination of different primary data types, which represents a complex entity. -- An Abstract Data Type ( A D T ) not only represents a set of complex data objects, but also includes a set of operations to be performed on these objects, defines that how the data objects are organized. -- The group of methods implements a set rules, which defines a logical way of handling data. -- The complex entity along with its group of methods is called Abstract Data Type ( A D T ) . -- Data structure is described as an instance of Abstract Data Type ( ADT ). -- We can define that Data structure is a kind of representation of logical relationship between related data elements. In data structure, decision on the operations such as storage, retrieval and access must be carried out between the logically related data elements. Data Structure Some Data structures Arrays Strings Lists Stacks Queues Trees Graphs Dictionaries Maps Hash Tables Sets Lattice Neural-Nets Linear Non-Linear Linear Lists Stacks Queues Trees Graphs Some Common Operations on Data structures Insertion : adding a new element to the collection. Deletion : removing an element from a collection. Traversal : access and examine each element in collection. Search : find whether an element is present or not. Sorting : rearranging elements in a particular order. Merging : combining two collections into one collection.

Arrays – Linked Lists What is a Linked List The elements of a linked list are not constrained to be stored in adjacent locations. The individual elements are stored “somewhere” in memory, rather like a family dispersed, but still bound together. The order of the elements is maintained by explicit links between them. Limitations of Arrays Fixed in size : Once an array is created, the size of array cannot be increased or decreased. Wastage of space : If no. of elements are less, leads to wastage of space. Sequential Storage : Array elements are stored in contiguous memory locations. At the times it might so happen that enough contiguous locations might not be available. Even though the total space requirement of an array can be met through a combination of non-contiguous blocks of memory, we would still not be allowed to create the array. Possibility of overflow : If program ever needs to process more than the size of array, there is a possibility of overflow and code breaks. Difficulty in insertion and deletion : In case of insertion of a new element, each element after the specified location has to be shifted one position to the right. In case of deletion of an element, each element after the specified location has to be shifted one position to the left. The Linked List is a collection of elements called nodes, each node of which stores two items of information, i.e., data part and link field. -- The data part of each node consists the data record of an entity. -- The link field is a pointer and contains the address of next node. -- The beginning of the linked list is stored in a pointer termed as head which points to the first node. -- The head pointer will be passed as a parameter to any method, to perform an operation. -- First node contains a pointer to second node, second node contains a pointer to the third node and so on. -- The last node in the list has its next field set to NULL to mark the end of the list.

Creating a Singly Linked List struct node { int rollno; struct node *next; }; int main() { struct node *head,*n1,*n2,*n3,*n4; /* creating a new node */ n1=(struct node *) malloc(sizeof(struct node)); n1->rollno=101; n1->next = NULL; /* referencing the first node to head pointer */ head = n1; n2=(struct node *)malloc(sizeof(struct node)); n2->rollno=102; n2->next = NULL; /* linking the second node after first node */ n1->next = n2; /* creating a new node * / n3=(struct node *)malloc(sizeof(struct node)); n3->rollno=104; n3->next=NULL; /* linking the third node after second node */ n2->next = n3; n4=(struct node *)malloc (sizeof (struct node)); n4->rollno=103; n4->next=NULL; /* inserting the new node between second node and third node */ n2->next = n4; n4->next = n3; Creating a Singly Linked List /* deleting n2 node */ n1->next = n4; free(n2); } 150 101 NULL head 150 n1-node 150 101 720 102 NULL 150 720 n1-node n2-node 150 101 720 102 910 104 NULL 150 720 910 n1-node n2-node n3-node 150 101 400 102 720 104 NULL head 150 400 910 n1-node n2-node 103 910 n3-node 720 n4-node 150 101 720 103 910 104 NULL head 150 910 720 n1-node n3-node n4-node 102 720 400 n2-node

Implementing Singly Linked List struct node { int data; struct node *next; }; struct node *createnode() { struct node *new; new = (struct node *)malloc(sizeof(struct node)); printf("\nEnter the data : "); scanf("%d",&new->data); new->next = NULL; return new; } void append(struct node **h) { struct node *new,*temp; new = createnode(); if(*h == NULL) { *h = new; return; temp = *h; while(temp->next!=NULL) temp = temp->next; temp->next = new; void display(struct node *p) { printf("\nContents of the List : \n\n"); while(p!=NULL) { printf("\t%d",p->data); p = p->next; } void insert_after(struct node **h) { struct node *new,*temp; int k; if(*h == NULL) return; printf("\nEnter data of node after which node : "); scanf("%d",&k); temp = *h; while(temp!=NULL && temp->data!=k) temp = temp->next; if(temp!=NULL) { new=createnode(); new->next = temp->next; temp->next = new; void insert_before(struct node **h) { struct node *new,*temp,*prev ; int k; if(*h==NULL) return; printf("\nEnter data of node before which node : "); if((*h)->data == k) { new = createnode(); new->next = *h; *h = new; return; temp = (*h)->next; prev = *h;

Implementing Singly Linked List ( continued ) while(temp!=NULL && temp->data!=k) { prev=temp; temp=temp->next; } if(temp!=NULL) { new = createnode(); new->next = temp; prev->next = new; void delnode(struct node **h) { struct node *temp,*prev; int k; if(*h==NULL) return; printf("\nEnter the data of node to be removed : "); scanf("%d",&k); if((*h)->data==k) { temp=*h; *h=(*h)->next; free(temp); return; temp=(*h)->next; prev=*h; while(temp!=NULL && temp->data!=k) { if(temp!=NULL) { prev->next = temp->next; free(temp); } void search(struct node *h) { struct node *temp; int k; if(h==NULL)return; printf("\nEnter the data to be searched : "); scanf("%d",&k); temp=h; while(temp!=NULL && temp->data!=k) temp=temp->next; (temp==NULL)? printf("\n\t=>Node does not exist") : printf("\n\t=>Node exists"); void destroy(struct node **h) { struct node *p; if(*h==NULL) return; while(*h!=NULL) { p = (*h)->next; free(*h); *h=p; printf("\n\n ******Linked List is destroyed******");

Implementing Singly Linked List ( continued ) int main() { struct node *head=NULL; int ch; while(1) { printf("\n1.Append"); printf("\n2.Display All"); printf("\n3.Insert after a specified node"); printf("\n4.Insert before a specified node"); printf("\n5.Delete a node"); printf("\n6.Search for a node"); printf("\n7.Distroy the list"); printf("\n8.Exit program"); printf("\n\n\tEnter your choice : "); scanf("%d",&ch); switch(ch) { case 1:append(&head);break; case 2:display(head);break; case 3:insert_after(&head);break; case 4:insert_before(&head);break; case 5:delnode(&head);break; case 6:search(head);break; case 7:destroy(&head);break; case 8:exit(0);break; default : printf( "Wrong Choice, Enter correct one : "); } /* function to sort linked list */ void sort(struct node *h) { struct node *p,*temp; int i, j, n, t, sorted=0; temp=h; for(n=0 ; temp!=NULL ; temp=temp->next) n++; for(i=0;i<n-1&&!sorted;i++) { p=h; sorted=1; for(j=0;j<n-(i+1);j++) { if ( p->data > ( p->next )->data ) { t=p->data; p->data =(p->next)->data; (p->next)->data = t; sorted=0; } p=p->next; /* function to count number of node in the list */ int count ( struct node *h) { int i; for( i=0 ; h!=NULL ; h=h->next) i++; return i; }

Algorithm for adding two polynomials in linked lists Add_Polynomial( list p, list q ) set p, q to point to the two first nodes (no headers) initialize a linked list r for a zero polynomial while p != null and q != null if p.exp > q.exp create a node storing p.coeff and p.exp insert at the end of list r advance p else if q.exp > p.exp create a node storing q.coeff and q.exp advance q else if p.exp == q.exp if p.coeff + q.coeff != 0 create a node storing p.coeff + q.coeff and p.exp advance p, q end while if p != null copy the remaining terms of p to end of r else if q != null copy the remaining terms of q to end of r

Tree structure using Doubly Linked List Pitfalls encountered while using singly linked list : A singly linked list allows traversal of the list in forward direction, but not in backward direction. Deleting a node from a list requires keeping track of the previous node,. In the list any node gets corrupted, the remaining nodes of the list become unusable. These problems of singly linked lists can be overcome by doubly linked list. A Doubly Linked List is a data structure having an ordered list of nodes, in which each node consists of two pointers. One pointer is to store the address of next node like in singly linked list. The second pointer stores the address of previous node. It is also known as two-way list. The specialty of DLL is that the list can be traversed in forward as well as backward directions. The concept of DLL is also used to representing tree data structures. head tail A B C /* a node in doubly linked list */ struct node { struct node *prev; int data ; struct node *next; } Tree structure using Doubly Linked List

Insertion of node in Doubly Linked List A q B D C p q A B C D Deletion of node in Doubly Linked List A B C p D A B C

Implementing Doubly Linked List struct node { struct node *prev; int data; struct node *next; }; struct node *createnode() { struct node *new; new = (struct node *)malloc(sizeof(struct node)); printf("\nEnter the data : "); scanf("%d",&new->data); new->prev = NULL; new->next = NULL; return new; } void append(struct node **h) { struct node *new,*temp; new = createnode(); if(*h == NULL) { *h = new; return; temp = *h; while(temp->next!=NULL) temp = temp->next; temp->next = new; new->prev = temp; void forward_display(struct node *p) { printf("\nContents of the List : \n\n"); while(p!=NULL) printf("\t%d",p->data); p = p->next; } printf("\n"); void insert_after(struct node **h) { struct node *new,*temp; int k; if(*h == NULL) return; printf("\nEnter data of node after which node : "); scanf("%d",&k); temp = *h; while(temp!=NULL && temp->data!=k) temp = temp->next; if(temp!=NULL) { new=createnode(); new->next = temp->next; temp->next = new; new->prev = temp; if(new->next != NULL) new->next->prev = new;

Implementing Doubly Linked List ( continued ) void insert_before(struct node **h) { struct node *new,*temp; int k; if(*h==NULL) return; printf("\nEnter data of node before which node : "); scanf("%d",&k); if((*h)->data == k) { new = createnode(); new->next = *h; new->next->prev=new; *h = new; return; } temp = *h; while(temp!=NULL && temp->data!=k) temp=temp->next; if(temp!=NULL) new->next = temp; new->prev = temp->prev; new->prev->next = new; temp->prev = new; void delnode(struct node **h) { struct node *temp; int k; if(*h==NULL) return; printf("\nEnter the data of node to be removed : "); scanf("%d",&k); if((*h)->data==k) temp=*h; *h=(*h)->next; (*h)->prev=NULL; free(temp); } while(temp!=NULL && temp->data!=k) temp=temp->next; if(temp!=NULL) temp->next->prev = temp->prev; temp->prev->next = temp->next;

Implementing Doubly Linked List ( continued ) void search(struct node *h) { struct node *temp; int k; if(h==NULL) return; printf("\nEnter the data to be searched : "); scanf("%d",&k); temp=h; while(temp!=NULL && temp->data!=k) temp=temp->next; if (temp==NULL) printf("\n\t=>Node does not exist") else printf("\n\t=>Node exists"); } void destroy(struct node **h) struct node *p; if(*h==NULL) return; while(*h!=NULL) p = (*h)->next; free(*h); *h=p; printf("\n\n ******Linked List is destroyed******"); int main() { struct node *head=NULL; int ch; while(1) { printf("\n1.Append"); printf("\n2.Display All"); printf("\n3.Insert after a specified node"); printf("\n4.Insert before a specified node"); printf("\n5.Delete a node"); printf("\n6.Search for a node"); printf("\n7.Distroy the list"); printf("\n8.Exit program"); printf("\n\n\tEnter your choice : "); scanf("%d",&ch); switch(ch) { case 1:append(&head);break; case 2:forward_display(head);break; case 3:insert_after(&head);break; case 4:insert_before(&head);break; case 5:delnode(&head);break; case 6:search(head);break; case 7:destroy(&head);break; case 8:exit(0);break; default : printf("Wrong Choice, Enter correct choice : "); }

Circular Doubly Linked List Circular Singly Linked List 910 101 400 102 720 103 910 104 150 tail 150 400 720 910 n1-node n2-node n3-node n4-node -- Singly Linked List has a major drawback. From a specified node, it is not possible to reach any of the preceding nodes in the list. To overcome the drawback, a small change is made to the SLL so that the next field of the last node is pointing to the first node rather than NULL. Such a linked list is called a circular linked list. -- Because it is a circular linked list, it is possible to reach any node in the list from a particular node. -- There is no natural first node or last node because by virtue of the list is circular. -- Therefore, one convention is to let the external pointer of the circular linked list, tail, point to the last node and to allow the following node to be the first node. -- If the tail pointer refers to NULL, means the circular linked list is empty. Circular Doubly Linked List prev data next prev data next prev data next prev data next -- A Circular Doubly Linked List ( CDL ) is a doubly linked list with first node linked to last node and vice-versa. -- The ‘ prev ’ link of first node contains the address of last node and ‘ next ’ link of last node contains the address of first node. -- Traversal through Circular Singly Linked List is possible only in one direction. -- The main advantage of Circular Doubly Linked List ( CDL ) is that, a node can be inserted into list without searching the complete list for finding the address of previous node. -- We can also traversed through CDL in both directions, from first node to last node and vice-versa.

Implementing Circular Singly Linked List struct node { int data; struct node *next; }; struct node *createnode() { struct node *new; new = (struct node *)malloc(sizeof(struct node)); printf("\nEnter the data : "); scanf("%d",&new->data); new->next = NULL; return new; } void append(struct node **t) { struct node *new,*head; new = createnode(); if(*t == NULL) { *t = new; new->next = *t; return; head = (*t)->next; (*t)->next = new; new->next = head; *t = new; void display(struct node *t) { struct node *temp = t->next, *head=t->next; printf("\nContents of the List : \n\n"); do { printf("\t%d",temp->data);temp = temp->next; }while(temp!=head); printf(“\n”); void insert_after(struct node **t) { struct node *new,*temp; int k, found=0; if(*t == NULL) return; printf("\nEnter data of node after which node : "); scanf("%d",&k); if((*t)->data==k) new = createnode(); new->next = (*t)->next; (*t)->next = new; *t=new; return; } temp=(*t)->next; while(temp!=*t) if(temp->data == k) { new->next = temp->next; temp->next = new; found=1; break; temp=temp->next; if(found==0) printf("\nNode does not exist..");

Implementing Circular Singly Linked List ( continued ) void insert_before(struct node **t) { struct node *new,*temp,*prev,*head; int k,found=0; if(*t==NULL) return; printf("\nEnter data of node before which node : "); scanf("%d",&k); head=(*t)->next; if(head->data == k) { new = createnode(); new->next = head; (*t)->next = new; return; } temp = head->next; prev = head; while(temp!=head) { if(temp->data==k) { prev->next = new; new->next = temp; found=1; break; } else { prev=temp; temp=temp->next; if(found==0) printf("\nNode does not exist.."); void delnode(struct node **t) { struct node *temp,*prev,*head; int k,found=0; if(*t==NULL) return; printf("\nEnter the data of node to be removed : "); scanf("%d",&k); head=(*t)->next; if(head->data==k) { temp=head; if(temp->next!=head) (*t)->next=head->next; else *t = NULL; free(temp); return; } temp=head->next; prev=head; while(temp!=head) { if(temp->data == k) { prev->next = temp->next; if(temp==*t) *t = prev; found=1; break; } else { prev=temp; temp=temp->next; if(found==0) printf("\nNode does not exist..");

Implementing Circular Singly Linked List ( continued ) int main() { struct node *tail=NULL; int ch; while(1) { printf("\n1.Append"); printf("\n2.Display All"); printf("\n3.Insert after a specified node"); printf("\n4.Insert before a specified node"); printf("\n5.Delete a node"); printf("\n6.Exit program"); printf("\n\n\tEnter your choice : "); scanf("%d",&ch); switch(ch) { case 1:append(&tail);break; case 2:display(tail);break; case 3:insert_after(&tail);break; case 4:insert_before(&tail);break; case 5:delnode(&tail);break; case 6:exit(0);break; default : printf(“\n\tWrong Choice… “); } Types of Data Structures Data structures are classified in several ways : Linear : Elements are arranged in sequential fashion. Ex : Array, Linear list, stack, queue Non-Linear : Elements are not arranged in sequence. Ex : trees, graphs Homogenous : All Elements are belongs to same data type. Ex : Arrays Non-Homogenous : Different types of Elements are grouped and form a data structure. Ex: classes Dynamic : Memory allocation of each element in the data structure is done before their usage using D.M.A functions Ex : Linked Lists Static : All elements of a data structure are created at the beginning of the program. They cannot be resized. Ex : Arrays