Wednesday, 16 August 2017

12-C Prog-21-Linked Queue Insertion and Deletion Program


#include<iostream.h>

#include<process.h>
struct Node
{int inf;
Node*next;
}
*front, *newptr,*save,*ptr,*rear;
Node *Create_New_Node(int);
void Insert(Node*);
void Display(Node*);
void DelNode_Q();
int main()
{ front=rear=NULL;
int inf;
char ch='y';
while(ch=='y'||ch=='y')
{
cout<<"enter informtion for the new node...";
cin>>inf;
newptr=Create_New_Node(inf);
if(newptr==NULL)
{ cout<<"\n Cannot create new noe!! Aborting!!\n ";
  exit(1);
}
Insert(newptr);
cout<<"\n press Y to enter more nodes,Nto exit...\n";
cin>>ch;
}//while end
do
{ cout<<"\n The linked -queue now is ( front ...to..rear):\n ";
  Display(front);
  cout<<"want to delete first noe?(y/n)...";
  cin>>ch;
  if(ch=='y'||ch=='Y')
  DelNode_Q();
} while (ch=='y'||ch=='Y');
  return 0;
}//main en
  Node *Create_New_Node(int n)
  { ptr=new Node;
  ptr->inf=n;
  ptr->next=NULL;
  return ptr;
  }
  void Insert(Node *np)
  {
     if(front==NULL)
       {front=rear=np;}
     else
       {rear->next=np;
       rear=np;}
  }
  void DelNode_Q()
  {
   if ( front==NULL)
    cout<<"UNDERFLOW!!\n";
   else
   {ptr=front;
    front=front->next;
    delete ptr;
   }
  }
  void Display(Node*np)
  {
    while(np!=NULL)
     { cout<<np->inf<<" ";
       np=np->next;
     }
    cout<<"!!\n";
  }

No comments:

Post a Comment