-
ok i'm a litlle nervous here because for 2 days i'm doing this stupid problem
when i enter the middlenode function, x is set before the node with a info...this is for C_ntua to be no doubt...
i finaly made the middlenode function to work
and the delete last node to work sooo
this is removelast
Code:
void removelast(node* y)
{
node *c=first;
while(c->next->next!=NULL)
{
c=c->next;
}
//cout<<"c are valoara"<<c->info;
delete c->next;
c->next=NULL;
last=c;
}
thanks to all of you who have helped me write this program
this is the full program , if someone want to give me some tips or advice is welcomed
Code:
#include <iostream>
using namespace std;
struct node
{
char info;
node *next;
};node *first,*last;
void newnode(char a);
void removefirst();
void removemiddle(node* x);
void removelast(node* y);
int main()
{
int n;
char k;
first=last=NULL;
cout<<"enter the number of nodes that the list have ";
cin>>n;
node *p=new node;
cout<<"enter the first node info ";
cin>>p->info;
p->next=NULL;
first=last=p;
for(int i=2;i<=n;i++)
{
cout<<"enter the value for "<<i<<"node ";
cin>>k;
newnode(k);
}
removefirst();
node *x=last;
while(x->info=='a')
{
removelast(x);
x=last;
}
x=first;
while(x->next!=NULL)
{
if(x->next->info=='a')
{
while(x->next->info=='a')
removemiddle(x);
}
x=x->next;
}
//if(x->info=='a')
// removelast(x);
x=first;
while(x)
{
cout<<x->info;
x=x->next;
}
return 0;
}
void newnode(char a)
{
node *i=new node;
i->info=a;
last->next=i;
last=i;
i->next=NULL;
}
void removefirst()
{
while(first->info=='a')
{
node *c;
c=first;
first=first->next;
delete c;
}
}
void removemiddle(node* x)
{
while(x->next->info=='a')
{
node *c;
c=x->next;
x->next=c->next;
delete c;
}
}
void removelast(node* y)
{
node *c=first;
while(c->next->next!=NULL)
{
c=c->next;
}
//cout<<"c are valoara"<<c->info;
delete c->next;
c->next=NULL;
last=c;
}