زهره صالحی
جمعه 21 آبان 1389, 10:43 صبح
سلام
می خوام یه برنامه بنویسم که یک عنصر جدید را طوری درج نماید که لیست مرتب باقی بماند ؟
اینو نوشتم منتها نمی فهمم چرا اشتباهه !
میشه یکی به راهنمایی کنه
#include<iostream.h>
struct Node
{
int info;
Node *next;
}*first , *tmp;
void nodes ()
{
tmp = new Node;
first = tmp;
int x;
cin >> x;
tmp->info = x;
do
{
cin >> x;
tmp->next = new Node;
tmp = tmp->next;
tmp->info = x;
tmp->next = NULL;
}while(x > 0);
}
void SetNumber()
{
tmp = first;
int x;
cout << "Enter Number :: \n";
cin >> x;
//darg dar bian onsor aval ta akhar ast !?
while(tmp->next != NULL)
{
Node *help = new Node;
if(tmp->info > x)
{
help->next = tmp->next;
tmp->next = help;
help->info = x;
break;
}
}
}
void print ()
{
Node *tmp = first;
cout << "Answer :: \n";
while (tmp != NULL)
{
cout << tmp->info << endl;
tmp = tmp->next;
}
}
void main ()
{
nodes();
SetNumber();
print();
}
می خوام یه برنامه بنویسم که یک عنصر جدید را طوری درج نماید که لیست مرتب باقی بماند ؟
اینو نوشتم منتها نمی فهمم چرا اشتباهه !
میشه یکی به راهنمایی کنه
#include<iostream.h>
struct Node
{
int info;
Node *next;
}*first , *tmp;
void nodes ()
{
tmp = new Node;
first = tmp;
int x;
cin >> x;
tmp->info = x;
do
{
cin >> x;
tmp->next = new Node;
tmp = tmp->next;
tmp->info = x;
tmp->next = NULL;
}while(x > 0);
}
void SetNumber()
{
tmp = first;
int x;
cout << "Enter Number :: \n";
cin >> x;
//darg dar bian onsor aval ta akhar ast !?
while(tmp->next != NULL)
{
Node *help = new Node;
if(tmp->info > x)
{
help->next = tmp->next;
tmp->next = help;
help->info = x;
break;
}
}
}
void print ()
{
Node *tmp = first;
cout << "Answer :: \n";
while (tmp != NULL)
{
cout << tmp->info << endl;
tmp = tmp->next;
}
}
void main ()
{
nodes();
SetNumber();
print();
}