QUEUE USING ARRAYS
QUEUE USING ARRAYS
QUEUE USING ARRAYS
Addition of Polynomials:
Example:
Input:
1st polynomial Expression = 5x^2+ 4x^1 +
2x^0
Consider the below Fig 1.23, which adds the two polynomials. The two
polynomials
are represented in linked list. The elements which having the same power
those corresponding coefficients are added.
Sparse Matrix:
A matrix is a two-dimensional data object made of m rows and n columns,
therefore
having total m x n values. If most of the elements of the matrix have 0 value,
then it is called a sparse matrix.
The sparse matrix shown in Fig. 1.25 can be represented using a linked list
for every row and column. Since a value is in exactly one row and one column,
it will appear in both lists exactly once. A node in the multi-linked will have
four parts. First stores the data, second stores a pointer to the next node in the
row, third stores a pointer to the next node in the column, and the fourth stores
the coordinates or the row and column number in which the data appears in the
matrix. However, as in case of doubly linked lists, we can also have a
corresponding inverse pointer for every pointer in the multi-linked list
representation of a sparse matrix.
Figure 1.25 Sparse matrix
Note:
When a non-zero value in the sparse matrix is set to zero, the corresponding
node in the multi- linked list must be deleted.
head ← prevNode
End if End
1) Create two more pointers other than head namely prevNode and curNode that
will hold the reference of previous node and current node respectively. Make
sure that prevNode points to first node i.e. prevNode = head. head should now
point to its next node i.e. the second node head = head->next. curNode
should also points to the second node i.e. curNode = head.
2) Now, disconnect the previous node i.e. the first node from others. We will
make sure that it points to none. As this node is going to be our last node.
Perform operation prevNode-
>next = NULL.
4) Now, re-connect the current node to its previous node i.e. curNode->next =
prevNode;.
5) Point the previous node to current node and current node to head node.
Means they should now point to prevNode = curNode; and curNode = head.
6) Repeat steps 3-5 till head pointer becomes NULL.
7) Now, after all nodes has been re-connected in the reverse order. Make the last
node as the first node. Means the head pointer should point to prevNode
pointer. Perform head = prevNode;. Finally you end up with a reversed linked
list of its original.
QUEUE USING ARRAYS
ENQUEUE
Algorithm
1. void insert (int queue[], int max, int front, int rear, int item)
2. {
3. if (rear + 1 == max)
4. {
5. printf("overflow");
6. }
7. else
8. {
9. if(front == -1 && rear == -1)
10. {
11. front = 0;
12. rear = 0;
13. }
14. else
15. {
16. rear = rear + 1;
17. }
18. queue[rear]=item;
19. }
20. }
Algorithm
1. int delete (int queue[], int max, int front, int rear)
2. {
3. int y;
4. if (front == -1 || front > rear)
5.
6. {
7. printf("underflow");
8. }
9. else
10. {
11. y = queue[front];
12. if(front == rear)
13. {
14. front = rear = -1;
15. else
16. front = front + 1;
17.
18. }
19. return y;
20. }
21. }
22. #include<stdio.h>
23. #include<stdlib.h>
24. #define maxsize 5
25. void insert();
26. void delete();
27. void display();
28. int front = -1, rear = -1;
29. int queue[maxsize];
30. void main ()
31. {
32. int choice;
33. printf("\nEnter your choice ?");
34. scanf("%d",&choice);
35. while(choice != 4)
36. {
37. printf("\n*************************Main Menu*************
****************\n");
38. printf("\
n=======================================================
==========\n");
39. printf("\n1.insert an element\n2.Delete an element\n3.Display the qu
eue\n4.Exit\n");
40. printf("\nEnter your choice ?");
41. scanf("%d",&choice);
42. switch(choice)
43. {
44. case 1:
45. insert();
46. break;
47. case 2:
48. delete();
49. break;
50. case 3:
51. display();
52. break;
53. case 4:
54. exit(0);
55. break;
56. default:
57. printf("\nEnter valid choice??\n");
58. }
59. }
60. }
61. void insert()
62. {
63. int item;
64. printf("\nEnter the element\n");
65. scanf("%d",&item);
66. if(rear == maxsize-1)
67. {
68. printf("\nOVERFLOW\n");
69. return;
70. }
71. if(front == -1 && rear == -1)
72. {
73. front = 0;
74. rear = 0;
75. }
76. else
77. {
78. rear = rear+1;
79. }
80. queue[rear] = item;
81. printf("\nValue inserted ");
82.
83. }
84. void delete()
85. {
86. int item;
87. if (front == -1 || front > rear)
88. {
89. printf("\nUNDERFLOW\n");
90. return;
91.
92. }
93. else
94. {
95. item = queue[front];
96. if(front == rear)
97. {
98. front = -1;
99. rear = -1 ;
100. }
101. else
102. {
103. front = front + 1;
104. }
105. printf("\nvalue deleted is %d",item);
106. }
107.
108.
109. }
110.
111. void display()
112. {
113. int i;
114. if(rear == -1)
115. {
116. printf("\nEmpty queue\n");
117. }
118. else
119. { printf("\nprinting values .....\n");
120. for(i=front;i<=rear;i++)
121. {
122. printf("\n%d\n",queue[i]);
123. }
124. }
125. }
Output:
*************Main Menu**************
==============================================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Value inserted
*************Main Menu**************
==============================================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Value inserted
*************Main Menu**************
===================================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
value deleted
*************Main Menu**************
==============================================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
90
*************Main Menu**************
==============================================
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
2. If
front == NULL, make both front and rear points //to
the new node.
Otherwise, add the new node in rear->next as
newnode (end of the list) i.e
rear->next=newnode.
Dequeue()
TYPES OF QUEUES:
A queue data structure can be classified into the following
types:
1. Circular Queue 2. Deque 3. Priority Queue 4. Multiple Queue
1.CIRCULAR QUEUE:
In a Linear queue, once the queue is completely full, it's not possible
to insert any more elements. When we dequeue any element to
remove it from the queue, we are actually moving the front of the
queue forward, but rear is still pointing to the last element of the
queue, we cannot insert new elements.
Circular Queue is also a linear data structure, which follows the
principle of FIFO (First In First Out), but instead of ending the
queue at the last position, it again starts from the first position
after the last, hence making the queue behave like a circular
data structure.
Types of Deque:
1. Input Restricted Deque: In this deque, input is
restricted at a single end but allows deletion at both the
ends.
2. Output Restricted Deque: In this deque, output is
3. Priority Queue:-
A priority queue is a data structure in which each
element is assigned a priority. The priority of the
element will be used to determine the order in
which the elements will be processed.
The general rules of processing the elements of a
priority queue are
o An element with higher priority is processed before an
element with a lower priority.
o Two elements with the same priority are
processed on a first-come-first-served (FCFS)
basis.
We are given with the data and the priority as an integer value and the
task is to
create a linked list as per the priority given and display the result.
Example
Input
Output
#include <stdio.h>
#include <stdlib.h>
// priority Node
typedef struct node {
int data;
int priority;
struct node* next;
} Node;
Node* newNode(int d, int p) {
Output
2713
Arrays:
Linked Lists:
I think the only time you wouldn't have to find the position is if you
kept some sort of pointer to it (as with the head and the tail in some
cases). So we can't flatly say that linked lists always beat arrays for
insert/delete options