I\'m trying having trouble with following C++ code. I need help, getting errors.
ID: 2990084 • Letter: I
Question
I'm trying having trouble with following C++ code. I need help, getting errors.
#include<iostream>
using namespace std;
struct node
{ //node
int info; //regular data
node *link; // pointer to the next node };
node *head; //the first node of the first linked list int size=0; //size of the first linked list node *head2; //the first node of the second linked list int size2=0; //size of the first linked list
void create()
{
node *next; //create a node pointer
cout << "Enter The Size Of The List You Want To Create:"; cin >> size; for(int i=0;i<size;i++)
{
next = new node; //create a new node
next->info=i*5; //put data in the node
next->link = head; //link the node to the head
head = next; // shift the head to the new node
}
cout << "Linked List Created" << endl; }
void print()
{
node *next; //create a node pointer
next=head; // link the node pointer to the head
cout << "No Memory Info" << endl;
cout << "================" << endl;
for(int i=0;i<size;i++)
{
cout << i << " " << next << " " << next->info << endl; //display node address and info
next=next->link; // move node pointer to the next node
}
}
void insert()
{
node *ins,*next; //create two node pointer next=head; //link the node to the head int loc; int value; cout << "Enter The Value To Be Inserted:"; cin >> value;
if(value<0)
{
cout << "Number Is Too Small" << endl;
}
else if(value>next->info)
{
cout << "Number Is Too Large" << endl;
}
else
{
for(int i=0;i<size;i++) // traverse the list to the required node {
if(value>next->info)
{
loc=i;
break;
}
next=next->link;
}
next=head;
for(int i=0;i<loc-1;i++)
next=next->link;
ins = new node; // create a new node
ins->info = value; // insert the required value in the node
ins->link = next->link; // replace the node with the traversed node
next->link = ins; // replace the traversed node with the inserted node
size++; //increase list size
}
}
int main()
{
int choice=0;
create();
while(1)
{
system("cls");
cout << "Menu" << endl;
cout << "====" << endl;
cout << "1. Print List" << endl;
cout << "2. Insert Value" << endl;
cin >> choice;
switch(choice){
case 1:
print();
break;
case 2:
insert();
break;
default:
cout << "Invalid option" << endl;
}
system("pause");
}
return 0;
}
Explanation / Answer
here i'm thomas plz rate i think there is no need anymore to send you code plz rate i'm programmar (Ali)