Sie sind auf Seite 1von 35

Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

1. Array
Array is a kind of data structure that can store a fixed-size sequential
collection of elements of the same type. An array is used to store a collection
of data, but it is often more useful to think of an array as a collection of
variables of the same type.
1.1 Array Insertion

1. Assume a[n],fai \\fai - Filled Array Index (index of the last element of Array)
2. Read pos
3. if(fai<n-1) // check space is available or not
4. {
5. Read ele
6. for(i=fai;i>pos-1;i--)
7. {
8. a[i+1]=a[i]
9. }
10. a[pos-1]=ele;
11. }

1.2 Array deletion

1.assume a[n], fai, pos, ele, size = n.


2. if( fai > -1) \\ element is present or not
3. {
4. read pos, ele.
5. if(pos-1<= fai) \\position is under limit
6. {
7. for(i=pos-1; i< fai; i++)
8. {
9. a[i]=a[i+1]
10. }
11. fai- -
12. }
13. else print (Invalid Position)
14. }
15. else print (No element )

1.3 Array binary search


1. assume a[10], l=0, u=9, mid, ele.
2. read ele \\ to be find
3. while(l<=u)
4. {
5. mid=(l+u)/2

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

6. if(ele>a[mid])
7. l=mid+1
8. else if(ele<a[mid])
9. u=mid-1
10. else
11. {
12. print (element found)
13. break
14. }
15. }

1.4 How to dynamically allocate a 1D array in C?

#include <stdio.h>
#include <stdlib.h>

int main()
{

// This pointer will hold the


// base address of the block created
int* ptr;
int n, i, sum = 0;

// Get the number of elements for the array


n = 5;
printf("Enter number of elements: %d\n", n);

// Dynamically allocate memory using malloc()


ptr = (int*)malloc(n * sizeof(int));

// Check if the memory has been successfully


// allocated by malloc or not
if (ptr == NULL) {

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

printf("Memory not allocated.\n");


exit(0);
}
else {

// Memory has been successfully allocated


printf("Memory successfully allocated using malloc.\n");

// Get the elements of the array


for (i = 0; i < n; ++i) {
ptr[i] = i + 1;
}

// Print the elements of the array


printf("The elements of the array are: ");
for (i = 0; i < n; ++i) {
printf("%d, ", ptr[i]);
}
}

return 0;
}
Output:
Enter number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,

To know more:- https://www.geeksforgeeks.org/dynamic-memory-


allocation-in-c-using-malloc-calloc-free-and-realloc/

1.5 How to dynamically allocate a 2D array in C?


Following are different ways to create a 2D array on heap (or dynamically allocate a 2D
array).
In the following examples, we have considered ‘r‘ as number of rows, ‘c‘ as number of
columns and we created a 2D array with r = 3, c = 4 and following values

1 2 3 4
5 6 7 8
9 10 11 12

1) Using a single pointer:

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

A simple way is to allocate memory block of size r*c and access elements using simple
pointer arithmetic.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int r = 3, c = 4;
int *arr = (int *)malloc(r * c * sizeof(int));

int i, j, count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
*(arr + i*c + j) = ++count;

for (i = 0; i < r; i++)


for (j = 0; j < c; j++)
printf("%d ", *(arr + i*c + j));

/* Code for further processing and free the


dynamically allocated memory */

return 0;
}

Output:

1 2 3 4 5 6 7 8 9 10 11 12

2) Using an array of pointers


We can create an array of pointers of size r. After creating an array of pointers, we
can dynamically allocate memory for every row.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int r = 3, c = 4, i, j, count;

int *arr[r];
for (i=0; i<r; i++)
arr[i] = (int *)malloc(c * sizeof(int));

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

// Note that arr[i][j] is same as *(*(arr+i)+j)


count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count; // Or *(*(arr+i)+j) = ++count

for (i = 0; i < r; i++)


for (j = 0; j < c; j++)
printf("%d ", arr[i][j]);

