ورود

View Full Version : لیست پیوندی



taravat
چهارشنبه 02 آبان 1386, 14:28 عصر
سلام به همه ی اعضای گروه.

برنامه ای با لیست پیوندی دوطرفه ی حلقوی بنویسید که:

1-گره ی p را بعد از گره ی x درج کند. 2-یک گره را از لیست حذف کند.(مثلاً به اسم x)

من این برنامه رو کامل نوشتم. ولی خروجی درستی نمی گیرم. اگه برنامه ی کاملشو دارین

لطفاً برام بفرستین. ترجیحاً تو ++ visual c. ممنونم.

__________________________________________________ ______________

پیامبر اکرم (ص):

بهترین شما کسی است که قرآن را فرا گیرد و به دیگران بیاموزد.

emad_67
چهارشنبه 02 آبان 1386, 17:35 عصر
برنامت رو بزار اینجا تا رفع اشکال کنیم

taravat
چهارشنبه 02 آبان 1386, 18:35 عصر
برنامه را در این ضمیمه ببینید:

emad_67
چهارشنبه 02 آبان 1386, 19:03 عصر
میشه دقیق تری بگی مشکلت چیه و چه خروجی رو انتظار داری؟
اگه مشکل توی یکی از تابع هاست بگو که اونو بررسی کنم

taravat
چهارشنبه 02 آبان 1386, 20:12 عصر
مشکل این برنامه در تابع های addtolist و delfromlist هست. مثلاً وقتی در خروجی داده ای

رو که می خوام پاک بشه وارد می کنم و enter میزنم که ادامه ی برنامه اجرا بشه (یعنی

عمل چاپ عناصر لیست) یک application error میده که توش نوشته

"the instruction at "0x004264bc" referenced memory at "0xddddd". the memory could

not be read ) یا در یه حالت دیگه لیست رو اشتباه چاپ میکنه.علاوه بر این وقتی تو یه اجرا

چند تا داده به لیست اضافه میکنم و enter می زنم که لیست رو چاپ کنه بجای مثلاً6 تا

داده 3 تا داده چاپ میکنه.یعنی برنامه کلاً مشکل داره. من برنامه رو trace کردم ولی اشکال

کار رو متوجه نشدم. برای همین گفتم اگه برنامه ی کاملشو دارین برام بفرستین.

emad_67
چهارشنبه 02 آبان 1386, 23:14 عصر
برنامت رو اصلاح کردم
اشکالاتی که داشتی:
در تابع addtolist در خطی داریم:


x->rlink=p;
x->rlink->llink=p;
که باید این دو خط جابجا بشه یعنی به این صورت


x->rlink->llink=p;
x->rlink=p;
توی حالت اول x->rlink رو برابر p قرار دادی و در خط بعد نوشتی x->rlink->llink=p که معادل این هست


p->llink=p
یعنی اشاره گر سمت چپ p به خودش اشاره میکنه. با تغییر به حالت دوم این مشکل رفع میشه.
اشکال دیگه ای که وجود داشت این بود که وقتی تو تابع addtolist اشاره گر p رو تخصیص حافظه کردی در حلقه do-while از همون p استفاده کردی (بدون تخصیص حافظه مجدد) که باید فسمت تخصیص حافظه p رو به داخل حلقه منتقل کنی که هر دفعه که حلقه تکرار میشه حافظه جدید برای p گرفته بشه.
بنابراین برنامه اصلاح شده اینه:


#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <iomanip>
using std::setw;

#include<string>
using std::string;


struct Dlink{
string name;
Dlink *llink;
Dlink *rlink;
};

Dlink *head,*tail,*x,*temp;
int n;
char answer;


//----------------------------------------readspec-------------------------------
void readspec(){
cout<<"how many students do you want to work with? ";
cin>>n;

cout<<"\n\nenter the name of "<<n<<" students: ";

cin>>tail->name;
for (int i=0;i<n-1;i++)
{
tail->rlink=new Dlink;//allocate a place in the memory to tail->link
tail->rlink->llink=tail;
tail=tail->rlink;//tail point to the place where tail->link is pointing
cin>>tail->name;
}
tail->rlink=head;
head->llink=tail;


cout<<endl<<endl;
}


//----------------------------------------printspec-----------------------------
void printspec(){
temp=head;
do
{
cout<<temp->name<<" ";
temp=temp->rlink;
}
while(temp!=head);
}


//----------------------------------------addtolist-----------------------------
void addtolist(){
Dlink *p;
string name1;

do
{
cout<<"\n\nDo you want to ADD a student to the list?(y/n) ";
cin>>answer;
if (answer=='y')
{
p=new Dlink;
cout<<"please enter the name of this student: ";
cin>>p->name;

cout<<"\nplease enter the name of the student who you want";
cout<<"\nto add a student after her: ";
cin>>name1;

x=head;
while((x->rlink!=head)&&(x->name != name1))
x=x->rlink;
if (name1!=x->name){
cout<<"\nthere is not this name in the list!";
return;
}

p->llink=x;
p->rlink=x->rlink;
x->rlink->llink=p;
x->rlink=p;
if (x==tail)
tail=p;
cout<<"we ADD a student to the list.";
}
printspec();
}
while(answer!='n');
}


//---------------------------------------delfromlist-----------------------------
void delfromlist(){
string delname;

do
{
cout<<"\n\nDo you want to DELETE a student from the list?(y/n) ";
cin>>answer;
if (answer=='y'){
cout<<"please enter the name of this student: ";
cin>>delname;


x=head;
while((x->rlink!=head)&&(x->name!=delname))
x=x->rlink;
if((x->rlink==head)&&(x->name!=delname))
cout<<"There is not a student with this name in the list.\n";

else
{
x->llink->rlink=x->rlink;
x->rlink->llink=x->llink;
if (x==head)
head=x->rlink;
if (x==tail)
tail=x->llink;
delete(x);
cout<<"we DELETE a student from the list.";
}

}
}
while(answer!='n');
}


//--------------------------------------------main--------------------------------
void main()
{
head=new Dlink;
tail=head;

cout<<setw(50)<<"IN THE NAME OF ALLAH\n\n"<<setw(54)<<"CIRCULAR DOUBLY LINKED LIST\n\n";
readspec();

printspec();

addtolist();

cout<<"\n\nthis is the linked list after adding some elements to it: \n";
printspec();

cout<<"\n\n--------------------------------------------------------------------\n";
cout<<"--------------------------------------------------------------------\n";


delfromlist();


cout<<"\n\nthis is the linked list after deleting some elements: \n\n";
printspec();

cout<<endl;
}

taravat
پنج شنبه 03 آبان 1386, 08:55 صبح
سلام. ممنون از اینکه برنامه مو اصلاح کردی.اشکالاتش باعث شد چیزهای بیشتری یاد

بگیرم. متشکرم