الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

احتاج مساعده في حل سؤال !

مغلق
بدأه munatii في 26 أكتوبر 2006 · 15 رد · 1,228 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

هلاااااااااا

كيفكم ؟؟

إن شاء الله بخير

انا كتبت البرنامج كامل بس في خطأ واحد ماعرفت له :o

#include <iostream.h>


struct nodeType
{
	int info;
	nodeType *link;
};

//----------

class linkedList
{
private:
	int count;
	nodeType *first;
	nodeType *last;

public:

	linkedList();
	void initializeList();
	int length();
	void destroy();
	int front();
	int back();
	bool search(int);
	void insertFirst(int);
	void insertLast(int);
	void deleteNode(int);
	void print();
	int splitAt( linkedList, int );

};

//----------------------------
// Constructor

linkedList::linkedList(){ first = NULL; last = NULL; count= 0; }

//--------------------------------------------------------------
// Destroy

void linkedList::destroy()
{
	nodeType *temp;

	while(first != NULL)
	{
		temp = first;
		first = first->link;
		delete temp;
	}

	last = NULL;
	count =0;
}

//--------------------------------------------------------------
// Initialization

void linkedList::initializeList()
{
	destroy();
}

//--------------------------------------------------------------
// Length


int linkedList::length()
{
	return count;
}

//--------------------------------------------------------------
// Front


int linkedList::front()
{
	return first->info;
}

//--------------------------------------------------------------
// Back

int linkedList::back()
{
	return last->info;
}

//--------------------------------------------------------------
// Search


bool linkedList::search(  int num)
{
	nodeType *current;
	bool found;

	current = first;
	found = false;

	while(current != NULL && !found)
		if(current->info == num)
			found=true;
		else
			current = current->link;


		return found;
}

//--------------------------------------------------------------
// Insert First


void linkedList::insertFirst(int num)
{
	nodeType *newNode;

	newNode = new nodeType;

	newNode->info = num;
	newNode->link = first;

	first = newNode;

	count++;

	if(last == NULL)
		last = newNode;
}

//---------------------------------------------------------------
// Delete Node


void linkedList::deleteNode(int num)
{
	nodeType *current;
	nodeType *trailCurrent;
	bool found;

	if(first==NULL)
		cerr<<"Cannot delete from an empty list.\n";
	else
	{
		if(first->info == num)
		{
			current = first;
			first = first->link;
			count--;

			if(first==NULL)
				last = NULL;
			delete current;
		}
		else
		{
			found =  false;
			trailCurrent = first;

			current = first->link;

			while(current != NULL && !found)
			{
				if(current->info != num)
				{
					trailCurrent = current;
					current = current->link;
				}
				else
					found = true;
			}
			if(found)
			{
				trailCurrent->link = current->link;
				count--;

				if(last == current)
					last = trailCurrent;
				delete current;
			}
			else
				cout<<"Item to be deleted is not in the list."<<endl;
		}
	}
}

//-------------------------------------------------------------------
// Insert Last


void linkedList::insertLast(int num)
{
	nodeType *newNode;

	newNode = new nodeType;

	newNode->info = num;
	newNode->link = NULL;

	if(first == NULL)
	{
		first = newNode;
		last = newNode;
		count++;
	}
	else
	{
		last->link = newNode;
		last = newNode;
		count++;
	}
}

//-------------------------------------------------------------------
// Print


void linkedList::print()
{
	nodeType *current;
	current = first;

	while(current != NULL)
	{
		cout<<current->info<<" ";
		current = current->link;
	}
}

//----------------------------------------------------
// Split At

int linkedList::splitAt(  linkedList* list, int num)
{

	nodeType *current = list, *list1, *list2;

	current = list;


	for(int i=0; i<num; i++)
	{
		current = current->link;
	}

	list2 = current->link;
	current->link = NULL
		list2 =list;
}


//----------------------------------------------------

