PDA

View Full Version : سوال: مشکل با پاک کردن لیست پیوندی



root88
سه شنبه 07 اردیبهشت 1389, 11:02 صبح
با سلام ،دوستان من با تابع زیری که یه لیست رو پاک میکه مشکل دارم.یه لیست ایجاد کردم که یه تعداد عنصر به تصادف ایجاد و توش قرار میگره .وقتی می خوام که لیست رو پاک کنم و دوباره توش داده بریزم دچار مشکل میشه و به تعداد خیلی زیادی گره با همان مقداری که می خوام توش بریزم ایجاد میکنه بطوری که نمی تونم تعدادشونو با تابع count حساب کنم .لطفا راهنمایی بفرمایید
برنامه:


int GenRandom(int lenght)
{
unsigned int Rand;
srand(time(0));
return Rand=rand()%lenght;
}
int main()
{int c=0,n=10,x;
x=GenRandom(n);
List testList; // first is NULL
testList.Clear();
testList.InsertEnd(x,1);
x=GenRandom(n);
testList.InsertEnd(x,2);
c= testList.Count();
testList.Clear();
x=GenRandom(n);
testList.InsertEnd(x,2);
c= testList.Count();

getch();
return 0;
}



تابع clear:


void Clear()
{
ListNode* t=NULL,* h=NULL;
t = first;
while(t != NULL)
{
h = t;
t = t->link ;
delete h;
}
}

root88
سه شنبه 07 اردیبهشت 1389, 13:59 عصر
دوستان لطفا برای حل این مشکل کمک کنید.

aakh1361
سه شنبه 07 اردیبهشت 1389, 14:02 عصر
همه كد رو قرار بده

root88
سه شنبه 07 اردیبهشت 1389, 14:16 عصر
اینم کد دوستان



// test2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "test2.h"
#include <iostream>
#include<conio.h>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
class ListNode
{
public:
int data1;
float data2;
bool action;
ListNode* link;
ListNode(int a,float b,bool c,ListNode* p=NULL):data1(a),data2(b),action(c),link(p){}
};
//------------------------------end class ListNode----------------------------------//
class List
{
protected:
ListNode* newNode(int a,float b,bool c,ListNode* p)
{
ListNode* q=new ListNode(a,b,c,p);
return q;
}
public:
ListNode* first;
List(int element1=0,float element2=0,bool element3=true,ListNode* p=NULL)
{
if(element1==0 && element2==0 && element3==true && p==NULL)
first=NULL;
else
first=newNode(element1,element2,element3,p);
}
void InsertFirst(int element1,float element2=0,bool element3=true)
{
ListNode* p=newNode(element1,element2,element3,first);
first=p;
}
void InsertEnd(int element1,float element2=0,bool element3=true)
{
ListNode* h=NULL;
ListNode* p=newNode(element1,element2,element3,NULL);
if(!first)
first=p;
else
{
h=first;
while(h->link!=NULL)
h=h->link;
h->link=p;
}
}
void deleteNode(int element)
{
ListNode* t=NULL,* h=NULL;
t = first;
if(first==NULL)
{
cout<<"delete a ction error : The list is empty!"<<endl;
return;
}
if(t->data1 == element)
{
first = t->link;
delete t;
return;
}
h=t;
while(t != NULL)
{
if(t->data1 == element)
{
h->link = t->link ;
delete t;
return;
}
h=t;
t=t->link ;
}
cout<<"delete action error : element" <<" "<<element<<" "<<"Not found"<<endl;
}
int Count()
{
ListNode *t=NULL;
int count=0;
for(t=first;t!=NULL;t=t->link)
count++;
return count;
}
void Display()
{
ListNode* current=NULL;
current = first;
while(current != NULL)
{
cout<<current->data1<<" ";
current = current->link;
}
}
float stopCondition()
{
float prob=1;
ListNode* t=NULL;
for(t=first;t!=NULL;t=t->link)
prob *=t->data2;
return prob;
}
ListNode* searchPoint(int x)
{
ListNode* t=NULL;
t=first;
while(t!=NULL)
{
if(t->data1==x)
return t;
else
t=t->link;

}
t=NULL;
return t;
}
bool IsInList(int v)
{
ListNode* t=NULL;
t=first;
while(t!=NULL)
{
if(t->data1==v)
return true;
else
t=t->link;
}
return false;
}
int GetDataBack(int v)
{
ListNode* t=NULL;
t=first;
for(int i=0;i<v;i++)
t=t->link;
return t->data1;
}
~List()
{
ListNode* t=NULL,* h=NULL;
t = first;
while(t != NULL)
{
h = t;
t = t->link ;
delete h;
}
}

void Clear()
{
ListNode* t=NULL,* h=NULL;
t = first;
while(t != NULL)
{
h = t;
t = t->link ;
delete h;
}
}
};
int GenRandom(int lenght)
{
unsigned int Rand;
srand(time(0));
return Rand=rand()%lenght;
}
int main()
{int c=0,n=10,x;

x=GenRandom(n);
List testList; // first is NULL
testList.Clear();
testList.InsertEnd(x,1);
x=GenRandom(n);
testList.InsertEnd(x,2);
c= testList.Count();
testList.Clear();
x=GenRandom(n);
testList.InsertEnd(x,2);
c= testList.Count();
cout<<c;

getch();
return 0;
}

root88
سه شنبه 07 اردیبهشت 1389, 15:38 عصر
ممنونم دوست عزیز!