#include <iostream>
usingnamespace std;
struct node{
int num;
node *next;
};
bool arTuscias(node *head);
char menu();
node* nodeKurimas(int num);
int size=0;
void sukurimasPradzioje(node *&head, node *&tail, int num);
void spausdinimas(node *dabartinis);
void sukurimasReplace(node **dabartinis, int num, int pos);
int main()
{
node *head = NULL;
node *tail = NULL;
char pasirinkimas;
int num;
int pos;
do{
pasirinkimas = menu();
switch(pasirinkimas){
case'1': cout<<"Iveskite skaiciu: ";
cin>>num;
sukurimasPradzioje(head,tail,num);
break;
case'2': cout<<"Spausdinamas visas sarasas: "<<endl;
spausdinimas(head);
break;
case'3': cout<<"Iveskite elemento pozicija"<<endl;
cin>>pos;
cout<<"Iveskite elementa"<<endl;
cin>>num;
sukurimasReplace(head,num,pos);
break;
default: cout<<"Toks pasirinkimas neegzistuoja"<<endl;
};
}while(pasirinkimas !='7');
return 0;
}
bool arTuscias(node *head){
if(head == NULL)
returntrue;
elsereturnfalse;
}
node* nodeKurimas(int num){
node* temp = new node();
temp->num=num;
temp->next=NULL;
return temp;
}
char menu(){
char pasirinkimas;
cout<<endl<<"Pasirinkite kokia funkcija norite atlikti"<<endl<<
"1. Creates linked list"<<endl<<
"2. Prints linked list"<<endl<<
"3. Inserts an element to a specified position "<<endl<<
cin>>pasirinkimas;
return pasirinkimas;
}
void sukurimasPradzioje(node *&head, node *&tail, int num){
if(arTuscias(head)){
node *temp = new node;
temp->num = num;
temp->next = NULL;
head = temp;
tail = temp;
}
else{
node *temp = new node;
temp->num = num;
temp->next = NULL;
tail->next = temp;
tail = temp;
}
size++;
}
void sukurimasReplace(node **dabartinis, int num, int pos){
// This condition to check whether the
// postion given is valid or not.
if (pos < 1 || pos > size + 1)
cout << "Invalid postion!" << endl;
else {
// Keep looping until the pos is zero
while (pos--) {
if (pos == 0) {
// adding Node at required postion
node* temp = nodeKurimas(num);
// Making the new Node to point to
// the old Node at the same position
temp->next = *dabartinis;
// Changing the pointer of the Node previous
// to the old Node to point to the new Node
*dabartinis = temp;
}
else
// Assign double pointer variable to point to the
// pointer pointing to the address of next Node
dabartinis = &(*dabartinis)->next;
}
size++;
}
}
void spausdinimas(node *dabartinis){
if(arTuscias(dabartinis))
cout<<"Sarasas tuscias"<<endl;
else{
cout<<"Saraso elementai:"<<endl;
while(dabartinis != NULL){
cout<<dabartinis->num<<endl;
dabartinis = dabartinis->next;
}
}
}
You pass a pointer as an argument where a 'pointer to pointer' is required. Solution: pass as '&head' instead o 'head'in line 46.
Beside some small other errors, your code is a good example why it's not good dumping the std library stuff into the global namespace: You cannot anymore use 'size' because it's still defined somewhere by including the <iostream> header. So don't use 'using namespace std'..