c program day-18
TRANSCRIPT
-
7/31/2019 C Program Day-18
1/14
-
7/31/2019 C Program Day-18
2/14
QUEUE :
It is a non-primitive, linear data structure, and a subclass of list data structure.
It is an ordered, homogeneous collection of elements in which elements areappended at one end called REAR end, and elements are deleted at other end called
FRONT end.
It follows First-in First-Out mechanism, in which elements comes first will be
removed first.
Two operation are frequently carried on queue. They are insertion and deletion.
The insertion operation is possible only when the queue contains elements less
than the capacity it can hold.The deletion is possible when the queue contains at least one element.
-
7/31/2019 C Program Day-18
3/14
Disadvantages of simple Queue :
If more elements are deleted, the value of front will be increased, and as a result beginning
portion of queue will be empty. Therefore, if we try to insert a new element, no element is
inserted, because new element are always inserted from the rear and if the last location of
queue holds an element, even though space is available at the beginning of queue, no element
will be inserted. The queue will be treated as Overflow
Queue using Pointer :Pointer implementation of a Queue can be carried out is similar to array
implementation. This Dynamic Implementation, create NODE at run time, accordingly Insertion,Deletion operation can be done.
(i) creating a Queue
(ii) Checking Queue either full or empty(iii) Initializing a Queue
(iv) Insert an element into the Queue
(v) Delete an element from the Queue.
(vi) Display elements of Queue.
-
7/31/2019 C Program Day-18
4/14
Types of Queues :
The major disadvantage of queue is, when the rearpointer reaches to the end of the queue (array), no more
elements can be added in the queue, even if the beginning
memory locations of array are empty. To Overcome the above
disadvantage, different types of Queue can be applied.
(i) Circular Queue
(ii) DEQueue (Double Ended Queue)
(iii) Priority Queue
-
7/31/2019 C Program Day-18
5/14
# include # include
struct queue{int items[100];int front,rear;};
struct queue q;int givendata,rdata,n;
empty(){return((q.front == q.rear) ? 1 : 0);
}
void rem(){if (empty() == 1)
printf("underflow");Continued,
-
7/31/2019 C Program Day-18
6/14
else{if (q.front == 99)
q.front = 0;else
(q.front)++;rdata = q.items[q.front];printf("\n Data removed is %d",rdata);}
}void show(){int i,x;
if (empty() == 1)printf("\n queue empty");
elsex = ((q.front== 99)? -1:q.front);for ( i = q.rear ; i > x ;i--)
printf("%d-->",q.items[i]);}
void insert(){ Continued,
-
7/31/2019 C Program Day-18
7/14
if (q.rear == 99)q.rear = 0;
else(q.rear)++;
if (q.rear == q.front)printf("overflow");else
q.items[q.rear] = givendata;}
void main(){q.front = q.rear = 99;do{printf("\n Queue Operations ");printf("\n 1.Insert item");printf("\n 2.Remove item");printf("\n 3.Show items in Queue ");printf("\n 4.Quit");
printf("\n Enter your choice : ");Continued,
-
7/31/2019 C Program Day-18
8/14
scanf("%d",&n);
switch(n)
{case 1:printf("\nEnter integer data : ");scanf("%d",&givendata);insert();break;
case 2:rem();break;
case 3:show();break;
}}while (n != 4);}
-
7/31/2019 C Program Day-18
9/14
# include # include
#define null 0
struct list{int info;struct list *next;};
typedef struct list node;node *front,*rear;
void insert()
{int i;node *new;new = (node *)malloc(sizeof(node));printf("\nEnter integer data :");
scanf("%d",&i);Continued,
-
7/31/2019 C Program Day-18
10/14
new->next = null;new->info = i;if (front == null)
{front = new;rear = new;
}else
{rear->next = new;rear = new;
}}void delete(){
node *temp;if (front!= null){
temp = front;front = front->next;
free(temp);}} Continued,
-
7/31/2019 C Program Day-18
11/14
void display(){
node *temp;temp = front;while (temp){
printf("%d-->",temp->info);temp = temp->next;
}}void main(){int ch;front = null;rear = null;
do{printf("\n 1. Insert");printf("\n 2. Delete");printf("\n 3. Display");printf("\n 4. Exit");printf("\n Enter your choice : ");
Continued,
-
7/31/2019 C Program Day-18
12/14
scanf("%d",&ch);switch(ch){case 1:
insert();break;
case 2:delete();break;
case 3:display();break;
}}while(ch != 4);
}
-
7/31/2019 C Program Day-18
13/14
Session Summary
The stack is an ordered collection of elements in which insertions and deletions arerestricted to one end.
PUSH is an operation used to add a new element into a stack
POP is an operation used to remove the top most element from the stack
An queue is an ordered collection of elements in which insertions are made is referred
as the rear end and the end from which deletions are made is referred as front end.
The primitive operations that can be carried out in a queue are enqueue and dequeue
operations
The enqueue is an operation used to add a new element into the rear end of a queue
The dequeue is an operation used to remove an element from the front end of a queue
-
7/31/2019 C Program Day-18
14/14
EXERCISES
1. State the difference between stack & Linked list?
2. State the difference between Queue & Linked List?
3. Explain in detail the Push and Pop operation in a stack?
4. Explain in detail the Enqueue and Dequeue operation in a Queue?
5. Explain in brief the methods to check whether the stack is empty or not?
6. Explain in brief the methods to check whether the queue is full or not?