/* Code for further processing and free the


dynamically allocated memory */

return 0;
}
Output:

1 2 3 4 5 6 7 8 9 10 11 12

3) Using pointer to a pointer


We can create an array of pointers also dynamically using a double pointer. Once we
have an array pointers allocated dynamically, we can dynamically allocate memory
and for every row like method 2.

filter_none
edit
play_arrow

brightness_4
#include <stdio.h>
#include <stdlib.h>

int main()
{
int r = 3, c = 4, i, j, count;

int **arr = (int **)malloc(r * sizeof(int *));


for (i=0; i<r; i++)
arr[i] = (int *)malloc(c * sizeof(int));

// Note that arr[i][j] is same as *(*(arr+i)+j)

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count; // OR *(*(arr+i)+j) = ++count

for (i = 0; i < r; i++)


for (j = 0; j < c; j++)
printf("%d ", arr[i][j]);

/* Code for further processing and free the


dynamically allocated memory */

return 0;
}
Output:

1 2 3 4 5 6 7 8 9 10 11 12

4) Using double pointer and one malloc call

filter_none
edit
play_arrow

brightness_4
#include<stdio.h>
#include<stdlib.h>

int main()
{
int r=3, c=4, len=0;
int *ptr, **arr;
int count = 0,i,j;

len = sizeof(int *) * r + sizeof(int) * c * r;


arr = (int **)malloc(len);

// ptr is now pointing to the first element in of 2D array


ptr = (int *)(arr + r);

// for loop to point rows pointer to appropriate location in 2D array


for(i = 0; i < r; i++)

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

arr[i] = (ptr + c * i);

for (i = 0; i < r; i++)


for (j = 0; j < c; j++)
arr[i][j] = ++count; // OR *(*(arr+i)+j) = ++count

for (i = 0; i < r; i++)


for (j = 0; j < c; j++)
printf("%d ", arr[i][j]);

return 0;
}
Output:

1 2 3 4 5 6 7 8 9 10 11 12

To know more:- https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/

2. Linked list
In computer science, a linked list is a linear collection of data elements, called
nodes, each pointing to the next node by means of a pointer.

start 10 20 30 40

2.1.1 Linked list creation.


struct node
{
int data;
struct node *ptr;
}*start = NULL;

1. Declare struct node *t, *last.


2. int i, n.
3. read n.
4. for(i = 1; i<= n; i++)
5. {
6. t = (struct node*) malloc (sizeof (struct node) ).
7. read t -> data
8. t->ptr = NULL

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

9. if (start == NULL)
10. {
11. start = t
12. }
13. else
14. {
15. last ->ptr = t
16. }
17. last = t.
18. }

2.1.2 Linked list insertion.


sta 10 20 30 40

5 25
t t

struct node
{
int data.
struct node *ptr.
}*start == NULL.

1. assume *start, *last


2. t = (struct node*) malloc( sizeof (struct node) ) .
3. read t -> data.
4. read pos.
5. if (pos == 1)
6. {
7. t -> ptr = start.
8. start = t.
9. }
10. else
11. {
12. t1 = start
13. for (i=2; i<pos && t1->ptr! = NULL; i++)
14. {

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

15. t1 = t1 -> ptr.


16. }
17. t -> ptr = t1 -> ptr.
18. t1 -> prt = t.
19. }

2.1.3 Linked list deletion.

sta 10 20 30 40
t t

struct node
{
int data.
struct node *ptr.
}*start = NULL.

