Linked List Data structure using C programming and all the detailed information has been provided

MeenakaRajenderan 25 views 50 slides Jul 30, 2024
Slide 1
Slide 1 of 50
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50

About This Presentation

Computer science and data structure


Slide Content

Introduction To Data Structures And List ADT 7/12/2023 1

2 A g enda Introduction to Data Structures List ADT Issues in Array Implementation Linked List Representation Types Applications of Linked List 7/12/2023

Introduction to Data Structures Structure Way of organizing and storing information so that it is easy to use Way of organizing and storing data on a computer so that it can be used easily and effectively Data Values or set of values of different data types such as int, float, etc., Data + Structure 7/12/2023 3

Introduction to Data Structures Definition: Data Structure is a way of organizing, storing and retrieving data and their relationships with each other 7/12/2023 4

Classification of Data Structures Data Structure Primitive i n t c har flo a t bo o le a n Non - Primitive Li n ear Ar r a y Linked List S t ack Queue Non - Linear T r ee G r a ph 7/12/2023 5

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 7/12/2023 6

7/12/2023 7 Examples – List ADT, Stack ADT, Queue ADT, etc., Some operations on these ADT are: List: insert(x) de l e t e(x) find(x) Stack: Push (x) Pop() isFull() isEmpty() Queue: enqueue() dequeue() isFull() isEmpty() ADT – Abstract Data Type

List ADT can be represented in two ways Array Linked List List ADT 7/12/2023 8

7/12/2023 9 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

7/12/2023 10 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 Issues in Array

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 Linked List ADT 7/12/2023 11

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; }; 7/12/2023 12

Statement to create a node: n= (struct node*) malloc(sizeof(sturct node)) n  next =NULL; struct node { int data; struct node *next; } *n; Creating a Node 7/12/2023 13

7/12/2023 14 Singly linked list Doubly linked list Circular linked list Types of Linked List

Type of linked list Each node contains only one pointer field that contains the address of the next node in the list Singly Linked List (SLL) 7/12/2023 15

Major operations are: Insertion At the beginning At the middle At the end Deletion At the beginning At the middle At the end Search Operations on Singly Linked List Global declaration of a Node: struct node { int data; struct node *next; } *head, *tail, *n, *t; 7/12/2023 16

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; } } 10 7/12/2023 17

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; } } 10 7/12/2023 18

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  d at a == m i d_d a t a ) break; } n  n e xt= t  n e xt ; t  next=n; } 10 83==9 ? NO 9= = 9 ? Y es 10 300 7/12/2023 19 300 1500 300

Routine for Deletion at the Beginning void del_beg () { t=head; head = t  n e xt ; free(t); } 7/12/2023 20

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); } 7/12/2023 21

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  n e xt= t  n e x t; free(t); } 2800 1500 Dept of CSE, Velammal Engineering College, 22 7 C h ! e = n 7 n a i ? 83!=70? Yes 9!=70? Yes No 7/12/2023

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; } t!=NULL? Yes 83!=9? Yes 7/12/2023 23 t!=NULL? Yes 9!=9? No Element Found!!

7/12/2023 24 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 7/12/2023 25

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 Representation of Node in DLL Declaration of a node: struct node { int data; struct node *next, *prev; }; A Node in DLL 7/12/2023 26

Major operations are: Insertion At the beginning At the middle At the end Deletion At the beginning At the middle At the end Search Operations on Doubly Linked List Global declaration of a Node: struct node { int data; struct node *next, prev; } *head, *tail, *n, *t; 7/12/2023 27

Routine for Insertion at the Beginning void ins_beg_dll (int num) //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  n e xt=head; hea d  p r e v=n; head=n; 70 1000 } 500 7/12/2023 29 } 28

