Create a Queue and do the basic operations using Linked List MC9217 DATA STRUCTURES LAB Anna University lab manual download - Computer Programming

Latest

C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Saturday, January 29, 2011

Create a Queue and do the basic operations using Linked List MC9217 DATA STRUCTURES LAB Anna University lab manual download

Create a Queue and do the basic operations using Linked List MC9217 DATA STRUCTURES LAB Anna University lab manual download


Aim: To write a program to create a Queue using Linked list

Algorithm:

  1. Start
  2. Read the value of ch
  3. Get the variable temp and set malloc for size
  4. Get item and store in data and link as Null
  5. Get link of rear to temp. If front as Null and print Queue as Empty
  6. Else print front element
  7. print the data until the temp get Null
  8. Stop

Logical Description       
Queues are data structures that, like the stack, have restrictions on where you can add and remove elements. To understand a queue, think of a cafeteria line: the person at the front is served first, and people are added to the line at the back. Thus, the first person in line is served first, and the last person is served last. This can be abbreviated to First In, First Out (FIFO).
One bit of terminology: the addition of an element to a queue is known as an enqueue, and removing an element from the queue is known as a dequeue.
Although the concept may be simple, programming a queue is not as simple as programming a stack. Let's go back to the example of the cafeteria line. Let's say one person leaves the line. Then what? Everyone in line must step forward, right? Now, imagine if only one person could move at a time. So, the second person steps forward to fill the space left by the first person, and then the third person steps forwards to fill the space left by the second person, and so on. Now imagine that no one can leave or be added to the line until everyone has stepped forward. You can see the line will move very slowly. It is not difficult to program a queue that works, but it is quite a proposition to make a queue that is fast!!
Program Code

#include<stdio.h>
#include<conio.h>
struct node
{
            int data;
            struct node* link;
}*head,*rear,*front,*curr;
void insert(int data)
{
            curr=(struct node*)malloc(sizeof(struct node));
            curr->data=data;
            curr->link=NULL;
            if(rear==NULL)
            {
                        rear=curr;
                        front=curr;
            }
            else
            {
                        rear->link=curr;
                        rear=curr;
            }
}
int delete()
{
            int d=0;
            d=front->data;
            curr=front;
            front=front->link;
            free(curr);
            return d;
}
void display()
{
            curr=front;
            if(front==NULL)
                        printf("\nQueue empty!\n");
            else if(front==rear)
                        printf("\nQueue is:%d",curr->data);
            else      {
            printf("\nQueue is:\n");
            while(curr->link!=NULL)
            {
                        printf("%d\t",curr->data);
                        curr=curr->link;
            }
            printf("%d\n",curr->data);      }
}

void main()
{
            int op,data;
            clrscr();
            do
            {
                        printf("\nQUEUE OPERATIONS:\n1.Insert\n2.Delete\n3.Exit\n");
                        printf("Enter the operation to be performed:");
                        scanf("%d",&op);
                        switch(op)
                        {
                                    case 1:
                                    {
                                                printf("Enter the element to be inserted:");
                                                scanf("%d",&data);
                                                insert(data);
                                                break;
                                    }
                                    case 2:
                                    {
                                                data=delete();
                                                if(data)
                                                            printf("Deleted element: %d",data);
                                                break;
                                    }
                                    case 3:
                                                break;
                                    default:
                                                printf("\nEnter a proper option!\n");
                        }
                        display();
            }while(op!=3);
            getch();
}

No comments:

Post a Comment