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

linkedlist طلب

بدأه سمآ.." في 13 يوليو 2011 · 6 رد · 672 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمه الله وبركآته

آخوتي ارجو مسآعدتي

أريد كتآبه كود بلغه c++ عن linked List

آنآ كتبت الكود كآمل

بس عندي مشكله بسطيه ابغى مسآعدتكم

وهي المطلوب من السؤال بحث عن قيمه , ثم اضافه node جديده بالمنتصف تحمل قيمه 99

لانني لآ آعرف كيف اضيف node جديده بالمنتصف

وهذا الكود

#include "stdafx.h" 
#include <iostream>
using namespace std;
//------ class IntNode for creating new node ----------------//
class IntNode {
public :
IntNode(int el, IntNode *ptr = 0) {info = el; next = ptr;}
int info;
IntNode *next;
};
//------ class IntLList for dealing with nodes --------------//
class IntLList {
public:
IntLList() {head = tail =0; }
void AddToHead(int);
void AddToTail(int);
void DeleteFromHead();
void DeleteFromTail();
void DeleteNode(int);
void FindNode(int);
void addnewNode(int);
void DisplayList();
private:
IntNode *head, *tail;
};
void IntLList::AddToHead(int data)
{
IntNode *newnode;
newnode = new IntNode(data,0);
newnode->next = head;
head = newnode;
if (tail==0)
tail = head;
}
void IntLList::AddToTail(int data)
{
IntNode *newnode;
newnode = new IntNode(data,0);
tail->next = newnode;
tail = newnode;
}

void IntLList::DeleteFromHead()
{
if(head!=0)
head = head->next;
if(head==0)
tail=head;
}
void IntLList::DeleteFromTail()
{
IntNode *current;
current = head;
while(current->next != tail)
{
current=current->next;
}
tail=current;
tail->next=0;
}
void IntLList::FindNode(int val)
{
IntNode *current;
current = head;
while(current != tail->next)
{
if (current->info == val)
cout << current->info << " " << current << "\n";
current=current->next;
}
cout << "----------------------" << "\n";
}

void IntLList::addnewNode(int val)
{
?????????
}
void IntLList::DisplayList()
{
IntNode *current;
current = head;
cout << "head = " << head << "\n";
while(current != tail->next)
{
cout << current->info << " " << current << "\n";
current=current->next;
}
cout << "tail = " << tail << " " << tail->next << "\n";
cout << "----------------------" << "\n";
}

void main()
{
IntLList mag;
mag.AddToHead(50);
mag.AddToHead(90);
mag.AddToHead(60);
mag.DisplayList();
mag.DeleteFromHead();
mag.DisplayList();
mag.AddToTail(88);
mag.AddToTail(77);
mag.AddToHead(66);
mag.AddToHead(50);
mag.DisplayList();
mag.DeleteFromTail();
mag.DisplayList();
mag.FindNode(50);
mag.addnewNode(50);
mag.DisplayList();
}

تم تعديل هذه المشاركة بواسطة سمآ.." في 13 يوليو 2011 في 21:18

#2
سمآ.." كتب:

...

void main()

...

حشرحلك أي شيء أنت عاوزة.

بس سؤال الأول.

مين قاللك أن main ال-return بتعها void ؟؟؟

و أنت في أي جامعة ؟

وَمَا أُوتِيتُمْ مِنْ الْعِلْمِ إِلاَّ قَلِيلاً

#3

هو أنت عاوز تضيف node فيه أخر اللست ؟ ولا عاوز تضيف value في النصف ?

وَمَا أُوتِيتُمْ مِنْ الْعِلْمِ إِلاَّ قَلِيلاً

#4

انا اللي اعرفه ان void ماترجع لي قيمه

اما int ترجع لي قيمه

واللي ابغاه اضافه node جديده تحمل value = 99 بالمنتصف

اللي احنا اخذناه هو اضافه node عند head و tail يعني في بدايه الlinked list وفي نهايته

#5

و علیكم السلام و رحمة الله و بركاته

أختنا الكريمة, تفضلي ( و مع تصحيح بعض أخطاء في دوال الحذف)

و سؤالك ليس واضح. يعني ماذا في المنتصف؟

على اي حال, الان دالة addNewNode()

تطلب منك قيمة في الليست و قيمة جديدة و ثم تقوم باضافة القيمة الجديدة بعد القيمة الموجودة في الليست.

#include <iostream>
using namespace std;
//------ class IntNode for creating new node ----------------//
class IntNode 
{
public :
	IntNode(int el, IntNode *ptr = 0) {info = el; next = ptr;}
	int info;
	IntNode *next;
};
//------ class IntLList for dealing with nodes --------------//
class IntLList 
{
public:
	IntLList() {head = NULL; tail=NULL; }
	void AddToHead(int);
	void AddToTail(int);
	void DeleteFromHead();
	void DeleteFromTail();
	void DeleteNode(int);
	void FindNode(int);
	void addnewNode(int,int);
	void DisplayList();
private:
	IntNode *head, *tail;
};
void IntLList::AddToHead(int data)
{
	IntNode *newnode;
	newnode = new IntNode(data,0);
	newnode->next = head;
	head = newnode;
	if (tail==0)
		tail = head;
}
void IntLList::AddToTail(int data)
{
	IntNode *newnode;
	newnode = new IntNode(data,0);
	if (head == NULL)
	{
		head = newnode;
		tail = newnode;
	}
	else
	{
		tail->next = newnode;
		tail=newnode;
	}
}

void IntLList::DeleteFromHead()
{
	if (head == NULL)
		return;
	else 
	{
		IntNode *removedNode;
		removedNode = head;
		if (head == tail) 
		{
			head = NULL;
			tail = NULL;
		} 
		else
		{
			head = head->next;
		}

		delete removedNode;
	}
}

void IntLList::DeleteFromTail()
{
	if (tail == NULL)
		return;
	else 
	{
		IntNode *removedNode;
		removedNode = tail;
		if (head == tail) 
		{
			head = NULL;
			tail = NULL;
		}
		else
		{
			IntNode *previousToTail = head;
			while (previousToTail->next != tail)
				previousToTail = previousToTail->next;
			tail = previousToTail;
			tail->next = NULL;
		}
		delete removedNode;
	}
}

void IntLList::FindNode(int val)
{
	IntNode *current;
	current = head;
	while(current != tail->next)
	{
		if (current->info == val)
			cout << current->info << " " << current << "\n";
		current=current->next;
	}
	cout << "----------------------" << "\n";
}

void IntLList::addnewNode(int previousVal, int newVal)
{
	IntNode *preNode; //previous node and if found we will add new value after it.
	preNode = head;
	bool Found=false;
	while(preNode != tail->next)
	{
		if (preNode->info == previousVal)
		{
			Found=true;
			break;
		}
		preNode=preNode->next;
	}
	if (Found==true)
	{ 
			IntNode *newNode;
			newNode=new IntNode(newVal,0);
			newNode->next = preNode->next;
			preNode->next = newNode;
	}
	else // there was not such a number in list
	{
		cout<<"\nError! We couldn't find such a number in list\n";
	}
}
void IntLList::DisplayList()
{
	IntNode *current;
	current = head;
	cout << "head = " << head << "\n";
	while(current != tail->next)
	{
		cout << current->info << " " << current << "\n";
		current=current->next;
	}
	cout << "tail = " << tail << " " << tail->next << "\n";
	cout << "----------------------" << "\n";
}

int main()
{
	IntLList mag;
	mag.AddToHead(50);
	mag.AddToHead(90);
	mag.AddToHead(60);
	mag.DisplayList();
	mag.DeleteFromHead();
	mag.DisplayList();
	mag.AddToTail(88);
	mag.AddToTail(77);
	mag.AddToHead(66);
	mag.AddToHead(50);
	mag.DisplayList();
	mag.DeleteFromTail();
	mag.DisplayList();
	mag.FindNode(50);
	mag.addnewNode(50,99);
	mag.DisplayList();
	mag.addnewNode(88,11);
	mag.addnewNode(90,22);
	mag.DisplayList();

	return 0;
}

-Ahmed Hassan كتب:

مين قاللك أن main ال-return بتعها void ؟؟؟

و أنت في أي جامعة ؟

صحیح, هذا السوال (هنـا) يشرح كل شيء :)

شكرا لك أخوي للتنبيه

و جامعتها هنآك في مدينتها :)

تم تعديل هذه المشاركة بواسطة IO JOC في 14 يوليو 2011 في 15:59

1

..:: رَبِّ زِدْني عِلْمًا ::..

#6
IO JOC كتب:

و علیكم السلام و رحمة الله و بركاته

أختنا الكريمة, تفضلي ( و مع تصحيح بعض أخطاء في دوال الحذف)

و سؤالك ليس واضح. يعني ماذا في المنتصف؟

على اي حال, الان دالة addNewNode()

تطلب منك قيمة في الليست و قيمة جديدة و ثم تقوم باضافة القيمة الجديدة بعد القيمة الموجودة في الليست.

#include <iostream>
using namespace std;
//------ class IntNode for creating new node ----------------//
class IntNode 
{
public :
	IntNode(int el, IntNode *ptr = 0) {info = el; next = ptr;}
	int info;
	IntNode *next;
};
//------ class IntLList for dealing with nodes --------------//
class IntLList 
{
public:
	IntLList() {head = NULL; tail=NULL; }
	void AddToHead(int);
	void AddToTail(int);
	void DeleteFromHead();
	void DeleteFromTail();
	void DeleteNode(int);
	void FindNode(int);
	void addnewNode(int,int);
	void DisplayList();
private:
	IntNode *head, *tail;
};
void IntLList::AddToHead(int data)
{
	IntNode *newnode;
	newnode = new IntNode(data,0);
	newnode->next = head;
	head = newnode;
	if (tail==0)
		tail = head;
}
void IntLList::AddToTail(int data)
{
	IntNode *newnode;
	newnode = new IntNode(data,0);
	if (head == NULL)
	{
		head = newnode;
		tail = newnode;
	}
	else
	{
		tail->next = newnode;
		tail=newnode;
	}
}

void IntLList::DeleteFromHead()
{
	if (head == NULL)
		return;
	else 
	{
		IntNode *removedNode;
		removedNode = head;
		if (head == tail) 
		{
			head = NULL;
			tail = NULL;
		} 
		else
		{
			head = head->next;
		}

		delete removedNode;
	}
}

void IntLList::DeleteFromTail()
{
	if (tail == NULL)
		return;
	else 
	{
		IntNode *removedNode;
		removedNode = tail;
		if (head == tail) 
		{
			head = NULL;
			tail = NULL;
		}
		else
		{
			IntNode *previousToTail = head;
			while (previousToTail->next != tail)
				previousToTail = previousToTail->next;
			tail = previousToTail;
			tail->next = NULL;
		}
		delete removedNode;
	}
}

void IntLList::FindNode(int val)
{
	IntNode *current;
	current = head;
	while(current != tail->next)
	{
		if (current->info == val)
			cout << current->info << " " << current << "\n";
		current=current->next;
	}
	cout << "----------------------" << "\n";
}

void IntLList::addnewNode(int previousVal, int newVal)
{
	IntNode *preNode; //previous node and if found we will add new value after it.
	preNode = head;
	bool Found=false;
	while(preNode != tail->next)
	{
		if (preNode->info == previousVal)
		{
			Found=true;
			break;
		}
		preNode=preNode->next;
	}
	if (Found==true)
	{ 
		if (preNode == tail)
			AddToTail(newVal);
    	else 
		{
			IntNode *newNode;
			newNode=new IntNode(newVal,0);
			newNode->next = preNode->next;
			preNode->next = newNode;
		}
	}
	else // there was not such a number in list
	{
		cout<<"\nError! We couldn't find such a number in list\n";
	}
}
void IntLList::DisplayList()
{
	IntNode *current;
	current = head;
	cout << "head = " << head << "\n";
	while(current != tail->next)
	{
		cout << current->info << " " << current << "\n";
		current=current->next;
	}
	cout << "tail = " << tail << " " << tail->next << "\n";
	cout << "----------------------" << "\n";
}

int main()
{
	IntLList mag;
	mag.AddToHead(50);
	mag.AddToHead(90);
	mag.AddToHead(60);
	mag.DisplayList();
	mag.DeleteFromHead();
	mag.DisplayList();
	mag.AddToTail(88);
	mag.AddToTail(77);
	mag.AddToHead(66);
	mag.AddToHead(50);
	mag.DisplayList();
	mag.DeleteFromTail();
	mag.DisplayList();
	mag.FindNode(50);
	mag.addnewNode(50,99);
	mag.DisplayList();
	mag.addnewNode(88,11);
	mag.addnewNode(90,22);
	mag.DisplayList();

	return 0;
}

صحیح, هذا السوال (هنـا) يشرح كل شيء :)

شكرا لك أخوي للتنبيه

و جامعتها هنآك في مدينتها :)

أنا لاحظت في قسم علوم كمبيوتر فيه منتدي جامعة عين شمس، أن فيه بعض الطلاب يستخدمون void لل-return.

link

أنا مستغرب أن يكون طالب فيه علوم كمبيوتر و مش بيستخدم مراجع جيدة

وَمَا أُوتِيتُمْ مِنْ الْعِلْمِ إِلاَّ قَلِيلاً

#7

IO JOC

آلف شششكر لك آخوويْ

فرج الله عليك دنيآ وآخرررهـ

وآسعدك المولى آينمآ كنت

_____

آحمد

شكرآ لك آخيْ على التووضيح

لآحرمت الاجر

لكن آنآ لست علوم كمبيوتر , آنآ شبكآت (network)

فقط درسنآ برمجه 1 , لذلك ليسى لدي العلم الكآفي بالبرمجه

وهذه المآده هي data structurs (هياكل البيآنآت )

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