void ins_end_dll (int num) //Let num=10 { n=(struct node *) malloc (size of(struct node)); n  n e xt=NUL L ; n  p r e v=NUL L ; n  data=num; if (head==NULL) //case 1 { head= n ; tail=n; } else //case 2 { tail  next=n; n  p r e v= t ai l ; tail=n; } Routine for Insertion at the End 7/12/2023 30 } 29

void ins_end_dll (int num, int mid_data) //Let num=10, mid_data=9 { n=(struct node *) malloc (size of(struct node)); n  n e xt=NUL L ; n  p r e v=NUL L ; n  data=num; for(t=head; t!=NULL; t=t  next) { if ( t  d at a == m i d_d a t a ) break; } n  n e xt= t  n e xt ; n  prev=t; t  n e xt  p r e v=n; t  next=n; } Routine for Insertion at the Middle 7/12/2023 30

void del_beg_dll () { t=head; head=head  next; hea d  p r e v=NUL L ; free(t); } Routine for Deletion at the Beginning t is freed 7/12/2023 31

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); } 7/12/2023 32

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); } 7/12/2023 33

Routine for Search an Element struct node* search_dll (int num) //Let num=9 { t=head; while(t!=NULL && t  data!=num) { t=t  next; } return t; } Ele m e n t F o u n d !! t is returned 7/12/2023 34

Due to the use of two pointers Dept of CSE, Velammal Engineering College, 35 C he n nai 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 DLL – Advantages & Disadvantages 7/12/2023

Type of linked list The last node will be pointing to the first node It can be either singly or doubly linked Circular Linked List (CLL) Global declaration of a node: struct node { int data; struct node *next, *prev; }*head,*n,*t; 100 7/12/2023 36

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  n e xt ; t  next=n; n  next=head; head=n; } 10 10 100 7/12/2023 38 } 37

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  d at a =num; t=head; while(t  next!=head) t = t  n e xt ; t  next=n; n  next=head; } 10 1000 7/12/2023 38

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  d at a == m i d_d a t a ) break; } n  next=t  next; t  next=n; } 10 1000 7/12/2023 39

Routine for Deletion at the Beginning void del_beg_cll () { struct node *t1; t=head; t1=head; whi l e(t  n e xt ! = hea d ) t=t  next; t  n e xt=hea d  n e xt ; head=t  next; free(t1); } 100 t1 is freed!! 7/12/2023 40

Routine for Deletion at the End void del_end_cll () { struct node *tp; t=head; while(t  next!=head) { tp=t; t=t  n e xt ; } tp  n e xt=hea d; free(t); } 100 t is freed !! 7/12/2023 41

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; f r ee(t); } t is freed!! 7/12/2023 42

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; } 7/12/2023 43

7/12/2023 44 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 CLL – Advantages & Disadvantages

7/12/2023 45 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 Examples of polynomial equations: – 9x 5 + 7x 3 – 4x 2 + 8 7x 4 – 17x 3 – 8x 2 To store the data of polynomial equations in the linked li s t, each nod e c o n t ains t w o d a t a fiel d s , nam e l y coefficient and power and one next pointer field struct node { int coeff; int pow; struct node *next; }*n; 7/12/2023 46

Creating a Polynomial List struct node* createpoly (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) { he ad = n; tail=n; } else { t ai l  n e xt=n; tail=n; } return head; } 7/12/2023 47

Polynomial Addition Void addpoly (struct node *poly1, struct node *poly2, struct node *poly) { while(ploy1!=NULL && poly2!=NULL) { if(poly1  pow>poly2  pow) { poly  pow=ploy1  pow; poly  coeff=poly1  coeff; ploy1=poly1  next; } else if(poly1  pow < poly2  pow) { poly  pow=ploy2  pow; poly  coeff=poly2  coeff; ploy2=poly2  next; } 7/12/2023 48

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 Addition Contd., } 2 } 5/5/2020 50 Dept of CSE, Velammal Engineering College, Chennai 7/12/2023 49

7/12/2023 50 Thank You!!!
Tags