954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

link list n queue ...workin code just a small help needed

in want to edit the code in such a way tht the user can enter upto 10 values maximum..need help pls ...

#include <iostream>
#include <iomanip>
using namespace std;

struct node

{

int info;

struct node* next;

}*front,*rear;

void enqueue(int elt);

int dequeue();

void display();

void main()

{

int ch,elt;

rear=NULL;

front=NULL;


while(1)

{

cout<<"Enter:\t1->Insert\t2->Delete\t3->Display\t4->Exit\n";

cin>>ch;

switch(ch)

{

case 1:

	cout<<"Enter The Values: ";

cin>>elt;
enqueue(elt);
break;

case 2:

elt=dequeue();

cout<<"The deleted value = "<<elt<<"\n";

break;

case 3:

display();
cout<<"\n";
break;

default:

cout<<"~~~Exit~~~";

cin.get();cin.get();

exit(0);

break;

}

}

}

void enqueue(int elt)

{

struct node *p;

p=(struct node*)malloc(sizeof(struct node));

p->info=elt;
p->next=NULL;

if(rear==NULL||front==NULL)

front=p;

else

rear->next=p;

rear=p;

}

int dequeue()

{

struct node *p;

int elt;

if(front==NULL||rear==NULL)

{

cout<<"\nThe Queue is empty. Cannot delete further.";

cin.get();cin.get();

exit(0);

}

else

{

p=front;

elt=p->info;

front=front->next;

free(p);

}

return(elt);

}

void display()

{

struct node *t;

t=front;

while(front==NULL||rear==NULL)

{

cout<<"\nQueue is empty";

cin.get();cin.get();

exit(0);

}
cout<<"The numbers in the queue are-->";
while(t!=NULL)

{
cout<<t->info<<"\t";
t=t->next;

}

}
ankit894u
Newbie Poster
24 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Why don't you keep track of how many values the user has entered, and use some mechanism to see if that number's 10?

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 
ankit894u
Newbie Poster
24 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 
Why don't you keep track of how many values the user has entered, and use some mechanism to see if that number's 10?

i cannot think of anything how to keep the track of that or allow the user to input in the array....can u pls help me by editing it...it would be gr8 favor..thanks a lot

ankit894u
Newbie Poster
24 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Example :

#include <iostream>

using namespace std;

int main()
{
   int val[4] = {1,2,3,4};

   int limit = 2;

   for(int i = 0; i < limit; i++)
     {
         cout<<"Enter value : ";
         cin >> val[i]
      }
   return 0;
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

Example :

#include <iostream>

using namespace std;

int main()
{
   int val[4] = {1,2,3,4};

   int limit = 2;

   for(int i = 0; i < limit; i++)
     {
         cout<<"Enter value : ";
         cin >> val[i]
      }
   return 0;
}


hey can u pls edit in my program bcz i m really afraid tht i might mess up d whole program...it already took me too long to write the program...thank u n i appreciate ur help

ankit894u
Newbie Poster
24 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 
hey can u pls edit in my program bcz i m really afraid tht i might mess up d whole program...it already took me too long to write the program...thank u n i appreciate ur help

Then copy and paste it into a text file, and save it. Then start playing around with it. If you mess up too much, then just copy and paste the
original content from the text file.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

hey can someone tell me how to make 10 nodes in the code tht i have made...thts the one way i could think of...

ankit894u
Newbie Poster
24 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 
hey can someone tell me how to make 10 nodes in the code tht i have made...thts the one way i could think of...

Try figuring it out yourself. Then maybe you'll learn something.

Rashakil Fol
Super Senior Demiposter
Team Colleague
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
 
Try figuring it out yourself. Then maybe you'll learn something.


dont u think while writing this code i must have not ried it several times...i did n couldnt do it thts d reason why m here asking for help...ita request if sum1 can really help...maybe in my other projects i wouldnt need d same help bcz by seeing the code i can learn it too

ankit894u
Newbie Poster
24 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Before fixing your code, you should fix your grammar.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

Is it necessary to use the struct data type in your code? All you seem to be doing is inputting and deleting (setting to 0) int values. I would suggest a simple array.

Tell me what you are trying to accomplish, so I do not have to try to reverse engineer your code. To me it seems as if you are trying to enter up to 10 integer values that would not consist of 0. I think I can help you if we don't need the struct data type because I don't understand it.

shaynerossum
Newbie Poster
13 posts since Apr 2009
Reputation Points: 10
Solved Threads: 1
 

Is it necessary to use the struct data type in your code? All you seem to be doing is inputting and deleting (setting to 0) int values. I would suggest a simple array.

Tell me what you are trying to accomplish, so I do not have to try to reverse engineer your code. To me it seems as if you are trying to enter up to 10 integer values that would not consist of 0. I think I can help you if we don't need the struct data type because I don't understand it.

i just have to use linked-list to implement a queue and write a client code to insert 10 numbers then remove them (display then remove).

ankit894u
Newbie Poster
24 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

Oh I'm sorry I don't know how to do that. Good Luck.

shaynerossum
Newbie Poster
13 posts since Apr 2009
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You