int main()
{
	linkedList list;
	linkedList list2;

	list.insertFirst(1);
	list.insertLast(5);
	list.insertLast(7);
	list.insertLast(3);
	list.insertLast(6);

	cout<<"The list is: ";
	list.print();
	cout<<endl;

	list.print();

	list.splitAt (list2, 7);

	cout<<"The list after spliting is";

	list.print();

	cout<<"list 2 is:";

	list2.print();


	return 0;
}

الخطأ هو

:\Documents and Settings\MoN MoN\Desktop\Assignment 3.cpp(239) : error C2511: 'splitAt' : overloaded member function 'int (class linkedList *,int)' not found in 'linkedList'

/style_im...images/code.gif

 

واهو يأشر على

[code]/ Split At

int linkedList::splitAt(  linkedList* list, int num)
{

	nodeType *current = list, *list1, *list2;

	current = list;


	for(int i=0; i<num; i++)
	{
		current = current->link;
	}

	list2 = current->link;
	current->link = NULL
		list2 =list;
}

ارجو مساعدتكم :^)

#2
اقتباس
int splitAt( linkedList, int );
اقتباس
int linkedList::splitAt( linkedList* list, int num)

عرفتى فين الخطأ :)؟

عموما أول ما الخطأ دا يطلعلك إعرفى إن عندك خطأ كتابى فى ال function declaration or definition

#3

فين الخطأ !! وكيف اصححه ؟

#4

دائماً إذا نغير في LinkedList فإننا نغير على Reference لذلك أتوقع الخطأ هو وجود * في تعريف الداله splitAt

#5
munatii كتب:
فين الخطأ !! وكيف اصححه ؟

:) معقوله ملاحظظتيش الفرق ما بين ال declaration و ال definition!!

هو ال * يا تحطيها فى ال declaration يا تشيليها من ال definition

#6

