19 Ocak 2011 Çarşamba

c programlamada linked list kullanımı

Yorum Bırak
struct yapımız:
struct node
  {  char name[20];    // Name of up to 20 letters
     int age;          // D.O.B. would be better
     float height;     // In metres
     node *nxt;        // Pointer to next node sıradaki node a ilerlemek için kullanacağımız pointer
     //if you want more data you can define here...
  };

node *start_ptr = NULL; //our starting pointer başlangıç pointerımız


eklemek için:
void add_node_at_end ()
  {  node *temp, *temp2;   // Temporary pointers

     // Reserve space for new node and fill it with data
     temp = new node;
     cout << "Please enter the name of the person: ";
     cin >> temp->name;
     cout << "Please enter the age of the person : ";
     cin >> temp->age;
     cout << "Please enter the height of the person : ";
     cin >> temp->height;
     temp->nxt = NULL;

     // Set up link to this node
     if (start_ptr == NULL)
         start_ptr = temp;//boşsa başlangıç pointerını temp yapıyoruz 
     else  //boş değilse daha onceden ekleme yapmışız demektir ve aşağıdan devam ediyoruz
       { temp2 = start_ptr;
         // We know this is not NULL - list not empty!
         while (temp2->nxt != NULL)
           {  temp2 = temp2->nxt;
              // Move to next link in chain
           }
         temp2->nxt = temp;
       }
  }


Göstermek için:
temp = start_ptr;
do
  {  if (temp == NULL)
       cout << "End of list" << endl;
     else
       {  // Display details for what temp points to
          cout << "Name : " << temp->name << endl;
          cout << "Age : " << temp->age << endl;
          cout << "Height : " << temp->height << endl;
          cout << endl;       // Blank line

          // Move to next node (if present)
          temp = temp->nxt;
       }
  }
while (temp != NULL);

silmek için:
void delete_end_node()
   { node *temp1, *temp2;
     if (start_ptr == NULL)
          cout << "The list is empty!" << endl;
     else
        { temp1 = start_ptr;
          while (temp1->nxt != NULL)
             { temp2 = temp1;
               temp1 = temp1->nxt;
             }
          delete temp1;
          temp2->nxt = NULL;
        }
   }

0 yorum:

Yorum Gönder