Beruflich Dokumente
Kultur Dokumente
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. }
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. }
#include <stdio.h>
#include <stdlib.h>
int main()
{
return 0;
}
Output:
Enter number of elements: 5
Memory successfully allocated using malloc.
The elements of the array are: 1, 2, 3, 4, 5,
1 2 3 4
5 6 7 8
9 10 11 12
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;
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12
#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));
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12
filter_none
edit
play_arrow
brightness_4
#include <stdio.h>
#include <stdlib.h>
int main()
{
int r = 3, c = 4, i, j, count;
count = 0;
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
arr[i][j] = ++count; // OR *(*(arr+i)+j) = ++count
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12
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;
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12
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
9. if (start == NULL)
10. {
11. start = t
12. }
13. else
14. {
15. last ->ptr = t
16. }
17. last = t.
18. }
5 25
t t
struct node
{
int data.
struct node *ptr.
}*start == NULL.
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)
22. {
23. t1 -> ptr = t ->ptr.
24. }
25. else
26. {
27. print (Error)
28. }
start 10 20 30 40
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. }
sta 10 20 30 40
5 25
t t
struct node
{
int data.
struct node *ptr.
}*start = NULL.
13. {
14. t1 = start.
15. for (i=2; i<pos && t1 -> ptr !=start; i++)
16. {
17. t1 = t1 -> ptr.
18. t1 -> ptr = t.
19. }}
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. }
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. }
star 10 20 30 40
5
t
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.
int pop()
1. {
2. if(top==-1)
3. print(overflow)
4. else
5. return stack[top--]
6. }
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. }
char pop()
{ if(top==-1)
return ‘#’
else
return stack[top--] }
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()).
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’
[ [
( [(
3 [( 3
+ [(+ 3
5 [(+ 35
* [(+ * 35
2 [( + * 352
) [( 352*+
+ [+ 352*+
8 [+ 352*+8
/ [ +/ 352*+8
2 [+/ 352*+82
] 352*+82/+
2 2
2, 3 2, 3
2, 3, * 2, 15 3*5=15
2, 3, *, + 17 2+15=17
2, 3, *, +,/ 17, 1
2, 3, *, +, / , - 16 17-1=16
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. }
) #)
f #) f
- #)- f
e #)- fe
( # fe-
+ #+ fe-
d #+ fe-d
* #+* 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
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
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
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. }
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. }
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. }
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. }
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
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. }
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. }
71. }
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. }
108. t->lc=LefttRotate(t->lc)
109. t=RightRotate(t)
110. return t
111. }
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. }