اذا شلتها راح احصل اخطاء اكثر :( ... مالعمل يعني ؟

#7

المسأله ليست انتهاء أخطاء ، المسأله فهم برمجة اللينكد لست في السي++

www.google.com

#8

حطيها فى ال declaration >

class linkedList
{
private:
	int count;
	nodeType *first;
	nodeType *last;

public:

	linkedList();
	void initializeList();
	int length();
	void destroy();
	int front();
	int back();
	bool search(int);
	void insertFirst(int);
	void insertLast(int);
	void deleteNode(int);
	void print();
	int splitAt( linkedList, int ); // <------- PUT IT HEEEEEREEEEE <------------|

};
#9

شلتها خير شر و استخدمت template

bs still 2 errors

#include <iostream.h>

template <class T>
struct nodeType
{
	T info;
	nodeType<T> *link;
};

//----------
template <class T>
class linkedList
{
private:
	int count;
	nodeType<T> *first;
	nodeType<T> *last;

public:

	linkedList();
	void initializeList();
	int length();
	void destroy();
	T front();
	T back();
	bool search(T);
	void insertFirst(T);
	void insertLast(T);
	void deleteNode(T);
	void print();
	T splitAt( linkedList, T );

};

//----------------------------
// Constructor
template <class T>

linkedList<T>::linkedList(){ first = NULL; last = NULL; count= 0; }

//--------------------------------------------------------------
// Destroy
template <class T>

void linkedList<T>::destroy()
{
	nodeType<T> *temp;

	while(first != NULL)
	{
		temp = first;
		first = first->link;
		delete temp;
	}

	last = NULL;
	count =0;
}

//--------------------------------------------------------------
// Initialization
template <class T>

void linkedList<T>::initializeList()
{
	destroy();
}

//--------------------------------------------------------------
// Length
template <class T>


int linkedList<T>::length()
{
	return count;
}

//--------------------------------------------------------------
// Front
template <class T>


T linkedList<T>::front()
{
	return first->info;
}

//--------------------------------------------------------------
// Back
template <class T>
T linkedList<T>::back()
{
	return last->info;
}

//--------------------------------------------------------------
// Search
template <class T>


bool linkedList<T>::search(  T num)
{
	nodeType<T> *current;
	bool found;

	current = first;
	found = false;

	while(current != NULL && !found)
		if(current->info == num)
			found=true;
		else
			current = current->link;


		return found;
}

//--------------------------------------------------------------
// Insert First
template <class T>

void linkedList<T>::insertFirst(T num)
{
	nodeType<T> *newNode;

	newNode = new nodeType<T>;

	newNode->info = num;
	newNode->link = first;

	first = newNode;

	count++;

	if(last == NULL)
		last = newNode;
}

//---------------------------------------------------------------
// Delete Node
template <class T>

void linkedList<T>::deleteNode(T num)
{
	nodeType<T> *current;
	nodeType<T> *trailCurrent;
	bool found;

	if(first==NULL)
		cerr<<"Cannot delete from an empty list.\n";
	else
	{
		if(first->info == num)
		{
			current = first;
			first = first->link;
			count--;

			if(first==NULL)
				last = NULL;
			delete current;
		}
		else
		{
			found =  false;
			trailCurrent = first;

			current = first->link;

			while(current != NULL && !found)
			{
				if(current->info != num)
				{
					trailCurrent = current;
					current = current->link;
				}
				else
					found = true;
			}
			if(found)
			{
				trailCurrent->link = current->link;
				count--;

				if(last == current)
					last = trailCurrent;
				delete current;
			}
			else
				cout<<"Item to be deleted is not in the list."<<endl;
		}
	}
}

//-------------------------------------------------------------------
// Insert Last
template <class T>

void linkedList<T>::insertLast(T num)
{
	nodeType<T> *newNode;

	newNode = new nodeType<T>;

	newNode->info = num;
	newNode->link = NULL;

	if(first == NULL)
	{
		first = newNode;
		last = newNode;
		count++;
	}
	else
	{
		last->link = newNode;
		last = newNode;
		count++;
	}
}

//-------------------------------------------------------------------
// Print
template <class T>

void linkedList<T>::print()
{
	nodeType<T> *current;
	current = first;

	while(current != NULL)
	{
		cout<<current->info<<" ";
		current = current->link;
	}
}

//----------------------------------------------------
// Split At
template <class T>

T linkedList<T>::splitAt(  linkedList list, T num)
{

	nodeType<T> *current = list, *list1, *list2;



	current = list;
	for(int i=0; i<num; i++)
	{
		current = current->link;
	}

	list2 = current->link;
	current->link = NULL;
		list2 =list;
}


//----------------------------------------------------
template <class T>
int main()
{


	linkedList<int> list;
	linkedList<int> list2;

	list.insertFirst(1);
	list.insertLast(5);
	list.insertLast(7);
	list.insertLast(3);
	list.insertLast(6);

	cout<<"The list is: ";
	list.print();
	cout<<endl;

	list.print();

	list.splitAt (list2, 7);

	cout<<"The list after spliting is";

	list.print();

	cout<<"list 2 is:";

	list2.print();


	return 0;
}

the errors

LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/Assi3.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

Assi3.exe - 2 error(s), 0 warning(s)

what does it mean ?

:wacko: :wacko:

#10

الخطأ هذا اعتقد انه في فيجول 2005 ، اعتقد انك قمتي باختبار تطبيق ويندوز ، يجب استخدام تطبيق كونصول لاستخدام الدالة main .

احمد

BEng , Electronics and communications.

Embedded systems engineer.

Graphics Programmer

عالم الكومبيوتر هو من لديه المعرفة في علوم الكومبيوتر ، الرياضيات ، هندسة الالكترونيات -احمد صالح

#11

كيف اعدله ؟؟

#12

ما تتبعت الكود كامل بس غير هذا الكود

template <class T>
int main()
{


	linkedList<int> list;
	linkedList<int> list2;

	list.insertFirst(1);
	list.insertLast(5);
	list.insertLast(7);
	list.insertLast(3);
	list.insertLast(6);

	cout<<"The list is: ";
	list.print();
	cout<<endl;

	list.print();

	list.splitAt (list2, 7);

	cout<<"The list after spliting is";

	list.print();

	cout<<"list 2 is:";

	list2.print();


	return 0;
}

إلى

int main()
{


	linkedList<int> list;
	linkedList<int> list2;

	list.insertFirst(1);
	list.insertLast(5);
	list.insertLast(7);
	list.insertLast(3);
	list.insertLast(6);

	cout<<"The list is: ";
	list.print();
	cout<<endl;

	list.print();

	list.splitAt (list2, 7);

	cout<<"The list after spliting is";

	list.print();

	cout<<"list 2 is:";

	list2.print();


	return 0;
}

لأن ال main عمرها ما كانت template

#13

الدالة splitAt المنطقق فيها مش مضبوط. فى البداية ضبط شكل العريفات و بعد ذلك يجب مراجعة هذه الدالة. وعلى وجه الخصوص هذين السطرين

nodeType *current, *list2; /// change

	current = list;
#14

the code is working now :rolleyes: but there is one lil thing i cant make it right..

for example.. list 1 = 2, 3,4,5

and i splited it from 4 to list 2

so list 2 is = 4,5

but if i wana split that last ( 2,3,4,5) from 2

meansi should copy the whole elements to list too

but with my codes..

it just copys the first element which is 2 to the second list?

why ??

and how can i copy it all ??

:wacko:

 // Split At
template <class T>

void linkedList<T>::splitAt(  linkedList<T>& list2, T num)
{

	nodeType<T> *current = first, *current2 = first;


	int count2 = 0;

	if(current2->info == num)
	{
		copyList(list2);
	}


	while(current->info != num)
	{
		count2++;
		current = current->link;
	}

	for(int i=0;i<count2 -1;i++)
	{
		current2 = current2->link;
	}
	list2.first=current;
	list2.last=last;
	last=current2;
	current2->link=NULL;

}
//----------------------------------------------------
template <class T>
void linkedList<T>::copyList(linkedList<T>& list2)
{
	nodeType<T> *newNode;
	nodeType<T> *current;


	if(list2.first == NULL)
	{
		first = NULL;
		last = NULL;
		count = 0;
	}

	else 
	{
		current = list2.first;
		count = list2.count;

		first = new nodeType<T>;

		first->info = current->info;
		first->link = NULL;

		last = first;

		current = current->link;

		while(current != NULL)
		{
			newNode = new nodeType<T>;

			newNode->info = current->info;
			newNode->link = NULL;

			last->link = newNode;
			last = newNode;

			current = current->link;
		}
	}
}
#15

ياه إنتى ليه معقدة الأمور كدة؟! الموضوع أبسط من كدة

أنا عدلت شوية على الكود بتاعك و شارح بال comments

void linkedList<T>::splitAt(  linkedList<T>& list2, T num)
{ 
	nodeType<T> *current = first;
	int count2 = 0;

	// the first element contains 'num'
	if (first->info == num)
	{
		list2.first = first;
		list2.last = last;
		list2.count = count;
		first = last = NULL;
		count = 0;
		return;
	}

	// else
	while(current->link!=NULL && current->link->info != num)
	{
		count2++;
		current = current->link;
	}
	// now 'current' is pointing at either:
	//	   + the element whose successor contains 'num'.
	//	   + the last element in the list whose successor is NULL.
	// so we need to check

	if (current == last)	// the list DOESN'T contain 'num' 
	{ 
		list2.first = list2.last = NULL;
		list2.count = 0;
	}

	else					// the list contains 'num'
	{
		// edit list2 members
		list2.first=current->link;
		list2.last=last;
		list2.count = count - count2;

		// edit list1 members
		last=current;
		current->link=NULL;
		count = count2;
	}
}
#16

thaanx

بس يعني ماراح احتاج لل copyList !!!

هذا الموضوع مغلق.

مواضيع مشابهة