how can I do that ? the easy way then?
circular link list in c++
Collapse
X
-
Here comes an example on how to use a stack. You can insert it at the end of the program I posted some posting ago ... you know, the one with list_a and list_b ...Originally posted by saraSShow can I do that ? the easy way then?
Code:ListClass *tmp_list_ptr; // define a std::stack stack<ListClass *> op_stack; // get the number of elems on the stack cout << "Stack elements:" << op_stack.size() << endl; // push elems onto the stack op_stack.push( &list_b ); op_stack.push( &list_a ); cout << "Stack elements after 2x push:" << op_stack.size() << endl; // get the top element tmp_list_ptr = op_stack.top(); cout << "No. of elems in top list: " << tmp_list_ptr->NumItems() << endl; cout << "Stack elements after top():" << op_stack.size() << endl; // remove the top element op_stack.pop(); cout << "Stack elements after pop():" << op_stack.size() << endl;
Comment
-
Your example looks look but does not work with my array of circular list or at least I dont know how to make it work so stuck again but i guess everybody in my class is having problem because we got more time I'm gonna try to leave the array in main just to create the set of circular list and in my funtion evaluation want to do the expresion evaluarion as a stack based postfix
like this
int main()
{
ListNodeClass *node;
ListClass *tmp_list_ptr;
int i;
cin>>arraysize;
ListClass list[arraysize];
for(i=0;i<array size;i++)
{
while(cin>>numb er && number!=-1)
{
node = new ListNodeClass;
node->Info = number;
list[i].InsertFront(*n ode);
}
cout<<list[i].NumItems()<<" ";
list[i].Printing();
cout<<endl;
}
Evaluate();
return 0;
} and for the evaluation
void Evaluate()
{
int i,l,A,B,Q,Z;
char postfix[80];
stack<ListClass *> op_stack;
ListNodeClass *tmp,*e[arraysize];
cin.getline (postfix,80);
SetExpression (postfix);
cout<<"ok";
puts(postfix);
cout<<"ok";
strcpy(eval,pos tfix);
cout<<"ok";
puts(eval);
cout<<"ok";
l=strlen(eval);
for(i=0;i<l;i++ )
{
if(eval[i]>=97 && eval[i]<=122)
{
e=eval[i];
tmp=CopyList(e[i]);
op_stack.push(e val[i]);
cout<<eval[i];
}
else
{
switch(eval[i])
{
case 'U': break;
case 'I': break;
case '-': break;
//case '\'' : break;
case '<' : break;
case '&' : break;
case '|' : break;
case '!' : break;
}
//op_stack.push(Q );
}
}
op_stack.pop();
} but getting some errors
dont know what to do
?Comment
-
So, as far as I understand. If you got all things running (i.e. the creation of the array with the singly-linked circular list), you start to evaluate the expression by scanning it and performing the required action.
So for the sample expression abcdefUUUUIabIa cIadIaeIafIUUUU = from your assignment, you push pointers to the lists (IMHO it would be sufficient to use the list identifier, but anyway) onto your stack, so if you reach the first 'U', your stack should look like
f e d c b a
with the top element on the left.
U means set union, so you got to unite the top 2 elements (i.e. lists on your stack). The result is the new top element, so after the U the stack looks like
f+e d c b a
I would say (correct me if I get things wrong here).
So, what you need to do technically is to invoke a routine to unite two lists :-) You may also need to allocate some result list, where you store the temporary result of the last operation (for the f+e list in the example above). The pointer to this temp result is pushed onto your stack.
The lists in your array should not be changed as they're needed for reference in later operations ...Comment
-
yes, I need to read the last line in my input file, and with that line if I have an "a" I need to make a copy of the first circular list and after that push it into the stack I have this fuction but not working think because i need a pointer or something
int main()
{
int i,m;
char postfix[80];
char listname = 97;
cin>>arraysize;
ListClass ListA[arraysize],L[arraysize];
for(i=0;i<array size;i++)
{
while(cin>>numb er && number!=-1)
{
ListA[i].InsertRear(num ber);
}
cout<<ListA[i].NumItems()<<" ";
ListA[i].printing();
cout<<endl;
}
Initialize ( );
cin.getline(pos tfix,80,'\n');
SetExpression (postfix);
cout <<postfix<< endl;
puts(postfix);
cout<<"ok";
strcpy(eval,pos tfix);
m=Evaluate( );
printf("answer: %d", m );
return 0;
}in main and function is
int Evaluate()
{
int i,l,A,B,Q,Z;
l=strlen(eval);
for(i=0;i<l;i++ )
{
if(eval[i])
{
Push(eval[i]);
}
else
{
A=Pop( );
B=Pop( );
switch(eval[i])
{
case 'U': break;
case 'I': break;
case '-': break;
case '\'' : break;
case '<' : break;
case '&' : break;
case '|' : break;
case '!' : break;
}
Push(Q);
}
}
Z=Pop( );
return Z;
}Comment
-
Hmm. As far as I understand, you don't need to make a copy of the list and push it on the stack. Just push pointers (or even symbols!) on the stack. Suppose the last line, i.e. the postfix expression, looks likeOriginally posted by saraSSyes, I need to read the last line in my input file, and with that line if I have an "a" I need to make a copy of the first circular list and after that push it into the stack I have this fuction but not working think because i need a pointer or something
a b U
This means
push a on the stack
push b on the stack
unite, i.e. pop the top element from the stack, pop the top element from the stack (2 times!) and unite it.
push the result on the stack.
It is sufficient to push the symbol/pointer on the stack and to get the list content if you really need it, i.e. for the unite operation. Hence, for every operation you need a function which receives one or two symbols/pointers (the two symbols a and b in our example), fetches the list(s) contents from the array and performs the oeration. Afterwards the result has to be pushed on the stack. For this temporary result you need an additional list (and symbol/pointer!).
So, you don't have to push copies of the list on the stack, just pointers or symbols. This is much more efficient. Consider the example:
a a a a a a a ...
you would make several copies of the list a, but if you only use the pointer/symbol you save a lot of space and lose no information. You know what I mean?Comment
-
I am not sure what you're trying to do here. The ListA is the array for the circular lists? What about the stack? Do you try to use the STL stack?Originally posted by saraSSyes I know what you mean but I dont know how to do the pointer part I have tried some thing like
ListClass ListA[arraysize],*L; and this ListClass ListA[arraysize],*L[arraysize];
but nothing works pointing to every set is my problem
Can you explain that in more detail?Comment
-
I think I got the pointer part working but now looks like I'm not reading the last line of my input file (last line is the one with the expression)to do the evaluation
int main()
{
int i,m;
char postfix[80];
cin>>arraysize;
ListClass ListA[arraysize];
for(i=0;i<array size;i++)
{
while(cin>>numb er && number!=-1)
{
ListA[i].InsertRear(num ber);
}
cout<<ListA[i].NumItems()<<" ";
ListA[i].printing();
cout<<endl;
}
Initialize ( );
cin.getline(pos tfix,80);
cout<<postfix<< endl;
Evaluate(ListA, postfix);
return 0;
}
what I'm doing wrong think my while loop is using all the lines ?
idont knowComment
-
Looks ok to me .. what does the lineOriginally posted by saraSSI think I got the pointer part working but now looks like I'm not reading the last line of my input file (last line is the one with the expression)to do the evaluation
int main()
{
int i,m;
char postfix[80];
cin>>arraysize;
ListClass ListA[arraysize];
for(i=0;i<array size;i++)
{
while(cin>>numb er && number!=-1)
{
ListA[i].InsertRear(num ber);
}
cout<<ListA[i].NumItems()<<" ";
ListA[i].printing();
cout<<endl;
}
Initialize ( );
cin.getline(pos tfix,80);
cout<<postfix<< endl;
Evaluate(ListA, postfix);
return 0;
}
what I'm doing wrong think my while loop is using all the lines ?
idont know
write to standard out?Code:cout<<postfix<<endl;
Comment
Comment