1. read pos.
2. if (pos == 1)
3. {
4. t = start.
5. start = start->ptr.
6. free (t).
7. }
8. else
9. {
10. t = t1 = start.
11. for ( i=1; i< pos; i++)
12. {
13. t1 = t.
14. t = t->ptr.
15. if (t == NULL)
16. {
17. f=1.
18. break.
19. }
20. }
21. if (f != 1)

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

22. {
23. t1 -> ptr = t ->ptr.
24. }
25. else
26. {
27. print (Error)
28. }

2.2. Circular Linked List.


In a circularly linked list, all nodes are linked in a continuous circle, without using
null.The next node after the last node is the first node.

start 10 20 30 40

2.2.1 Circular LL creation.


struct node
{
int data.
struct node *ptr.
}*start = NULL.

1. declare struct node *t, *last.


2. read n.
3. for ( i = 1 to n )
4. {
5. t = (struct node*) malloc( sizeof (struct node) ) .
6. read t -> data.
7. if (start == NULL)
8. {
9. start = t.
10. }
11. else
12. {
13. last -> ptr = t.
14. }
15. t -> ptr = start.
16. last = start.
17. }

2.2.2 Circular LL Traversing

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

struct node
{
int data.
struct node *ptr.
}*start = NULL.
1. assume *start.
2. declare struct node *t.
3. for (t=start; start != NULL; t = t -> ptr)
4. {
5. print ( t -> data)
6. if (t -> ptr = start)
7. break
8. }

2.2.3 Circular LL insertion.

sta 10 20 30 40

5 25
t t

struct node
{
int data.
struct node *ptr.
}*start = NULL.

1. assume *start, *last.


2. declare struct node *t, *t1.
3. read pos.
4. t = (struct node*) malloc( sizeof (struct node) ) .
5. read t-> data.
6. if (pos == 1)
7. {
8. t -> ptr = start.
9. start =t.
10. last -> ptr = t.
11. }
12. else

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

13. {
14. t1 = start.
15. for (i=2; i<pos && t1 -> ptr !=start; i++)
16. {
17. t1 = t1 -> ptr.
18. t1 -> ptr = t.
19. }}

2.2.4 Circular LL deletion

sta 10 20 30 40
t t

struct node
{
int data.
struct node *ptr.
}*start = NULL.
1. assume *start, *last.
2. declare struct node *t, *t1.
3. if (start != NULL)
4. {
5. read pos.
6. if (pos == 1)
7. {
8. t = start.
9. start = start -> ptr.
10. free (t).
11. last -> ptr = start.
12. }
13. else
14. {
15. t = start.
16. for (i=1; i < pos; i++)
17. {
18. t1 = t.
19. t = t -> ptr.
20. }
21. t1 -> ptr = t -> ptr.
22. free (t).
23. }

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

2.3 Doubly Linked List


A doubly linked list is a linked data structure that consists of a set of sequentially linked records
called nodes. Each node contains two fields, called links, that are references to the previous and
to the next node in the sequence of nodes. The beginning and ending nodes' previous and next
links, respectively, point to NULL.

2.3.1 Doubly linked list creation.


Pre Data

star 10 20 30 40

struct node
{
int data;
struct node *pre, *next;
}*start = NULL.
1. declare struct node *t, *last.
2. read n.
3. for (i = 1 to n)
4. {
5. t = (struct node*) malloc( sizeof (struct node) )
6. read t -> data.
7. t -> next = NULL.
8. if ( start == NULL)
9. {
10. start = t.
11. t -> pre = NULL.
12. }
13. else
14. {
15. last -> next = t.
16. t -> pre = last.
17. }
18. last = t. }

2.3.2 Doubly LL insertion.

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

Pre Data Next

star 10 20 30 40

5
t

1. assume *start, *last.


2. Declare struct node *t, *t1.
3. read pos.
4. t = (struct node*) malloc( sizeof (struct node) )
5. read t -> data.
6. If ( pos == 1)
7. {
8. t -> next = start.
9. start -> pre = t.
10. start = t.
11. t -> pre = NULL.
12. }
13. else
14. {
15. t1 = start.
16. for (i=2; i<pos; i++)
17. {
18. t1=t1->next;
19. }
20. t -> next = t1 -> next.
21. t -> pre = t1.
22. if( t1 ->next!= NULL)
23. {
24. (t1->next)->pre=t
25. }
26. t1->next=t

2.3.3 Doubly LL deletion.

1. assume a DLL *start, *last.


2. Declare struct node *t, *t1.
3. if( start!=NULL)
4. {
5. Read pos
6. If(pos==1)

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

7. {
8. t=start.
9. start=start->next
10. free(t).
11. }
12. else
13. {
14. t=t1=start.
15. for( i=1; i<pos; i++)
16. {
17. t1=t
18. t=t->next.
19. }
20. t1->next=t->next.
21. if( t->next != NULL)
22. {
23. (t->next)->pre=t1.
24. }
25. free(t) }

4. Stack

A Stack is an abstract data structure which is used to store data in a particular order.
It is either implemented with the help of array or a linked list. Two operations that can
be performed on a Stack are: Push operation which inserts an element into the
stack. Pop operation which removes the last element that was added into the stack.
It follows Last In First Out(LIFO) Order.
4.1.1 Stack implementation by Array.

Declare int stack[10],size=10,top=-1


push (int ele)
1. {
2. if (top==size-1)
3. print (overflow)
4. else
5. stack[++top]=ele.
6. }

int pop()

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

1. {
2. if(top==-1)
3. print(overflow)
4. else
5. return stack[top--]
6. }

4.1.2 Stack implementation by Linked list


struct node
{
int data
struct node *ptr.
}*top=NULL;

push( int ele)


1. {
2. t = (struct node*) malloc( sizeof (struct node) )
3. if (t!=NULL)
4. {
5. t->data=ele
6. t->ptr=top
7. top= t
8. }
9. else
10. print (overflow)
11. }

int pop()
1. {
2. if(top!=NULL)
3. {
4. t=top.
5. top=top->ptr
6. ele =t->data
7. free(t)
8. return ele
9. }
10. else
11. print underflow
12. }

4.2 Stack applications

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

4.2.1 Parenthesis checking

Declare stack[20],top=-1 // for stack implementation

void push(char ele)


{ stack[++top]=ele; }

char pop()
{ if(top==-1)
return ‘#’
else
return stack[top--] }

1. Declare FILE *fp


2. fp=fopen(“demo.c”,”r”)
3. while((ch=fgetc(fp)!=EOF)
4. {
5. if(ch==’{‘)
6. push(‘{‘)
7. Else if (ch==’}’)
8. {
9. ch1=pop();
10. if(ch1==’#’)
11. Break;
12. }
13. }
14. if(ch==’#’ || top!=-1)
15. print(“incorrect sequence”)
16. Else
17. print(“Correct sequence”)

4.2.2 Solving postfix expression


1. Assume a postfix expression in p[]
2. while(p[i]!=’\0’)
3. {
4. if(p[i] is equal to (‘+’,’-‘,’/’,’*’))
5. {
6. op2=pop();
7. op1=pop();

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

8. switch(p[i])
9. {
10. case ‘+’ : push(op1+op2);
11. break.
12. case ‘-’ : push(op1-op2);
13. break.
14. case ‘/’ : push(op1/op2);
15. break.
16. case ‘*’ : push(op1*op2);
17. break.
18. }}
19. else
20. push(p[i]-‘0’).
21. i++;
22. }
23. print (result = pop()).

4.2.3 Infix to Postfix


1. Declare char p[ ]; char q[ ], ch; int ind=0
2. read p
3. push(‘#’)
4. for(i=0;p[i]!=0;i++)
5. {
6. if(p[i]>=’0’ && p[i]<=’9’)
7. q[ind++]=p[i]
8. else if(p[i]==’(‘)
9. push(‘(‘)
10. else if(p[i]==’)’) {
11. ch=pop()
12. while(ch!=’(‘)
13. {
14. q[ind++]=ch
15. ch=pop()
16. }
17. else
18. {
19. op=pop()
20. while(prio(op)>=prio(p[i]))
21. {
22. q[ind++]=op
23. op=pop()
24. }
25. push(op)

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

26. push(p[i])
27. }
28. }//end of for
29. ch=pop()
30. while(ch!=’#’)
31. {
32. q[ind++]=ch.
33. ch=pop()
34. }
35. q[ind]=’\0’

int prio(char ele)


1. {
2. if(ele==’+’|| ele==’-‘)
3. return 2.
4. else if(ele==’/’||ele==’*’)
5. return 3.
6. Else
7. return 1. }

4.2.4 Infix to postfix conversion by stack (Stepwise solution)


[(3+5*2)+8/2] Stack (pop only priority Q(array/output)
is equal and high)

[ [

( [(

3 [( 3

+ [(+ 3

5 [(+ 35

* [(+ * 35

2 [( + * 352

) [( 352*+

+ [+ 352*+

8 [+ 352*+8

/ [ +/ 352*+8

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

2 [+/ 352*+82

] 352*+82/+

Evaluation of postfix expression :


2, 3, 5, *, +, /, - stack Partial Result

2 2

2, 3 2, 3

2, 3, 5 2, 3, 5 Op1 (opr) Op2

2, 3, * 2, 15 3*5=15

2, 3, *, + 17 2+15=17

2, 3, *, +,/ 17, 1

2, 3, *, +, / , - 16 17-1=16

4.2.4 Infix to prefix


1. declare char p[ ], char q[ ], int x
2. read p
3. x= strlen(p)
4. push(‘#’)
5. for(i=x; i>0; i--)
6. {
7. if(p[i]>=’0’ && p[i]<=’9’)
8. q[ind++]=p[i]
9. else if(p[i]==’)‘)
10. push(‘)‘)
11. else if(p[i]==’(’)
12. ch=pop()
13. while(ch!=’)‘)
14. {
15. q[ind++]=ch
16. ch=pop()
17. }
18. else
19. {
20. op=pop()

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

21. while(prio(op)>prio(p[i]))
22. {
23. q[ind++]=op
24. op=pop()
25. }
26. push(op)
27. push(p[i])
28. }
29. }//end of for
30. ch=pop()
31. while(ch!=’#’)
32. {
33. q[ind++]=ch.
34. ch=pop()
35. }
36.
37. int prio(char ele)
38. {
39. if(ele==’+’|| ele==’-‘)
40. return 2.
41. else if(ele==’/’||ele==’*’)
42. return 3.
43. else
44. return 1.
45. }

Infix to Prefix conversion by Stack (Stepwise solution)


a+b-c*d+(e-f) Stack (Pop only priority is Q(array/output)
(right to left) high)

) #)

f #) f

- #)- f

e #)- fe

( # fe-

+ #+ fe-

d #+ fe-d

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

* #+* fe-d

c #+* fe-dc

- #+- fe-dc*

b #+- fe–dc*b

+ #+-+ fe–dc*b

A #+-+ fe–dc*ba

fe–dc*ba+-+
Reverse the expression +, -, +, a, b, *, c, d, –, e, f

4.2.5 Evaluation of prefix.


-, +,2,3,*,/,8,2,3 (R to L) Stack Result

3 3

2 3, 2

8 3, 2, 8

/ 3, 4 8/2=4

* 12 4*3=12

3 12, 3

2 12, 3, 2

+ 12, 5 3+2=5

- -7 5-12=-7

4.2.6 Tower of Hanoi.

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

n-

S(Source) M(Mediator) D

S(Source) M(Mediator) D

S(Source) M(Mediator) D

S(Source) M(Mediator) D
1. TOH(n, A, B, C) // ( S, M, D)
2. {
3. if(n==1)
4. {
5. Print(“move %c disk from %c to %c”, n, A, C)
6. Return
7. }
8. TOH( n-1, A, C, B) // (S, D, M)
9. Print( “move %c disk from %c to %c”, n, A, C)
10. TOH(n-1, B, A, C) //( M, S, D)
11. }

5.Queue

5.1 Implementation of Queue by Array.


1. define queue[10], front=-1, rear=-1, size=10;
2. insertionQA(int ele)
3. {
4. if(rear==size-1)
5. {
6. print(Overflow)
7. }
8. else if(rear==-1)
9. {

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

10. rear=front=0
11. queue[rear]=ele
12. }
13. }//end of insertionQA
14. deletionQA()
15. {
16. if(front=-1)
17. {
18. print(overflow)
19. }
20. else if(front==rear)
21. front=rear=-1
22. else
23. front=front+1
24. }
25. printQA()
26. {
27. if(front==-1)
28. {
29. print(underflow)
30. }
31. else
32. {
33. for(i=front;i<=rear;i++)
34. Print queue[i]
35. }
36. }

5.2 Circular Queue.


1. declare cq[], front=-,size=10;
2. insertionCQA(int ele)
3. {
4. if (front==0&&rear==size-1||front==rear+1) //or if (front=(rear+1)5size)
5. print(Overflow)
6. else if(rear==-1)
7. {
8. front=rear=0
9. cq[rear]=ele
10. }
11. else
12. {

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

13. rear=(rear+1)%size
14. cq[rear]=ele
15. }
16. DeletionQA()
17. {
18. if(front==-1)
19. print(overflow)
20. else if (front==rear)
21. front=rear=-1
22. else
23. front=(front+1)%size
24. }
25. print()
26. {
27. for(i=front;front!=-1;i=(i+1)%size)
28. {
29. print(cq[i])
30. if(i==rear)
31. break;
32. }
33. }

1. OR //alternate for print()


2. print()
3. {
4. if(front<=rear)
5. {
6. for(i=front;i<=rear;i++)
7. print(cq[i])
8. }
9. else
10. {
11. for(i=front;i<=size-1;i++)
12. print(cq[i])
13. for(i=0;i<=rear;i++)
14. print(cq[i])
15. }
16. }

5.3 Queue- Linked List


1. struct node

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

2. {
3. int data
4. struct node *ptr
5. }*front=NULL,*rear=NULL
6. insertionQLL(int ele)
7. {
8. struct node *t
9. t=malloc()
10. if(t!=NULL)
11. {
12. T->data=ele
13. t->ptr=NULL
14. if(rear==NULL)
15. front=rear=t
16. else
17. {
18. rear->ptr=t
19. rear=t
20. }
21. }
22. deletionQLL()
23. {
24. if(front==NULL)
25. print(overflow)
26. else if(front==rear)
27. {
28. free (front)
29. front=rear=NULL
30. }
31. else
32. {
33. t=front
34. front=front->ptr
35. free(t)
36. }
37. }
38. printQLL
39. {
40. for(t=front;t!=NULL;t=t->ptr)
41. print(t->data)
42. }

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

5.4 Priority Queue by linked list


1. struct node
2. {
3. int data, pri
4. struct node *ptr
5. }*front=NULL;
6. insertionPQLL(int ele, int priority)
7. {
8. t= malloc;
9. if(t!=NULL)
10. t->data=ele
11. t->pri=priority
12. if(front==NULL)
13. {
14. front=t;
15. t->ptr=NULL;
16. }
17. else
18. {
19. if(t->pri < front->pri)
20. {
21. t->ptr=front
22. front=t
23. }
24. else
25. {
26. for(t2=front; t->pri >= t2->pri && t2!=NULL; )
27. {
28. t1=t2
29. t2=t2->ptr
30. }
31. t->ptr=t1->ptr
32. t1->ptr=t;
33. }//end of inner else
34. }// end of outer else
35. }// end of if
36. }

Priority using array:


1. Declare mq[3][5], fr[3][2]={-1,-1,-1,-1,-1,-1};
2. InsertionPQA(in ele, int prioirty)
3. {
4. If(fr[priority][0]==(fr[prioirty][1]+1)%size)

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

5. Print (Overflow)
6. Else if(fr[priority][0]==-1)
7. {
8. fr[priority][0]=fr[prioirty][1]=0
9. mq[priroity][0]=ele;
10. }
11. Else
12. {
13. fr[priority][1]=(fr[priority][1]+1)%size
14. mq[priority][fr[priority][1]]=ele;
15. }
16. deletionPQA(int priority)
17. {
18. if(mq[priority][1]==-1)
19. print(Underflow)
20. else if(mq[priority][0]==mq[priority][1])
21. mq[priority][0]=mq[priority][1]=0
22. else
23. mq[priority][0]=(mq[priority][0]+1)%size
24. }
25.

6.Trees
6.1 Pre Order:
1. Ptr is pointing binary tree(Root)
2. While(ptr!=NULL)
3. {
4. Print(ptr->data)
5. If(ptr->rc!=NULL)
6. push(ptr->rc)
7. Else if(ptr->lc!=NULL)
8. ptr=ptr->lc;
9. Else
10. ptr=pop()
11. }

6.2 Inorder:
1. again: While(ptr != NULL)
2. {
3. push(ptr)
4. ptr=ptr->lc
5. }

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

6. ptr=pop()
7. while(ptr!=NULL)
8. {
9. Print(ptr->data)
10. If(ptr->rc!=NULL)
11. {
12. ptr=ptr->rc
13. goto again
14. }
15. Else
16. ptr=pop()
17. }

6.3 Postorder
1. While(ptr!=NULL)
2. {
3. If(ptr->rc!=NULL)
4. Push(ptr=ptr->lc)
5. Else if(ptr->lc!=NULL)
6. Push(ptr->lc)
7. Else
8. Ptr=pop()
9. Print(ptr->data)
10. }
11.
6.4 BST Insertion And Creation.
1. struct node
2. {
3. int data
4. struct node *lc,*rc;
5. };
6. struct node *insertion(struct node* root1, int ele)
7. {
8. If(root1=NULL)
9. {
10. struct node *t=malloc()
11. t->data=ele
12. t->lc=t->rc=NULL
13. else if(ele>root1->data)
14. root1->rc=insertion(root1->r,ele)
15. else if(ele>root1->data)
16. root1->lc=insertion(root1->lc,ele)
17. return root1

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

18. }
19. main()
20. {
21. struct node *root =NULL
22. read ele
23. root=insertion(root,ele)
24. }
25.
6.5 Searching in BST
1. search(struct node *root1, int ele)
2. {
3. If(root1==NULL)
4. Print(Not Found)
5. Else if(root1->data==ele)
6. Print(Found)
7. Else if(ele>root1->data)
8. search(root1->rc,ele)
9. Else if(ele<root1->data)
10. search(root1->lc,ele)
11. }
12.
6.6 Deletion in BST
1. struct node* deletion(struct node* root1, int ele)
2. {
3. If(root1==NULL)
4. return root1;
5. Else if(ele >root1->data)
6. root1->rc=deletion(root1->rc,ele)
7. Else if(ele<root1->data)
8. root1->lc=deletion(root1->lc,ele)
9. Else
10. {
11. If(root1->lc==NULL)
12. {
13. struct node *temp=root1->rc
14. free root1
15. return temp
16. }
17. If(root1->lc==NULL)
18. struct node *temp=root1->lc
19. free root1
20. return temp
21. }

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

22. struct node *temp=minvalue(root1->rc)


23. root1->data=temp->data
24. root1->rc=deletion(root1->rc,temp->data)
25. } //end of else
26. return root1

27. struct node *minvalue(struct node *t)


28. {
29. While(t->lc!=NULL)
30. t=t->lc
31. return root1
32. }

5.7 AVL Tree Insertion


1. struct node
2. {
3. int data
4. struct node *lc, *rc
5. int ht
6. }

7. int height(struct node *t)


8. {
9. int lh, rh
10. If(t==NULL)
11. return 0
12. If(t->lc==NULL)
13. lh=1
14. Else
15. lh=1+t->lc->ht
16. If(t->rc==NULL)
17. rh=1
18. Else
19. rh=1+t->rc->ht
20. If(lh>rh)
21. return lh
22. Else
23. return rh
24. }

25. int balfact(struct node *t)


26. {
27. int lh, rh

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

28. if(t==NULL)
29. return 0
30. if(t->lc==NULL)
31. lh=0
32. else
33. lh=t->lc->ht
34. if(t->rc==NULL)
35. rh=0
36. else
37. rh=t->rc->ht
38. return (lh-rh)
39. }

40. struct node *insertion(struct node *t, int ele)


41. {
42. If(t==NULL)
43. {
44. t= malloc()
45. t->data=ele
46. t->lc=t->rc=NULL
47. }
48. Else if(ele>t->data)
49. {
50. t->rc=insertion(t->rc,ele)
51. if(balfact(t)==-2)
52. {
53. If(ele>t->rc->data)
54. t=RRrotation(t)
55. else
56. t=RLrotation(t)
57. }
58. }
59. Else if(ele<t->data)
60. {
61. t->lc=insertion(t->lc,ele)
62. if(balfact(t)==2)
63. {
64. If(ele<t->lc->data)
65. t=LLrotation(t)
66. else
67. t=LRrotation(t)
68. }
69. t->ht=height(t)
70. return t

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

71. }

72. RRrotation(struct node *t)


73. {
74. t=LeftRotate(t)
75. return t
76. }

77. LLrotation(struct node *t)


78. {
79. t=RightRotate(t)
80. return t
81. }

82. LeftRotate(*a)
83. {
84. struct node *b=a->rc
85. a->rc=b->lc
86. b->lc=a
87. a->ht=height(a)
88. b->ht=height(b)
89. return b
90. }

91. RightRotate(*a)
92. {
93. struct node *b=a->lc
94. a->lc=b->rc
95. b->rc=a
96. a->ht=height(a)
97. b->ht=height(b)
98. return b
99. }

100. RLrotation(struct node *t)


101. {
102. t->rc=RightRotate(t->rc)
103. t=LeftRotate(t)
104. return t
105. }

106. LRrotation(struct node *t)


107. {

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

108. t->lc=LefttRotate(t->lc)
109. t=RightRotate(t)
110. return t
111. }

5.8 AVL Deletion:


1. deletion(*t,ele)
2. {
3. if(t==NULL)
4. {
5. print “Not Found!”
6. return NULL;
7. }
8. if(ele>t->data)
9. {
10. t->rc=deletion(t->rc=ele);
11. if(balfact(t)==2)
12. {
13. if(balfact(t->lc)>=0)
14. t=LLrotation(t);
15. else
16. t=LRrotation(t);
17. }
18. }
19. else if(ele<t->data)
20. {
21. t->lc=deletion(t->lc,ele)
22. if(balfact(t)==-2)
23. s{
24. if(balfact(t->lc)<=0)
25. RRrotation(t);
26. else
27. RLrotation(t);
28. }
29. }
30. if(t->rc!=NULL)
31. {
32. *temp=t->rc;
33. while(temp->lc=NULL)
34. temp=temp->lc;
35. t->data=temp->data;
36. t->rc=deletion(t->rc,temp->data);
37. if(balfact(t)==2)

Available by :- Crystal Dias-2019 batch | 3A-CSE


Data Structures Class Notes by Mahesh Jangid | Faculty of CSE

38. {
39. if(balfact(t->lc)>=0)
40. t=LLrotation(t);
41. else
42. t=LRrotation(t);
43. }
44. else
45. {
46. return(t->lc)
47. }
48. t->ht=height(t->lc)
49. return t;
50. }

Available by :- Crystal Dias-2019 batch | 3A-CSE

Das könnte Ihnen auch gefallen