--------------------------------------------------------------------------------
بسم الله الرحمن الرحيم
الدكتور طلب منا برنامج وهو بحث وفي نفس الوقت هو اختبار الشهر الثاني وبذلت قصارا جهدي لكن للأسف لاجدوى :: وهذا هو المطلوب:
مادة تراكيب البيانات 202 حاسب
طور برنامجاً بلغة ++c يمكن المستدم من اجراْ العمليات التالية على القوائم المتصلة
1-انشاء القائمة
2-طباعة عناصر القائمة على الشاشة
3-حاساب عدد عناصر القائمة
4-اضافة عنصر في اول القائمة
5-اضافة عنصر فس اخر القائمة
6-البحث عن عنصر في القائمة
7-حذف عنصر من القائمة
8-قلب عناصر القائمة
9-فرز عناصر القائمة
تكون جميعها في برنامج واحد يقوم المستخدم بلأختيار من القائمة من واحد الى تسعة واجراء العمليات
#include <iostream.h>
#include <assert.h>
#include <string.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;
// Node *last_ptr; this is optional
public:
SinglyLinkedList();
~SinglyLinkedList();
void buildList( char* stringarray[], int arraylength);
void pr intList();
void deleteNode( char *searchdata);
void addNodeBe fore( 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 deleteAllOcc( char * searchdata);
};
SinglyLinkedList::SinglyLinkedList()
{
first_ptr = NULL;
}
SinglyLinkedList::~SinglyLinkedList()
{
Node *current_ptr, *next_ptr;
current_ptr = first_ptr;
// re**** 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:: 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;
}
//************************************************** ******
// write a function that reverse the list. The first element becomes the last one and so.
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;
}
}
//************************************************** ******
// write a function that delete all occurence of searchdata
void SinglyLinkedList::deleteAllOcc(char * searchdata) {
if(first_ptr != NULL) {
Node * temp = first_ptr;
Node * current= first_ptr;
Node * previous= first_ptr;
while(current!=NULL) {
if (strcmp(current->data,searchdata)==0) {
if (current== first_ptr)
{
first_ptr= first_ptr->next;
}
else {
temp = current;
previous->next=current->next;
delete temp;
}
current= previous->next;
}
else {current = current->next;
previous= previous->next;
}
}//while
} //else
}//if
void main()
{
SinglyLinkedList objSLL1;
char* strings[] = { "2", "3", "1", "0", "2" };
objSLL1.buildList( strings, 5 );
if (objSLL1.isEmpty())
cout << "Empty list.\n";
objSLL1.printList();
cout << "Length: " << objSLL1.length() << endl;
objSLL1.reverse();
objSLL1.printList();
objSLL1.deleteAllOcc("2");
objSLL1.printList();ذه جميع الدوال ولم يبقى عليك الا كتابة القائمة التي تظهر للمستخدم MENU ومنها يستطيع المستخدم تحديد ما يريد من العمليات او يختار ( رقم تحدده خارج التسعه للخروج من القائمه والبرنامج ككل ولا تلومني بأني لا استطيع عملها لأني طالب دبلوم ارجووووووووووووووووووووو المساعدة