Thursday, September 11, 2008

c++ program-Queue operation at front end using linked lists

#include < iostream>
#include < stdio.h>

using namespace std;

class node
{
public: int data;
node *ptr;
};

class linkedList
{
private: node *head;

public: linkedList() //constructor
{
head = NULL;
}

void insert_front();
int delete_front();
void display();

~linkedList() //destructor
{
delete head;
}
};// end of LL

void linkedList::insert_front()
{
node *newNode;
int item;
newNode= new node;
cout<< "enter the item to be inserted" < cin>> item;
newNode->data=item;
newNode->ptr=head;
head=newNode;
}

int linkedList::delete_front(void)
{
node *temp;
int item;
if(head==NULL)
{
cout<< "list is empty"<< endl;
return 1;
}

temp = head;
cout<< "deleted item is"<< temp->data<< endl;
item=temp->data;
head=temp->ptr;
delete(temp);
return item;
}

void linkedList::display()
{
node *temp;
temp=head;
cout<< " \n enter contents of list"<< endl;

while(temp!=NULL)
{
cout<< temp->data<< "->";
temp=temp->ptr;

}
cout<< "NULL"<< endl;
}

void main()
{
linkedList ll;
int choice, rpt;

while(rpt)
{
cout<< "1. Insert at front"<< endl;
cout<< "2. Delete from front"<< endl;
cout<< "3. Display"<< endl;
cout<< "4. exit"<< endl;

cout<< "enter the choice";

cin>> choice;

switch(choice)
{
case 1: ll.insert_front();
break;
case 2: ll.delete_front();
break;
case 3: ll.display();
break;
case 4: return;
}
cout<< "do you want to continue: yes: 1 or No: 0"<< endl;
cin>> rpt;
}
}

0 comments: