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

مساعدة في خوازرمية linklist

بدأه رورو2000 في 2 نوفمبر 2011 · 3 رد · 495 مشاهدة · في الرياضيات والخوارزميات
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

لو سمحتم احتاج مساعدتكم في كتابة برنامج يحتوي على خوارزمية "list" يقوم بإدخال 3اسماء لطلاب وارقامهم

انا قمت بكتابة الخوارزمية لكن واجهت مشكلة في main البرنامج الأساسي لم اعرف كيف استدعي الدالة التي تقوم بإدخال اسم الطالب ورقمه ثم طلب مني ابحث عن اسم احمد واستبدله بإسم علي هل استخدم if في هذا المطلوب؟

 #include <iostream>


using namespace std;

class linklist
{
     public:

             class node
			 {public:
              int data;
            node *next;
char name;
         }*p;

linklist()
{
     p=NULL;
}

void append(int num,char sname)
{
     node *q,*t;

   if( p == NULL )
   {
        p = new node;
      p->data = num;
p->name=sname;
      p->next = NULL;
   }
   else
   {
        q = p;
      while( q->next != NULL )
           q = q->next;

      t = new node;
      t->data = num;
t->name=sname;

      t->next = NULL;
      q->next = t;
   }
}

void add_as_first(int num,char sname)
{
     node *q;

   q = new node;
   q->data = num;
p->name=sname;

   q->next = p;
   p = q;
}

void addafter( int c, int num,char sname)
{
     node *q,*t;
   int i;
   for(i=0,q=p;i<c;i++)
   {
        q = q->next;
      if( q == NULL )
      {
           cout<<"\nThere are less than "<<c<<" elements.";
         return;
      }
   }

   t = new node;
   t->data = num;
t->name=sname;

   t->next = q->next;
   q->next = t;
}

void del( int num )
{
     node *q,*r;
   q = p;
   if( q->data == num )
   {
        p = q->next;
      delete q;
      return;
   }

   r = q;
   while( q!=NULL )
   {
        if( q->data == num )
      {
           r->next = q->next;
         delete q;
         return;
      }

      r = q;
      q = q->next;
   }
   cout<<"\nElement "<<num<<" not Found.";
}

void Traverse()
{
     node *q;
   cout<<endl;

   for( q = p ; q != NULL ; q = q->next )
        cout<<endl<<q->data;

}

int count()
{
     node *q;
   int c=0;
   for( q=p ; q != NULL ; q = q->next )
        c++;

   return c;
}

~linklist()
{
     node *q;
   if( p == NULL )
        return;

   while( p != NULL )
   {
        q = p->next;
      delete p;
      p = q;
   }
}};

int main()
{
	char a[3];
	int b[3];
	int i;
     linklist ll;
	 for(i=0;i<2;i++)
	 {
      ll.add_as_first(b,a);
	 }

for(i=0;i<2;i++)
ll.Traverse()

   int h;
   cin>>h;
}

وشكرا جزيلا مقدما

#2

السلام عليكم ....

قمت ببعض التعديلات ..

حاول الاستفادة من هذه المشاركات ...

singly linked list

أرجو المساعدة

ملاحظة :

الــ add_as_first قمت باستبدال بين p و q انتبه للموضوع ..

#include <iostream>
#include <string.h>


using namespace std;

class linklist {
public:

	class node {
	public:
    	int data;
    	node *next;
    	char name[20];
	} *p;

	linklist() {
    	p = NULL;
	}

	void append(int num, char sname[20]) {
    	node *q, *t;

    	if (p == NULL) {
        	p = new node;
        	p->data = num;
        	strcpy(p->name, sname);
        	p->next = NULL;
    	} else {
        	q = p;
        	while (q->next != NULL)
            	q = q->next;

        	t = new node;
        	t->data = num;
        	strcpy(t->name, sname);

        	t->next = NULL;
        	q->next = t;
    	}
	}

	void add_as_first(int num, char sname[20]) {
    	node *q;

    	q = new node;
    	q->data = num;
    	strcpy(q->name, sname);

    	q->next = p;
    	p = q;
	}

	void addafter(int c, int num, char sname[20]) {
    	node *q, *t;
    	int i;
    	for (i = 0, q = p; i < c; i++) {
        	q = q->next;
        	if (q == NULL) {
            	cout << "\nThere are less than " << c << " elements.";
            	return;
        	}
    	}

    	t = new node;
    	t->data = num;
    	strcpy(t->name, sname);

    	t->next = q->next;
    	q->next = t;
	}

	void del(int num) {
    	node *q, *r;
    	q = p;
    	if (q->data == num) {
        	p = q->next;
        	delete q;
        	return;
    	}

    	r = q;
    	while (q != NULL) {
        	if (q->data == num) {
            	r->next = q->next;
            	delete q;
            	return;
        	}

        	r = q;
        	q = q->next;
    	}
    	cout << "\nElement " << num << " not Found.";
	}

	void Traverse() {
    	node *q;
    	cout << endl;

    	for (q = p; q != NULL; q = q->next)
        	cout << endl << q->data << "  " << q->name;

	}

	int count() {
    	node *q;
    	int c = 0;
    	for (q = p; q != NULL; q = q->next)
        	c++;

    	return c;
	}

	~linklist() {
    	node *q;
    	if (p == NULL)
        	return;

    	while (p != NULL) {
        	q = p->next;
        	delete p;
        	p = q;
    	}
	}
};

int main() {
	char a[20];
	int b;
	int i;
	linklist ll;
	for (i = 0; i < 2; i++)
	{
    	cout << "enter b:";
    	cin >> b;
    	cout << "enter a";
    	cin >> a;

    	ll.add_as_first(b, a);
	}

	//for (i = 0; i < 2; i++)
	ll.Traverse();

	int h;
	cin >> h;
	return 0;
}
1

لا إله إلا الله ... محمد رسول الله

لو كانت مشاركتي مفيدة و تريد تشجيعي على المزيد من العطاء , فضلا قم بتقييم المشاركة

المعرًف القديم : houssam11350_11350

من مواضيعي : ArabGenCode : مولد كود و إجراءات مخزنة و واجهات لجداول سيكوال سيرفر

#3

كذا مضبوط جدا

شكرا لك

#4

طيب طريقة البحث في القائمة ابحث عن اسم احمد اذا حصلته استبدله بإسم علي

كيف طريقته هل استخدم if ؟

وجزاكم الله خيرا

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