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

liste doublement chainée

بدأه hind@prg في 28 نوفمبر 2014 · 1 رد · 1,227 مشاهدة · في لغة C و ++C
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

 

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

التمرين فقط عباره عن doubly linked list تحتوي على معلومتين احداهما كلمة و التانيه ترجمتها و طبعا 2 pointers لربط العقد ببعضها

مطلوب function تقوم باضافة عقده و اخرى تحذف عقده و اخرى للعرض

#include<iostream>
#include<conio.h>
#include<cstdlib>
using namespace std;

typedef struct Trans{
	char word[20];
	char translation[20];
	struct Trans*next;
	struct Trans*prev;
}T;


/**************************/
/*         	Add 	   	  */
/**************************/
void Add(T* head , T* queue){

	T* node = new T;
	cout<<"Give a word: ";
	cin>>node->word;
	
	cout<<"Give its translation: ";
	cin>>node->translation;
	
	if(head == NULL){ //case Empty list
		node->prev = NULL;
		node->next = NULL;
		head = node;
		queue = node;
	}else{ 
		node->prev = queue;
		node->next = NULL;
		queue->next = node;
		queue = node;
	}	
}


/**************************/
/*        Display   	  */
/**************************/
void Display(T* head){
	T* node = head;
	
	if(courant != NULL){
		cout<<node->word<<" ---------> "<<node->translation<<endl;
		Display(node->next); 
	}else{
		cout<<"Empty list!";
	}
}

/**************************/
/*        Remove 	  */
/**************************/
void Remove(T* head , char w[20]){
	T* node = head;
	if(head == NULL){
		cout<<"Empty list!" ; 
	}else{
		while((node != NULL)&&(node->word != w)){
			node = node->next;
		}
		if(node->word == w){
			node->prev = node->next;
			free(node);
		}else{
			cout<<"End of list!";
		}
	}
}

/**************************/
/*          Main  		  */
/**************************/
int main(){
	
	T* head = NULL;
	T* queue = head;
	int choice;
	char w , rep;
	
	cout<<"========= MENU ========"<<endl;
	cout<<"Add new word (1)"<<endl;
	cout<<"Display all words (2)"<<endl;
	cout<<"Remove a word (3)"<<endl;
	cout<<"======================="<<endl;
	
	
	do{	
		cout<<"enter a choice: ";
		cin>>choice;
		switch(choice){
			case 1:
				Add(head ,  queue);
				break;
			case 2:
				Display(head);
				break;
			case 3:
				//Remove(head , w);
				//break;		
			default:
				cout<<"Error"<<endl;
				break;
		}
		cout<<"Other choice (y/n)? ";
		cin>>rep;
	}while(rep != 'n');
getch();
return 0;
	
}

تم تعديل هذه المشاركة بواسطة hind@prg في 28 نوفمبر 2014 في 17:16

#2

السلام عليكم ,,
 
المشكل عندك في التابع
Display  به متغير (courant )لم يتم الاعلان عنه !!!!
 
أظن أنك قصدك به المتغير  node ,
,,

/**************************/
/*        Display   	  */
/**************************/
void Display(T* head){
	T* node = head;
	
	if(node != NULL){
		cout<<node->word<<" ---------> "<<node->translation<<endl;
		Display(node->next); 
	}else{
		cout<<"Empty list!";
	}
}

الان يمكن تنفيذ البرنامج بلا أخطاء ,,,
 
لكن :(
 
البرنامج لا يؤدي المطلوب  :(  ,,, عليك بالمحولة l من جديد  ;)
 
ارى ان تضع liste chainée simple  دون  استخدام مؤشرين ,,, مؤشر واحد يكفي والعنصر الاخير يشير للقيمة NULL اي نهاية السلسلة ,,
 
او تقوم بربط آخر عنصر بأول عقدة
 
اليك مثال لتوضيح

1417905194061.png

 

_______________

 

 ساضع محاولة قمت بها " liste chainée simple"

 /* < declaration.h > */
 
#include <iostream>
#include<conio.h>
#include<cstdlib>
#include <string>

/**________________________________________________________________________ **/

typedef struct Trans;
typedef struct Trans *ptr;

/**________________________________________________________________________ **/

struct Trans
{
    string word;
    string translation;
    ptr next;

};


/**________________________________________________________________________ **/

/** ....................  prototype : */
/**________________________________________________________________________ **/

 void  menu     (void);
 void  display  (ptr);
 ptr   add      (ptr);
 ptr   _remove  (ptr,string);

 /**________________________________________________________________________ **/
#include "declaration.h"

using namespace std;

  ptr F = NULL ;
  
 /**
   * Definitions Des Fonctions
   */
 void menu()
{
    char c;
    string s;
    system("color F1");

    do{
        cout<<" _ _ _ _ _ _ _ _ _ _ _ _ _ _";

        cout<<"\n  Add new word        ::  1";

        cout<<"\n  Display all words   ::  2";

        cout<<"\n  Remove a word       ::  3";

        cout<<"\n   Q u i t t e r      ::  4";

        cout<<"\n _ _ _ _  _ _ _ _ _ _ _ _ _";

        cout<<"\n  enter a choice : "; 
        cin>>c;

        switch(c){
         /*========================= case 01 ========================== */
            case '1' :
                cout<<"\n Add new word  :: "<<endl;

                F =  add(F);
                break;

         /*========================= case 02 ========================== */
            case '2' :
                    if( F == NULL )
                     cout<<"\n\n    l a   l i s t e   e s t   VIDE ::  ERReur ... !! ";
                    else{
                         cout<<"\n  Display all words  ::  "<<endl;
                           display(F);
                        }
                 break;

         /*========================= case 03 ========================== */
            case '3' :
                    if( F == NULL )
                       cout<<"\n\n    l a   l i s t e   e s t   VIDE ::  ERReur ... !! ";
                   else {
                       cout<<"\n  Remove a word   :: ";
                       cin>>s;
                       F =  _remove(F,s);
                  }
                 break;
         /*========================= case 04 ========================== */
            case '4':
                     system("cls");
                     cout<<" \n  F I N ...\n\n "; break;

            default : cout<<"\n\n  Choice does not exist  !! ";
        }

        system("pause>1");
        system("cls");

    }while( c != '4');

}
/** _____________________________________________________________________ **/

ptr  add(ptr F)
{
    ptr node,s,p;
    node = new  Trans;

    cout<<"\n :: Give a word: ";
	cin>>node->word;

	cout<<" :: Give its translation: ";
	cin>>node->translation;


    if(F == NULL){
        node->next = F;
        F = node ;
        return F;
    }

    p = F;
    s = p->next ;

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

    p->next = node;
    node->next = s;

    return F ;
}

/** _____________________________________________________________________ **/

ptr _remove   (ptr F,string m)
{
  ptr n,p,s;

  if( F == NULL ){
        cout<<"Empty list!" ;// vide..
    return F;
  }

  if(F->word == m){
    n = F ;
    F = F->next;
    free(n);
    return F;
  }
    p = F;
    s = F->next ;
    while( s != NULL )
    {
        if( s->word == m)
        {
            p->next = s->next ;
            free(s);
            return F;
        }
        p = s ;
        s = p->next;
    }

  return F;
}

/** _____________________________________________________________________ **/

void display(ptr F){
 ptr node ;
 node = F;
 while( node != NULL){
    cout<<"\n ::    "<<node->word<<" ---------> "<<node->translation<<endl;
    node = node->next ;
 }
		cout<<" \n\n\n       ::     Empty list !   :: ";


}
/** _____________________________________________________________________ **/

وأعتذر عن تأخر الرد  حتى الآن... ;)

شكرا

elearning_image.jpg

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