السلام عليكم ورحمة الله وبركاته
الاخوة الكرام
من خلال تجولي في هذا المنتدى الرائع والكريم
وجدت عددا كبيرا من الاخوة المبرمجين المتمكنين في البرمجة بلغة c++
وهذا ما شجعني كي أطرح المشكلة التي واجهتني في كود linked list
حيث ان هذه الـ linkedlist هي نصية
لا أعلم ما هي المشكلة فيها
لذا أرجو منكم مساعدتي جزاكم الله كل الخير
#include <assert.h>
#include <string.h>
#include <iostream.h>
struct Node
{
char *data;
Node *next;
Node() { data = NULL; next = NULL; }
~Node()
{
if (data != NULL) delete[] data;
}
void updateData( char * newdata)
{
if (newdata != NULL)
{
if (data != NULL) delete[] data;
data = new char[strlen( newdata) + 1];
strcpy(data, newdata);
}
}
};
class SinglyLinkedList
{
private:
Node *first_ptr;
public:
SinglyLinkedList();
~SinglyLinkedList();
void buildList( char* stringarray[], int arraylength);
void pr intList();
void deleteNode( char *searchdata);
void addNodeBefore( char *searchdata, char * newdata);
void addNodeAfter( char *searchdata, char * newdata);
int length();
bool isEmpty();
bool inList( char *searchdata);
long getIndex( char *searchdata);
void reverse();
void addNodeFirst( char* newdata);
void addNodeLast( char* newdata);
}
SinglyLinkedList::SinglyLinkedList()
{
first_ptr = NULL;
}
SinglyLinkedList::~SinglyLinkedList()
{
Node *current_ptr, *next_ptr;
current_ptr = first_ptr;
// remove elements from the list one by one, traversing starting from first
while (current_ptr != NULL)
{
next_ptr = current_ptr->next;
delete current_ptr;
current_ptr = next_ptr;
}
first_ptr = NULL; // reset first to NULL
}
bool SinglyLinkedList::isEmpty()
{
return first_ptr == NULL;
}
void SinglyLinkedList::buildList(char* stringarray[], int arraylength)
{
int i;
Node *new_ptr, *prev_ptr;
for (i=0; i<arraylength; i++)
{
new_ptr = new Node();
new_ptr->updateData( stringarray );
new_ptr->next = NULL;
if (first_ptr == NULL)
{
first_ptr = new_ptr;
prev_ptr = new_ptr;
}
else
{
prev_ptr->next = new_ptr;
prev_ptr = new_ptr;
}
}
}
void SinglyLinkedList::addNodeBefore(char *searchdata, char *newdata)
{
Node *new_ptr, *prev_ptr, *current;
current = first_ptr;
bool found = false;
while (current !=NULL && found ==false)
{
if (strcmp(current->data , searchdata)==0)
{
found = true;
}
else
{ prev_ptr= current;
current= current->next;
}
}
if (found == true)
{
new_ptr = new Node();
new_ptr->updateData( newdata);
if (first_ptr== current)
{
new_ptr->next = first_ptr;
first_ptr= new_ptr;
}
else
{
new_ptr->next = current;
prev_ptr ->next= new_ptr;
}
}
}
void SinglyLinkedList::addNodeAfter( char *searchdata, char * newdata)
{
Node * new_ptr, *current;
current = first_ptr;
bool found = false;
current= first_ptr;
while (current != NULL && found == false)
{
if (strcmp(current->data , searchdata)==0)
{ found = true;
}
else
{
current = current->next;
}
}
if (found == true)
{
new_ptr = new Node();
new_ptr->updateData( newdata);
new_ptr->next= current->next;
current->next = new_ptr;
}
}
void SinglyLinkedList::addNodeFirst( char * newdata)
{
Node * new_ptr;
new_ptr = new Node();
new_ptr->updateData( newdata);
new_ptr->next= first;
first = new_ptr;
}
void SinglyLinkedList::addNodeLast( char * newdata)
{
Node * new_ptr, current;
current= first;
while(current->next!=NULL)
current = current->next;
new_ptr = new Node();
new_ptr->updateData( newdata);
current->next= new_ptr;
}
void SinglyLinkedList:: deleteNode( char *searchdata)
{
Node *prev_ptr, *current;
current = first_ptr;
bool found = false;
while (current !=NULL && found == false)
{
if (strcmp(current->data ,searchdata)==0)
{
found = true;
}
else
{ prev_ptr= current;
current= current->next; }
}
if (found == true)
{
if (first_ptr == current)
{
first_ptr= current->next;
}
else
{
prev_ptr ->next= current->next;
}
delete current;
}
}
long SinglyLinkedList::getIndex( char *searchdata)
{
int index = 0;
Node *current_ptr;
current_ptr = first_ptr;
if (current_ptr == NULL)
return -1;
else
{
while (current_ptr != NULL)
{
if (strcmp(searchdata, current_ptr->data) == 0)
return index;
else
current_ptr = current_ptr->next;
index++;
}
}
return -1;
}
bool SinglyLinkedList::inList( char *searchdata)
{
Node *current_ptr;
current_ptr = first_ptr;
if (current_ptr == NULL)
return false;
else
{
while (current_ptr != NULL)
{
if (strcmp(searchdata, current_ptr->data) == 0)
return true;
else
current_ptr = current_ptr->next;
}
}
return false;
}
void SinglyLinkedList::pr intList()
{
Node *current_ptr;
current_ptr = first_ptr;
cout << "List Contents: " << endl;
if (current_ptr == NULL)
cout << " <EMPTY>" << endl;
else
{
while (current_ptr != NULL)
{
cout << " " << current_ptr->data << endl;
current_ptr = current_ptr->next;
}
}
}
int SinglyLinkedList::length()
{
Node *current_ptr;
int cnt;
current_ptr = first_ptr;
cnt = 0;
while (current_ptr != NULL)
{
cnt++;
current_ptr = current_ptr->next;
}
return cnt;
}
void SinglyLinkedList::reverse(){
if (first_ptr!=NULL) {
Node * current = first_ptr->next;
Node * newFirst=first_ptr;
newFirst->next= NULL;
Node * temp=first_ptr;
while (current!=NULL) {
temp = current->next;
current->next = newFirst;
newFirst = current;
current=temp;
}
first_ptr=newFirst;
}
}
void main()
{
SinglyLinkedList objSLL1;
char* strings[] = { "aa", "bb", "cc", "dd", "ff" };
int choice;
char* x;
do{
cout << endl
<< "|-------------------MAIN MENU-------------------|" << endl
<< "|Enter1 : To build linked list |" << endl
<< "|Enter2 : To print linked list |" << endl
<< "|Enter3 : To Display the length of linked list |" << endl
<< "|Enter4 : To Add to the first |" << endl
<< "|Enter5 : To Add to the last |" << endl
<< "|Enter6 : To Search in linked list |" << endl
<< "|Enter7 : To delete from linked list |" << endl
<< "|Enter8 : To Reverse linked list |" << endl
<< "|Enter9 : To Exit |" << endl
<< "|------------------MAIN MENU--------------------|" << endl;
cout<<"Enter Your Choice>>> ";
cin >> choice;
switch(choice)
{
case 1 :
cout << endl << "\t\t --<<<built linked list >>>--" << endl;
objSLL1.buildList( strings, 5 );
break;
case 2 :
cout << endl << "\t\t --<<<Print linked list >>>--" << endl;
objSLL1.printList();
break;
case 3 :
cout << endl << "\t\t --<<<length of linked list >>>--"
<< endl << "Length: " << objSLL1.length() << endl;
break;
case 4 :
cout << endl << "\t\t --<<<Add to the first of linked list >>>--" << endl
<< "Enter the item to add: ";
cin.getline(x,10);
objSLL1.addNodeFirst(x);
break;
case 5 :
cout << endl << "\t\t --<<<Add to the last of linked list >>>--" << endl
<< "Enter the item to add: ";
cin.getline(x,10);
objSLL1.addNodeLast(x);
break;
case 6 :
cout << endl << "\t\t --<<<Search in linked list >>>--" << endl
<< "Enter the item to search: ";
cin.getline(x,10);
objSLL1.addNodeLast(x);
break;
case 7 :
cout << endl << "\t\t --<<<Delete from linked list >>>--" << endl
<< "Enter the item to delete: ";
cin.getline(x,10);
objSLL1.deleteNode(x);
break;
case 8 :
cout << endl << "\t\t --<<<Reverse linked list >>>--" << endl;
objSLL1.reverse();
break;
case 9 :
cout << endl << "\t<<<<END OF PROGRAM>>>>" << endl;
break;
defualt:
cout << endl << "\t INVALID INPUT, TRY AGAIN (1-9) !" << endl;
}
}while(choice != 9 );
}
بارك الله فيكم جميعا
وجزاكم الله كل الخير



