Witam!
Mam napisać operacje wstawiania na kolejce jednostronnej, niestety mam problem przy wstawianiu przed jakiś element, pomocy niedługo kolokwium...Zamieszczam cały kod programu ;)
#include <iostream>
using namespace std;
struct node
{
int val;
node *next;
};
node *head=NULL;
node *tail=NULL;
void addhead(int p);
void show();
void addtail(int p);
void wstawza(int a,int b);
void wstaw_przed(int a,int b);
int main()
{
addhead(2);
addhead(3);
addhead(4);
addhead(6);
addtail(7);
show();
cout<<"********* za a ***********"<<endl;
wstawza(6,5);
show();
cout<<"********* przed a(head==a) ***********"<<endl;
wstaw_przed(6,9);
show();
cout<<"********* przed a(head!=a) ***********"<<endl;
wstaw_przed(4,8);
show();
system("pause");
}
void addhead(int p)
{
if(head==NULL)
{
head=new node;
head->next=NULL;
head->val=p;
}
else
{
node *pom=new node;
pom->val=p;
pom->next=head;
head=pom;
pom=NULL;
}
}
void show()
{
if(head==NULL && tail==NULL)
cout<<"Lista pusta"<<endl;
else
{
node *pom=head;
node *p=tail;
while(pom!=NULL)
{
cout<<pom->val<<"->";
pom=pom->next;
}cout<<p->val;
cout<<endl;
}
}
void addtail(int p)
{
if(tail==NULL)
{
tail=new node;
tail->val=p;
tail->next=NULL;
}
else
{
node *pom=new node;
pom->val=p;
pom->next=tail;
tail=pom;
pom=NULL;
}
}
void wstawza(int a,int b)
{
if(head==NULL && tail==NULL)
cout<<"Lista pusta"<<endl;
else
{
node *pom=head;
while(pom->next!=NULL)
{
if(pom->val==a)
{
node *pom2=new node;
pom2->val=b;
pom2->next=pom->next;
pom->next=pom2;
}
pom=pom->next;
}
if(pom->val==a)
{
addtail(b);
}
}
}
void wstaw_przed(int a,int b)
{
if(head==NULL && tail==NULL)
cout<<"Lista pusta"<<endl;
else
{
node *pom=new node;
if (head->val==a)
{
pom->val=b;
pom->next=head;
head=pom;
}
if(pom->val==a) //I ODTĄD CHYBA SIE SYPIE
{
node *pom2=new node;
pom2->val=b;
pom2->next=pom;
pom2->next=pom->next;
}
if(pom->val==a)
{
addhead(b);
}
}
}