SlideShare a Scribd company logo
KONGUNADU COLLEGE OF ENGINEERING AND
TECHNOLOGY
DEPARTMENT OF INFORMATION TECHNOLOGY
20CS301 - DATA STRUCTURES
Subject Handled by
Mrs. R.Aruna,AP / IT
KNCET.
UNIT - I LINKEDLIST
Abstract Data types (ADTs) — List ADT — Array-
based implementation — Linked list
implementation — Singly linked lists — Circularly
linked lists — Doubly linked lists — Applications
of lists — Polynomial Manipulation.
UNIT - II STACKS AND QUEUES
Stack ADT — Operations — Applications —
Evaluating arithmetic expressions — Conversion of
Infix to postfix expression — Queue ADT —
Operations — Implementation — Applications of
Queues.
UNIT-III TREES
Tree ADT — Tree Traversals — Binary Tree ADT —
Expression Trees — Applications of Trees — Binary
Search Tree ADT — AVL Trees — Splay Tree — B
—Tree — Heap — Applications of heap.
UNIT IV GRAPHS
Definitions — Topological Sort — Shortest-Path
Algorithms — Dijkstra's Algorithm Minimum
Spanning Tree — Prim's algorithms — Depth-First
Traversal — Biconnectivity Euler circuits —
Applications of graphs.
UNIT V SORTING AND HASHING TECHNIQUES
Sorting — Merge sort — Quick sort — Insertion sort —
Shell sort — Radix sort; Hashing — Hash Functions —
Collision resolution techniques — Linear Probing —
Separate Chaining — Open Addressing — Rehashing —
Extendible Hashing.
INTRODUCTION TO DATA STRUCTURES AND
LIST ADT
DEFINITION:
• Data Structure is a way of organizing, storing and retrieving
data and their relationships with each other
CLASSIFICATION OF DATA STRUCTURES
ADT –ABSTRACT DATA TYPE
• It is a type for objects whose behavior is defined by set of values and set
of operations from user point of view
• Mentions only what operations are to be performed but not how they will
be implemented
• Thus, ADT is the specification of a data type within some language,
independent of implementation
ADT –ABSTRACT DATA TYPE
• Examples - List ADT, Stack ADT, Queue ADT, etc., Some
operations on these ADT are:
List:
•insert(x)
•delete(x)
•find(x)
Stack:
•Push (x)
•Pop()
•isFull()
•isEmpty()
Queue:
•enqueue()
•dequeue()
•isFull()
•isEmpty()
LIST ADT
• List ADT can be represented in two ways
– Array
– Linked List
ARRAY ADT
• Collection of elements of same data type
• Elements in an array is stored in adjacent memory location
• Syntax:
datatype arrayname [size]
Eg : int a [50];
Operations:
• insertion()
• deletion()
• search()
ARRAY ADT
Eg : int arr [6];
ISSUES IN ARRAY
• Fixed size: Resizing is expensive
• Requires continuous memory for allocation
– Difficult when array size is larger
• Insertions and deletions are inefficient
– Elements are usually shifted
• Wastage of memory space unless the array is full
ARRAY IMPLEMENTATION OF LIST ADT
Output:
Main Menu
1.Create 2.Delete 3.Search 4.Insert 5.Display 6.Exit
Enter your Choice 1
Enter the number of nodes 3
Enter the Element 8 9 7
Main Menu
1.Create 2.Delete 3.Search 4.Insert 5.Display 6.Exit
Enter your Choice 2
Enter the position u want to delete 2
The Elements after deletion 8 7
Main Menu
1.Create 2.Delete 3.Search 4.Insert 5.Display 6.Exit
Enter your Choice 4
Enter the position u need to insert 2
Enter the element to insert 6
The list after insertion 8 6 7
Main Menu
1.Create 2.Delete 3.Search 4.Insert 5.Display 6.Exit
Enter your Choice 6
CREATE
void create()
{
printf(“n Enter the number of nodes”);
scanf(“%d”, &n);
for(i=0;i<n;i++)
{
printf(“n Enter the Element:”,i+1);
scanf(“%d”, &b[i]);
}
}
DELETE
void deletion()
{
printf(“n Enter the position u want to delete::”);
scanf(“%d”, &pos);
if(pos>=n)
{
printf(“n Invalid Location::”);
}
else
{
for(i=pos+1;i<n;i++)
{
b[i-1]=b[i];
}
n—;
}
printf(“n The Elements after deletion”);
for(i=0;i<n;i++)
{
printf(“t%d”, b[i]);
}
}
SEARCH
void search()
{
printf(“n Enter the Element to be searched:”);
scanf(“%d”, &e);
for(i=0;i<n;i++)
{
if(b[i]==e)
{
printf(“Value is in the %d Position”, i);
}
else
{
printf(“Value %d is not in the list::”, e);
continue;
}
INSERT
void insert()
{
printf(“n Enter the position u need to insert::”);
scanf(“%d”, &pos);
if(pos>=n)
{
printf(“n invalid Location::”);
}
else
{
for(i=MAX-1;i>=pos-1;i—)
{
b[i+1]=b[i];
}
printf(“n Enter the element to insert::n”);
scanf(“%d”,&p);
b[pos]=p;
n++;
}
printf(“n The list after insertion::n”);
display();
}
DISPLAY
void display()
{
printf(“n The Elements of The list ADT are:”);
for(i=0;i<n;i++)
{
printf(“nn%d”, b[i]);
}
LINKED LIST ADT
• Linked list is a linear data structure
• Made up of chain of nodes
• Each node contains a data and a pointer to the next node in the
chain
• Last node of the list is pointed to NULL
REPRESENTATION OF NODE IN LINKED LIST
• Each node contains two fields:
– Data field: Contains data element to be stored
– Pointer field: Contains the address of next node
Declaration of a node:
struct node
{
int data;
struct node *next;
};
CREATING A NODE
Statement to create a node:
n= (struct node*) malloc(sizeof(struct node))
nnext=NULL;
Struct node
{
Int data;
Struct node *next;
} *n;
TYPES OF LINKED LIST
• Singly linked list
• Doubly linked list
• Circular linked list
SINGLY LINKED LIST (SLL)
• Each node contains only one pointer field that contains the
address of the next node in the list
OPERATIONS ON SINGLY LINKED LIST
Major operations are:
•Insertion
–At the beginning
–At the middle
–At the end
•Deletion
–At the beginning
–At the middle
–At the end
•Search
Global declaration of a Node:
struct node
{
int data;
struct node *next;
} *head, *tail, *n, *t;
ROUTINE FOR INSERTION AT THE BEGINNING
void ins_beg(int num) //Let num=10
{
n=(struct node *) malloc(size of(struct node));
nnext=NULL;
ndata=num;
if (head==NULL) //case 1
{
head=n;
tail=n;
}
else //case 2
{
nnext=head;
head=n;
}
}
ROUTINE FOR INSERTION AT THE END
void ins_end(int num) //Let num=10
{
n=(struct node *) malloc(size of(struct node));
nnext=NULL;
ndata=num;
if (head==NULL) //case 1
{
head=n;
tail=n;
}
else //case 2
{
tailnext=n;
tail=n;
}
}
ROUTINE FOR INSERTION AT THE MIDDLE
void ins_end(int num, int mid_data) //Let num=10, mid_data=9
{
n=(struct node *) malloc(size of(struct node));
nnext=NULL;
ndata=num;
for(t=head; t!=NULL; t=tnext)
{
if(tdata==mid_data)
break;
}
nnext=tnext;
tnext=n;
}
10
300
83==9? NO 9==9? YES
10
300
1500
300
ROUTINE FOR DELETION AT THE BEGINNING
void del_beg()
{
t=head;
head=tnext;
free(t);
}
ROUTINE FOR DELETION AT THE END
void del_end()
{
Struct node *tp;
t=head;
while(tnext!=NULL)
{
tp=t;
t=tnext;
}
tail=tp;
tailnext=NULL;
free(t);
}
1000 850 1500 2500
ROUTINE FOR DELETION AT THE MIDDLE
void del_mid(int num) //Let num=70
{
Struct node *tp;
t=head;
while(tdata!=num)
{
tp=t;
t=tnext;
}
tpnext=tnext;
free(t);
}
83!=70? Yes 9!=70? Yes
2800
70!=70? NO
1500
ROUTINE FOR SEARCH AN ELEMENT
Struct node* search (int num) //Let num=9
{
t=head;
while(t!=NULL && tdata!=num)
{
t=tnext;
}
return t;
}
LINKED LIST –ADVANTAGES & DISADVANTAGES
Advantages:
•No wastage of memory
–Memory allocated and deallocated as per requirement
•Efficient insertion and deletion operations
Disadvantages:
•Occupies more space than array to store a data
–Due to the need of a pointer to the next node
•Does not support random or direct access
DOUBLY LINKED LIST (DLL)
•Type of linked list
•Each node contains two pointer fields that contains the address of
the next node and that of previous node in the list
REPRESENTATION OF NODE IN DLL
•Each node contains three fields:
–Data: Contains data element to be stored
–next: Contains the address of next node
–prev: Contains address of previous node
Declaration of a node:
structnode
{
intdata;
structnode *next, *prev;
};
OPERATIONS ON DOUBLY LINKED LIST
Major operations are:
•Insertion
–At the beginning
–At the middle
–At the end
•Deletion
–At the beginning
–At the middle
–At the end
•Search
Global declaration of a Node:
structnode
{
intdata;
structnode *next, prev;
} *head, *tail, *n, *t;
ROUTINE FOR INSERTION AT THE BEGINNING
void ins_beg_dll(intnum) //Let num=70
{
n=(struct node *) malloc(size of(struct node));
nnext=NULL;
nprev=NULL;
ndata=num;
if(head==NULL) //case 1
{
head=n;
tail=n;
}
else //case 2
{
nnext=head;
headprev=n;
head=n;
}
}
70
1000
500
500
ROUTINE FOR INSERTION AT THE END
void ins_end_dll(int num) //Let num=10
{
n=(structnode *) malloc(size of(struct node));
nnext=NULL;
nprev=NULL;
ndata=num;
if (head==NULL) //case 1
{
head=n;
tail=n;
}
else //case 2
{
tailnext=n;
nprev=tail;
tail=n;
}
}
500
500
2500
ROUTINE FOR INSERTION AT THE MIDDLE
void ins_end_dll(intnum, intmid_data) //Let num=10, mid_data=9
{
n=(structnode *) malloc(size of(structnode));
nnext=NULL;
nprev=NULL;
ndata=num;
for(t=head; t!=NULL; t=tnext)
{
if(tdata==mid_data)
break;
}
nnext=tnext;
nprev=t;
tnextprev=n;
tnext=n;
}
500
500
2500
ROUTINE FOR DELETION AT THE BEGINNING
void del_beg_dll()
{
t=head;
head=headnext;
headprev=NULL;
free(t);
}
t is freed
ROUTINE FOR DELETION AT THE END
void del_end_dll()
{
t=head;
while(tnext!=NULL)
{
t=tnext;
}
tail=tailprev;//tail=tprev;
tailnext=NULL;
free(t);
}
t is freed
ROUTINE FOR DELETION AT THE MIDDLE
void del_mid_dll(int num) //Let num=9
{
t=head;
while(tdata!=num)
{
t=tnext;
}
tprevnext=tnext;
tnextprev=tprev;
free(t);
}
9 != 9 NO
1500 1000
free t
ROUTINE FOR SEARCH AN ELEMENT
structnode* search_dll(intnum) //Let num=9
{
t=head;
while(t!=NULL && tdata!=num)
{
t=tnext;
}
return t;
}
9 != 9 NO
DLL –ADVANTAGES & DISADVANTAGES
Advantages:
•Can traverse in both directions
•Operations in the middle of the list can be done efficiently
–No need of extra pointer (tp) to track the previous node
•Reversing the list is easy
Disadvantage:
•Space required to store a data is even more higher than SLL
–Due to the use of two pointers
CIRCULAR LINKED LIST (CLL)
•Type of linked list
•The last node will be pointing to the first node
•It can be either singly or doubly linked
Global declaration of a node:
Struct node
{
Int data;
Struct node *next, *prev;
}*head,*n,*t;
1000
ROUTINE FOR INSERTION AT THE BEGINNING
void ins_beg_cll(int num) //Let num=10
{
n=(struct node *) malloc(size of(struct node));
nnext=NULL;
ndata=num;
if (head==NULL) //case 1
{ head=n;
nnext=head;
}
else //case 2
{ t=head;
while(tnext!=head)
t=tnext;
tnext=n;
nnext=head;
head=n;
}
}
X
10
850 != 1000 YES 1500 != 1000 YES 2500 != 1000 YES 1000 != 1000 NO
1000
X
10
100
100
1000 100
ROUTINE FOR INSERTION AT THE END
void ins_end_cll(int num) //Let num=10
{
n=(struct node *) malloc(size of(struct node));
nnext=NULL;
ndata=num;
t=head;
while(tnext!=head)
t=tnext;
tnext=n;
nnext=head;
}
1000
X
10
100
X
10
100
100 1000
ROUTINE FOR INSERTION AT THE MIDDLE
void ins_end_cll(int num, int mid_data) //Let num=10, mid_data=9
{
n=(struct node *) malloc(size of(struct node));
nnext=NULL;
ndata=num;
for(t=head; tnext!=head; t=tnext)
{
if(tdata==mid_data)
break;
}
nnext=tnext;
tnext=n;
}
10
300
83==9 NO 9==9? YES
10
300
1500
300
1000
300
1000
ROUTINE FOR DELETION AT THE BEGINNING
void del_beg_cll()
{
Struct node *t1;
t=head;
t1=head;
while(tnext!=head)
t=tnext;
tnext=headnext;
head=tnext;
free(t1);
}
850 != 1000 YES 1500 != 1000 YES 2500 != 1000 YES 1000 != 1000 NO
850
ROUTINE FOR DELETION AT THE END
void del_end_cll()
{
Struct node *tp;
t=head;
while(tnext!=head)
{
tp=t;
t=tnext;
}
tpnext=head;
free(t);
}
1000
1000
ROUTINE FOR DELETION AT THE MIDDLE
void del_mid_cll(int num) //Let num=9
{
Struct node *tp;
t=head;
while(tdata!=num)
{
tp=t;
t=tnext;
}
tpnext=tnext;
free(t);
}
1000
1500
ROUTINE FOR SEARCH
Struct node* search_cll (int num) //Let num=9
{
t=head;
while(tnext!=head && tdata!=num)
{
t=tnext;
}
return t;
}
CLL –ADVANTAGES & DISADVANTAGES
Advantage:
•Comparing to SLL, moving to any node from a node is
possible
Disadvantages:
•Reversing a list is complex compared to linear linked list
•If proper care is not taken in managing the pointers, it
leads to infinite loop
•Moving to previous node is difficult, as it is needed to
complete an entire circle
sll5.cpp
APPLICATIONS OF LIST
Lists can be used to
•Sort elements
•Implement stack and queue
•Represent graph (adjacent list representation)
•Implement hash table
•Implement multiprocessing of applications
•Manipulate polynomial equations
POLYNOMIAL ADT
•A polynomial equation is an equation that has multiple
terms made up of numbers and variables. Polynomials
can have different exponents.
•Polynomial manipulations such as addition, subtraction
can be performed.
Examples of polynomial equations:
–9x5
+ 7x3
– 4x2
+ 8
7x4
– 17x3
– 8x2
To store the data of polynomial equations in the linked list, each
node contains two data fields, namely coefficient, power and one
next pointer field
struct node
{
int coeff;
int pow;
struct node *next;
}*n;
CREATING A POLYNOMIAL LIST
void create_poly(int c, int p, struct node *t)
{ struct node *head, *tail;
//head=t;
n=(struct node*) malloc(size of(struct node));
n->coeff=c;
n->pow=p;
n->next=null;
if (head==null)
{ head=n;
tail=n;
}
else
{ tail->next=n;
tail=n;
}
return head;
}
POLYNOMIAL ADDITION
Void add_poly(struct node *poly1, struct node *poly2, struct node *poly)
{
while(poly1!=NULL && poly2!=NULL)
{
if(poly1pow>poly2pow)
{
polypow=poly1pow;
polycoeff=poly1coeff;
ploy1=poly1next;
}
else if(poly1pow < poly2pow)
{
polypow=poly2pow;
polycoeff=poly2coeff;
poly2=poly2next;
}
POLYNOMIAL ADDITION CONTD.,
else
{ polypow=ploy1pow;
polycoeff=poly1coeff + poly2coeff;
ploy1=poly1next;
ploy2=poly2next;
}
}
while(poly1 != NULL || poly2 != NULL)
{ if (poly1 != NULL)
{ polypow=ploy1pow;
polycoeff=poly1coeff;
ploy1=poly1next;
}
if (poly2 != NULL)
{ polypow=ploy2pow;
polycoeff=poly2coeff;
ploy2=poly2next;
} } }
POLYNOMIAL SUBTRACTION
else
{ polypow=ploy1pow;
polycoeff=poly1coeff - poly2coeff;
ploy1=poly1next;
ploy2=poly2next;
}
}
while(poly1 != NULL || poly2 != NULL)
{ if (poly1 != NULL)
{ polypow=ploy1pow;
polycoeff=poly1coeff;
ploy1=poly1next;
}
if (poly2 != NULL)
{ polypow=ploy2pow;
polycoeff=poly2coeff;
ploy2=poly2next;
} } }
RADIX SORT:
• Radix sort is one of the sorting algorithms used to sort a
list of integer numbers in order. In radix sort algorithm,
a list of integer numbers will be sorted based on the
digits of individual numbers.
• Radix sort algorithm requires the number of passes
which are equal to the number of digits present in the
largest number among the list of numbers.
• For example, if the largest number is a 3 digit number
then that list is sorted with 3 passes.
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
Multi-Linked Lists
•A multilinked list is a more general linked list with multiple
links from nodes.
•In a general multi-linked list each node can have any
number of pointers to other nodes,
•Multi-lists are essentially the technique of embedding
multiple lists into a single data structure.
•A multi-list has more than one next pointer, like a doubly
linked list, but the pointers create separate lists

More Related Content

PPTX
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
PPTX
Unit 1 LINEAR DATA STRUCTURES
PPTX
Linked List Data structure using C programming and all the detailed informat...
PPTX
Data Structures - Unit 1 linked list
PPTX
EC2311 – Data Structures and C Programming
PPTX
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
PDF
Fjdkkdnncmckkgkhkhkkhkhkhkhkhkhkhkhkhkhhl
PPT
Unit i(dsc++)
UNIT I LINEAR DATA STRUCTURES – LIST .pptx
Unit 1 LINEAR DATA STRUCTURES
Linked List Data structure using C programming and all the detailed informat...
Data Structures - Unit 1 linked list
EC2311 – Data Structures and C Programming
8.DATA STRUCTURES UNIT 1 AND 2 CS3301PPT.pptx
Fjdkkdnncmckkgkhkhkkhkhkhkhkhkhkhkhkhkhhl
Unit i(dsc++)

Similar to UNIT I LINEAR DATA STRUCTURES – LIST .pptx (20)

PDF
Data and File Structure Lecture Notes
PPT
Data structures
PPT
Sorting & Linked Lists
PPTX
Data structures - unit 1
PPTX
Data structures and algorithms
PPTX
List,Stacks and Queues.pptx
PPT
Linked list introduction and different operation
PPTX
unit 1.pptx
PDF
Data Structures Mastery: Sample Paper for Practice"
PPT
linkedlist (1).ppt
PPTX
Data structure
PPT
Data Structures and algorithms using c .ppt
PDF
Unit - 2.pdf
PPTX
linkedlist.pptx
PPTX
Adt of lists
PPT
lecture 02.2.ppt
PPTX
Stack, queue, linked list
PPTX
Stack, Queue, Linked List.pptx
PDF
linked lists in data structures
PPT
Linked list1.ppt
Data and File Structure Lecture Notes
Data structures
Sorting & Linked Lists
Data structures - unit 1
Data structures and algorithms
List,Stacks and Queues.pptx
Linked list introduction and different operation
unit 1.pptx
Data Structures Mastery: Sample Paper for Practice"
linkedlist (1).ppt
Data structure
Data Structures and algorithms using c .ppt
Unit - 2.pdf
linkedlist.pptx
Adt of lists
lecture 02.2.ppt
Stack, queue, linked list
Stack, Queue, Linked List.pptx
linked lists in data structures
Linked list1.ppt
Ad

Recently uploaded (20)

PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
Geodesy 1.pptx...............................................
PDF
composite construction of structures.pdf
PPTX
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
PPTX
Construction Project Organization Group 2.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
additive manufacturing of ss316l using mig welding
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
web development for engineering and engineering
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
CH1 Production IntroductoryConcepts.pptx
Internet of Things (IOT) - A guide to understanding
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Model Code of Practice - Construction Work - 21102022 .pdf
Geodesy 1.pptx...............................................
composite construction of structures.pdf
Fluid Mechanics, Module 3: Basics of Fluid Mechanics
Construction Project Organization Group 2.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
OOP with Java - Java Introduction (Basics)
additive manufacturing of ss316l using mig welding
CYBER-CRIMES AND SECURITY A guide to understanding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
web development for engineering and engineering
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Embodied AI: Ushering in the Next Era of Intelligent Systems
MET 305 MODULE 1 KTU 2019 SCHEME 25.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Ad

UNIT I LINEAR DATA STRUCTURES – LIST .pptx

  • 1. KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF INFORMATION TECHNOLOGY 20CS301 - DATA STRUCTURES Subject Handled by Mrs. R.Aruna,AP / IT KNCET.
  • 2. UNIT - I LINKEDLIST Abstract Data types (ADTs) — List ADT — Array- based implementation — Linked list implementation — Singly linked lists — Circularly linked lists — Doubly linked lists — Applications of lists — Polynomial Manipulation.
  • 3. UNIT - II STACKS AND QUEUES Stack ADT — Operations — Applications — Evaluating arithmetic expressions — Conversion of Infix to postfix expression — Queue ADT — Operations — Implementation — Applications of Queues.
  • 4. UNIT-III TREES Tree ADT — Tree Traversals — Binary Tree ADT — Expression Trees — Applications of Trees — Binary Search Tree ADT — AVL Trees — Splay Tree — B —Tree — Heap — Applications of heap.
  • 5. UNIT IV GRAPHS Definitions — Topological Sort — Shortest-Path Algorithms — Dijkstra's Algorithm Minimum Spanning Tree — Prim's algorithms — Depth-First Traversal — Biconnectivity Euler circuits — Applications of graphs.
  • 6. UNIT V SORTING AND HASHING TECHNIQUES Sorting — Merge sort — Quick sort — Insertion sort — Shell sort — Radix sort; Hashing — Hash Functions — Collision resolution techniques — Linear Probing — Separate Chaining — Open Addressing — Rehashing — Extendible Hashing.
  • 7. INTRODUCTION TO DATA STRUCTURES AND LIST ADT
  • 8. DEFINITION: • Data Structure is a way of organizing, storing and retrieving data and their relationships with each other
  • 10. ADT –ABSTRACT DATA TYPE • It is a type for objects whose behavior is defined by set of values and set of operations from user point of view • Mentions only what operations are to be performed but not how they will be implemented • Thus, ADT is the specification of a data type within some language, independent of implementation
  • 11. ADT –ABSTRACT DATA TYPE • Examples - List ADT, Stack ADT, Queue ADT, etc., Some operations on these ADT are: List: •insert(x) •delete(x) •find(x) Stack: •Push (x) •Pop() •isFull() •isEmpty() Queue: •enqueue() •dequeue() •isFull() •isEmpty()
  • 12. LIST ADT • List ADT can be represented in two ways – Array – Linked List
  • 13. ARRAY ADT • Collection of elements of same data type • Elements in an array is stored in adjacent memory location • Syntax: datatype arrayname [size] Eg : int a [50]; Operations: • insertion() • deletion() • search()
  • 14. ARRAY ADT Eg : int arr [6];
  • 15. ISSUES IN ARRAY • Fixed size: Resizing is expensive • Requires continuous memory for allocation – Difficult when array size is larger • Insertions and deletions are inefficient – Elements are usually shifted • Wastage of memory space unless the array is full
  • 16. ARRAY IMPLEMENTATION OF LIST ADT Output: Main Menu 1.Create 2.Delete 3.Search 4.Insert 5.Display 6.Exit Enter your Choice 1 Enter the number of nodes 3 Enter the Element 8 9 7 Main Menu 1.Create 2.Delete 3.Search 4.Insert 5.Display 6.Exit Enter your Choice 2 Enter the position u want to delete 2 The Elements after deletion 8 7 Main Menu 1.Create 2.Delete 3.Search 4.Insert 5.Display 6.Exit Enter your Choice 4 Enter the position u need to insert 2 Enter the element to insert 6 The list after insertion 8 6 7 Main Menu 1.Create 2.Delete 3.Search 4.Insert 5.Display 6.Exit Enter your Choice 6
  • 17. CREATE void create() { printf(“n Enter the number of nodes”); scanf(“%d”, &n); for(i=0;i<n;i++) { printf(“n Enter the Element:”,i+1); scanf(“%d”, &b[i]); } }
  • 18. DELETE void deletion() { printf(“n Enter the position u want to delete::”); scanf(“%d”, &pos); if(pos>=n) { printf(“n Invalid Location::”); } else { for(i=pos+1;i<n;i++) { b[i-1]=b[i]; } n—; } printf(“n The Elements after deletion”); for(i=0;i<n;i++) { printf(“t%d”, b[i]); } }
  • 19. SEARCH void search() { printf(“n Enter the Element to be searched:”); scanf(“%d”, &e); for(i=0;i<n;i++) { if(b[i]==e) {
  • 20. printf(“Value is in the %d Position”, i); } else { printf(“Value %d is not in the list::”, e); continue; }
  • 21. INSERT void insert() { printf(“n Enter the position u need to insert::”); scanf(“%d”, &pos); if(pos>=n) { printf(“n invalid Location::”); } else {
  • 22. for(i=MAX-1;i>=pos-1;i—) { b[i+1]=b[i]; } printf(“n Enter the element to insert::n”); scanf(“%d”,&p); b[pos]=p; n++; } printf(“n The list after insertion::n”); display(); }
  • 23. DISPLAY void display() { printf(“n The Elements of The list ADT are:”); for(i=0;i<n;i++) { printf(“nn%d”, b[i]); }
  • 24. LINKED LIST ADT • Linked list is a linear data structure • Made up of chain of nodes • Each node contains a data and a pointer to the next node in the chain • Last node of the list is pointed to NULL
  • 25. REPRESENTATION OF NODE IN LINKED LIST • Each node contains two fields: – Data field: Contains data element to be stored – Pointer field: Contains the address of next node Declaration of a node: struct node { int data; struct node *next; };
  • 26. CREATING A NODE Statement to create a node: n= (struct node*) malloc(sizeof(struct node)) nnext=NULL; Struct node { Int data; Struct node *next; } *n;
  • 27. TYPES OF LINKED LIST • Singly linked list • Doubly linked list • Circular linked list
  • 28. SINGLY LINKED LIST (SLL) • Each node contains only one pointer field that contains the address of the next node in the list
  • 29. OPERATIONS ON SINGLY LINKED LIST Major operations are: •Insertion –At the beginning –At the middle –At the end •Deletion –At the beginning –At the middle –At the end •Search Global declaration of a Node: struct node { int data; struct node *next; } *head, *tail, *n, *t;
  • 30. ROUTINE FOR INSERTION AT THE BEGINNING void ins_beg(int num) //Let num=10 { n=(struct node *) malloc(size of(struct node)); nnext=NULL; ndata=num; if (head==NULL) //case 1 { head=n; tail=n; } else //case 2 { nnext=head; head=n; } }
  • 31. ROUTINE FOR INSERTION AT THE END void ins_end(int num) //Let num=10 { n=(struct node *) malloc(size of(struct node)); nnext=NULL; ndata=num; if (head==NULL) //case 1 { head=n; tail=n; } else //case 2 { tailnext=n; tail=n; } }
  • 32. ROUTINE FOR INSERTION AT THE MIDDLE void ins_end(int num, int mid_data) //Let num=10, mid_data=9 { n=(struct node *) malloc(size of(struct node)); nnext=NULL; ndata=num; for(t=head; t!=NULL; t=tnext) { if(tdata==mid_data) break; } nnext=tnext; tnext=n; } 10 300 83==9? NO 9==9? YES 10 300 1500 300
  • 33. ROUTINE FOR DELETION AT THE BEGINNING void del_beg() { t=head; head=tnext; free(t); }
  • 34. ROUTINE FOR DELETION AT THE END void del_end() { Struct node *tp; t=head; while(tnext!=NULL) { tp=t; t=tnext; } tail=tp; tailnext=NULL; free(t); } 1000 850 1500 2500
  • 35. ROUTINE FOR DELETION AT THE MIDDLE void del_mid(int num) //Let num=70 { Struct node *tp; t=head; while(tdata!=num) { tp=t; t=tnext; } tpnext=tnext; free(t); } 83!=70? Yes 9!=70? Yes 2800 70!=70? NO 1500
  • 36. ROUTINE FOR SEARCH AN ELEMENT Struct node* search (int num) //Let num=9 { t=head; while(t!=NULL && tdata!=num) { t=tnext; } return t; }
  • 37. LINKED LIST –ADVANTAGES & DISADVANTAGES Advantages: •No wastage of memory –Memory allocated and deallocated as per requirement •Efficient insertion and deletion operations Disadvantages: •Occupies more space than array to store a data –Due to the need of a pointer to the next node •Does not support random or direct access
  • 38. DOUBLY LINKED LIST (DLL) •Type of linked list •Each node contains two pointer fields that contains the address of the next node and that of previous node in the list
  • 39. REPRESENTATION OF NODE IN DLL •Each node contains three fields: –Data: Contains data element to be stored –next: Contains the address of next node –prev: Contains address of previous node Declaration of a node: structnode { intdata; structnode *next, *prev; };
  • 40. OPERATIONS ON DOUBLY LINKED LIST Major operations are: •Insertion –At the beginning –At the middle –At the end •Deletion –At the beginning –At the middle –At the end •Search Global declaration of a Node: structnode { intdata; structnode *next, prev; } *head, *tail, *n, *t;
  • 41. ROUTINE FOR INSERTION AT THE BEGINNING void ins_beg_dll(intnum) //Let num=70 { n=(struct node *) malloc(size of(struct node)); nnext=NULL; nprev=NULL; ndata=num; if(head==NULL) //case 1 { head=n; tail=n; } else //case 2 { nnext=head; headprev=n; head=n; } } 70 1000 500 500
  • 42. ROUTINE FOR INSERTION AT THE END void ins_end_dll(int num) //Let num=10 { n=(structnode *) malloc(size of(struct node)); nnext=NULL; nprev=NULL; ndata=num; if (head==NULL) //case 1 { head=n; tail=n; } else //case 2 { tailnext=n; nprev=tail; tail=n; } } 500 500 2500
  • 43. ROUTINE FOR INSERTION AT THE MIDDLE void ins_end_dll(intnum, intmid_data) //Let num=10, mid_data=9 { n=(structnode *) malloc(size of(structnode)); nnext=NULL; nprev=NULL; ndata=num; for(t=head; t!=NULL; t=tnext) { if(tdata==mid_data) break; } nnext=tnext; nprev=t; tnextprev=n; tnext=n; } 500 500 2500
  • 44. ROUTINE FOR DELETION AT THE BEGINNING void del_beg_dll() { t=head; head=headnext; headprev=NULL; free(t); } t is freed
  • 45. ROUTINE FOR DELETION AT THE END void del_end_dll() { t=head; while(tnext!=NULL) { t=tnext; } tail=tailprev;//tail=tprev; tailnext=NULL; free(t); } t is freed
  • 46. ROUTINE FOR DELETION AT THE MIDDLE void del_mid_dll(int num) //Let num=9 { t=head; while(tdata!=num) { t=tnext; } tprevnext=tnext; tnextprev=tprev; free(t); } 9 != 9 NO 1500 1000 free t
  • 47. ROUTINE FOR SEARCH AN ELEMENT structnode* search_dll(intnum) //Let num=9 { t=head; while(t!=NULL && tdata!=num) { t=tnext; } return t; } 9 != 9 NO
  • 48. DLL –ADVANTAGES & DISADVANTAGES Advantages: •Can traverse in both directions •Operations in the middle of the list can be done efficiently –No need of extra pointer (tp) to track the previous node •Reversing the list is easy Disadvantage: •Space required to store a data is even more higher than SLL –Due to the use of two pointers
  • 49. CIRCULAR LINKED LIST (CLL) •Type of linked list •The last node will be pointing to the first node •It can be either singly or doubly linked Global declaration of a node: Struct node { Int data; Struct node *next, *prev; }*head,*n,*t; 1000
  • 50. ROUTINE FOR INSERTION AT THE BEGINNING void ins_beg_cll(int num) //Let num=10 { n=(struct node *) malloc(size of(struct node)); nnext=NULL; ndata=num; if (head==NULL) //case 1 { head=n; nnext=head; } else //case 2 { t=head; while(tnext!=head) t=tnext; tnext=n; nnext=head; head=n; } } X 10 850 != 1000 YES 1500 != 1000 YES 2500 != 1000 YES 1000 != 1000 NO 1000 X 10 100 100 1000 100
  • 51. ROUTINE FOR INSERTION AT THE END void ins_end_cll(int num) //Let num=10 { n=(struct node *) malloc(size of(struct node)); nnext=NULL; ndata=num; t=head; while(tnext!=head) t=tnext; tnext=n; nnext=head; } 1000 X 10 100 X 10 100 100 1000
  • 52. ROUTINE FOR INSERTION AT THE MIDDLE void ins_end_cll(int num, int mid_data) //Let num=10, mid_data=9 { n=(struct node *) malloc(size of(struct node)); nnext=NULL; ndata=num; for(t=head; tnext!=head; t=tnext) { if(tdata==mid_data) break; } nnext=tnext; tnext=n; } 10 300 83==9 NO 9==9? YES 10 300 1500 300 1000 300
  • 53. 1000 ROUTINE FOR DELETION AT THE BEGINNING void del_beg_cll() { Struct node *t1; t=head; t1=head; while(tnext!=head) t=tnext; tnext=headnext; head=tnext; free(t1); } 850 != 1000 YES 1500 != 1000 YES 2500 != 1000 YES 1000 != 1000 NO 850
  • 54. ROUTINE FOR DELETION AT THE END void del_end_cll() { Struct node *tp; t=head; while(tnext!=head) { tp=t; t=tnext; } tpnext=head; free(t); } 1000 1000
  • 55. ROUTINE FOR DELETION AT THE MIDDLE void del_mid_cll(int num) //Let num=9 { Struct node *tp; t=head; while(tdata!=num) { tp=t; t=tnext; } tpnext=tnext; free(t); } 1000 1500
  • 56. ROUTINE FOR SEARCH Struct node* search_cll (int num) //Let num=9 { t=head; while(tnext!=head && tdata!=num) { t=tnext; } return t; }
  • 57. CLL –ADVANTAGES & DISADVANTAGES Advantage: •Comparing to SLL, moving to any node from a node is possible Disadvantages: •Reversing a list is complex compared to linear linked list •If proper care is not taken in managing the pointers, it leads to infinite loop •Moving to previous node is difficult, as it is needed to complete an entire circle
  • 59. APPLICATIONS OF LIST Lists can be used to •Sort elements •Implement stack and queue •Represent graph (adjacent list representation) •Implement hash table •Implement multiprocessing of applications •Manipulate polynomial equations
  • 60. POLYNOMIAL ADT •A polynomial equation is an equation that has multiple terms made up of numbers and variables. Polynomials can have different exponents. •Polynomial manipulations such as addition, subtraction can be performed.
  • 61. Examples of polynomial equations: –9x5 + 7x3 – 4x2 + 8 7x4 – 17x3 – 8x2 To store the data of polynomial equations in the linked list, each node contains two data fields, namely coefficient, power and one next pointer field struct node { int coeff; int pow; struct node *next; }*n;
  • 62. CREATING A POLYNOMIAL LIST void create_poly(int c, int p, struct node *t) { struct node *head, *tail; //head=t; n=(struct node*) malloc(size of(struct node)); n->coeff=c; n->pow=p; n->next=null; if (head==null) { head=n; tail=n; } else { tail->next=n; tail=n; } return head; }
  • 63. POLYNOMIAL ADDITION Void add_poly(struct node *poly1, struct node *poly2, struct node *poly) { while(poly1!=NULL && poly2!=NULL) { if(poly1pow>poly2pow) { polypow=poly1pow; polycoeff=poly1coeff; ploy1=poly1next; } else if(poly1pow < poly2pow) { polypow=poly2pow; polycoeff=poly2coeff; poly2=poly2next; }
  • 64. POLYNOMIAL ADDITION CONTD., else { polypow=ploy1pow; polycoeff=poly1coeff + poly2coeff; ploy1=poly1next; ploy2=poly2next; } } while(poly1 != NULL || poly2 != NULL) { if (poly1 != NULL) { polypow=ploy1pow; polycoeff=poly1coeff; ploy1=poly1next; } if (poly2 != NULL) { polypow=ploy2pow; polycoeff=poly2coeff; ploy2=poly2next; } } }
  • 65. POLYNOMIAL SUBTRACTION else { polypow=ploy1pow; polycoeff=poly1coeff - poly2coeff; ploy1=poly1next; ploy2=poly2next; } } while(poly1 != NULL || poly2 != NULL) { if (poly1 != NULL) { polypow=ploy1pow; polycoeff=poly1coeff; ploy1=poly1next; } if (poly2 != NULL) { polypow=ploy2pow; polycoeff=poly2coeff; ploy2=poly2next; } } }
  • 66. RADIX SORT: • Radix sort is one of the sorting algorithms used to sort a list of integer numbers in order. In radix sort algorithm, a list of integer numbers will be sorted based on the digits of individual numbers. • Radix sort algorithm requires the number of passes which are equal to the number of digits present in the largest number among the list of numbers. • For example, if the largest number is a 3 digit number then that list is sorted with 3 passes.
  • 68. Multi-Linked Lists •A multilinked list is a more general linked list with multiple links from nodes. •In a general multi-linked list each node can have any number of pointers to other nodes, •Multi-lists are essentially the technique of embedding multiple lists into a single data structure. •A multi-list has more than one next pointer, like a doubly linked list, but the pointers create separate lists

Editor's Notes

  • #57: https://codeforwin.org/2015/09/c-program-to-insert-node-at-beginning-of-singly-linked-list.html
  • #59: https://codeforwin.org/2015/09/c-program-to-insert-node-at-beginning-of-singly-linked-list.html
  • #60: https://codeforwin.org/2015/09/c-program-to-insert-node-at-beginning-of-singly-linked-list.html
  • #61: https://codeforwin.org/2015/09/c-program-to-insert-node-at-beginning-of-singly-linked-list.html
  • #62: https://codeforwin.org/2015/09/c-program-to-insert-node-at-beginning-of-singly-linked-list.html
  • #63: https://codeforwin.org/2015/09/c-program-to-insert-node-at-beginning-of-singly-linked-list.html
  • #64: https://codeforwin.org/2015/09/c-program-to-insert-node-at-beginning-of-singly-linked-list.html
  • #65: https://codeforwin.org/2015/09/c-program-to-insert-node-at-beginning-of-singly-linked-list.html
  • #66: https://codeforwin.org/2015/09/c-program-to-insert-node-at-beginning-of-singly-linked-list.html
  • #67: https://codeforwin.org/2015/09/c-program-to-insert-node-at-beginning-of-singly-linked-list.html