Sunday, May 22, 2011

MDU DSA LAB PROGRAMS Write a program to illustrate the working of a Queue which is implemented using pointers. But note that the memory space occupied by a node in a linked representation is not exactly twice the space used by an element of array representation.

#include /*HEADER FILE*/
struct node
{
int inf; /*DECLARATION OF VARIABLES*/
struct node *link;
}

*front, *rear, *newval, *load;

void insert(int);
int del(struct node *);
void display(struct node *);

main()
{
int var,choice; /*VARIABLE DECLARED IN MAIN */
rear='\0';
front='\0';

while(1)
{
printf("\n MAIN MENU");
printf("\n 1.INSERT");
printf("\n 2.DELETE");
printf("\n 3.DISPLAY");
printf("\n 4.ENTER THE CHOICE: ");
scanf("%d",&choice);

switch(choice)
{
case 1: printf("\n enter the element: ");
scanf("%d",&var);
insert(var);
display(front);
break;
case 2: var=del(front);
printf("\n deleted element is %d",var);
display(front);
break;
case 3: display(front);
break;
case 4: exit(0);
break;
default: printf("\n you entered a wrong choice");
break;
}
}
}

void insert(int var) /*NODE INSERTION*/
{
newval=(struct node *)malloc(sizeof(struct node));
newval->inf=var;
newval->link='\0';
if(rear=='\0')
{
rear=newval;
front=newval;
}
else
{
rear->link=newval;
}
}

int del(struct node *front) /*NODE DELETION*/
{
int var;
if (front=='\0')
{
printf("\n queue is empty");
return(0);
}
else
{
var=front->inf;
if(front==rear)
{
front='\0';
rear='\0';
}
else
{
front=front->link;
}
}
return(var);
}
void display(struct nod *front) /*DISPLAY FUNCTION*/
{
load=front;
while(rear->link!='\0')
{
printf("\t %d",load->inf);
load=load->link;
}
}


OUTPUT


MAIN MENU
1.insert
2.delete
3.display
4.enter your choice: 1

enter the element: 528
528
MAIN MENU
1.insert
2.delete
3.display
4.enter your choice: 1
enter the element: 828
528 828

MAIN MENU
1.insert
2.delete
3.display
4.enter your choice: 2
element to be deleted 528
828
MAIN MENU
1.insert
2.delete
3.display
4.enter your choice: 3
828
MAIN MENU
1.insert
2.delete
3.display
4.enter your choice: 4

No comments:

Post a Comment