#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <string.h>
/*===========================*/
struct NODE
{
//NODE link; // self-referential poinetr
int data;
};
struct student
{
char *name;
int id;
int level;
int phone;
student *link;
student() // constructor
{
/*name = new char[150];
for(int i=0; i<150; i++)
*(name+i) = '\0';
id = 0;
level = 0;
phone = 0;*/
}
void displaystudent(void)
{
cout << " Name : "<< name << endl
<< " ID : "<< id << endl
<< " Level : "<< level << endl
<< " Number of Phone : "<< phone << endl
<< "---------------------------\n";
}
};
/*===========================*/
class List
{
private:
student *head;
int count;
void _insert(student v_in, student *pPre);
void _delete(student *pPre, student *pLoc, student *v_outPtr);
void _search(int target, student **pPre, student **pLoc);
public:
List(void);
~List(void);
void buildList();
int add(student v_in);
void remove(int id, student *v_outPtr);
void retrieve(int id,student& v_out);
void displayList(void);
int Count(void);
};
/*===========================*/
// constructor
List::List(void)
{
head = NULL;
count = 0;
}
/*===========================*/
// destructor
List::~List(void)
{
student *deletePtr;
if(head)
{
while( count > 0 )
{
deletePtr = head;
head = head->link;
count--;
delete deletePtr;
}
}
}
/*===========================*/
void List::buildList()
{
student vec;
student *pPre, *pLoc;
ifstream file;
file.open("student.txt");
while(! file.eof())
{
file.getline( vec.name , 150, '\n');
file >> vec.id;
file >> vec.level;
file >> vec.phone;
}
_search(vec.id,&pPre,&pLoc);
_insert(vec,pPre);
vec.name = new char[150];
}
/*===========================*/
// insertion private function
bool List::insert( student v_in, student *pPre)
{
student *pNew;
if((pNew =! new student))
return false; // failed insertion ( memory overflow )
*pNew = v_in;
pNew->link = NULL;
if( pPre == NULL )
{
pNew->link = head;
head = pNew;
}
else
{
pNew->link = pPre->link;
pPre->link = pNew;
}
count++;
return true; // success insertion
}
/*===========================*/
// searching private function
bool List::_search( int target, student **pPre, student **pLoc)
{
*pPre = NULL;
*pLoc = head;
while( (*pLoc != NULL) && (target> (*pLoc)->id) )
{
*pPre = *pLoc;
*pLoc = (*pLoc)->link;
}
if (*pLoc == NULL)
return false;
if(target < (*pLoc)->id)
return true;
return false;
}
/*===========================*/
void List::_delete(student *pPre, student *pLoc, student *v_outPtr)
{
*v_outPtr = *pLoc;
if(pPre == NULL)
head = pLoc->link;
else
pPre->link = pLoc->link;
count--;
delete pLoc;
}
/*===========================*/
int List::add( student v_in)
{
student *pPre, *pLoc;
if(_search(v_in.id, &pPre, &pLoc))
return 1;
if(! _insert(v_in, pPre))
return -1;
return 0;
}
/*===========================*/
bool List::remove( int target, student *v_outPtr)
{
student *pPre, *pLoc;
if( ! _search(target, &pPre, &pLoc))
return false;
_delete( pPre, pLoc, v_outPtr);
return true;
}
/*===========================*/
bool List::retrieve( int target, student &v_out)
{
student *pPre, *pLoc;
if (_search(target, &pPre, &pLoc))
{
v_out = *pLoc;
return true;
}
return false;
}
/*===========================*/
int List::Count()
{
return count;
}
/*===========================*/
void List::displayList()
{
student *pWalk = head;
student s;
s.displaystudent();
// l.buildList();
ifstream file;
file.open("student.txt");
if(Count() == 0)
cout<<"\n Sorry, the student list is empty!\n";
else
{
cout << "\n**** Begin student Print ****\n\n";
while(pWalk != NULL)
{
pWalk->displaystudent();
pWalk = pWalk->link;
}
cout << "\n**** End student Print ****\n\n";
}
}
/*===========================*/
int main(void)
{
char ch;
bool valid = true;
student vec;
int test;
List list;
ifstream file;
file.open( "student.txt");
if(file.fail())
cout<<"\nThe file: student.txt can NOT be opened! \n\n";
else
{
list.buildList();
file.close();
}
do
{
cout<< " student Menu\n"
<< " =====================\n"
<< " 1. Add a student\n"
<< " 2. Delete a student\n"
<< " 3. Retrieve a student\n"
<< " 4. Display all student\n"
<< " 5. Exit\n"
<< " Enter your choice: ";
cin >> ch;
if(ch == '1' || ch =='2' || ch =='3' || ch =='4')
switch(ch)
{
case '1':
system("cls");
cout<<"Enter the new student info: \nName of student: ";
cin.ignore();
cin.getline(vec.name, 150, '\n');
cout<<"ID: ";
cin>>vec.id;
cout<<"Level: ";
cin>>vec.level;
cout<<"phone: ";
cin>>vec.phone;
test = list.add( vec );
if( test == 0 )
cout<< "** You add new student succesfully **\n";
else
if( test == 1 )
cout <<" NOT added!\n"
<<" the student name you entered is already in the list\n\n";
else
cout<<" NOT added!\n"
<<"MEMORY OVERFLOW!\n\n";
break;
case '2':
system("cls");
cout<< "Enter the name of a student to delete it: ";
cin.ignore();
cin.getline( vec.name, 150, '\n');
if(list.remove( vec.id, &vec))
{
cout<< "The following student is deleted succesfully: \n";
vec.displaystudent();
}
else
cout<<" NO student with this name ! \n\n";
break;
case '3':
system("cls");
cout<< "Enter the name of a student to retrieve its info: ";
cin.ignore();
cin.getline(vec.name, 150, '\n');
if (list.retrieve(vec.id, vec))
vec.displaystudent();
else
cout<<" NO student with this name !\n\n";
break;
case '4':
system("cls");
list.displayList();
break;
}
else
if(ch != '5')
cout << " \n\a Invalid choice! Choices are <1,2,3,4,5>\n\n";
}while(ch != '5');
return 0;
}
مرحبا للجميع انا طالية في جامعة القدس المفتوحة فرع طولكرم في فلسطين طلب منا الاستاز عمل مشروع قوائم متصلة لمادة تركيب البيانات وتصميم الخوارزميات وانا عملت كود لادخال طلاب وحذف طلاب وبحث بس انا كان عندي خطا في الكود وياريت تشوفولي
الكود وتصححولي الغلط وبطلب كمان طلب انه ترسمولي القوائم المتصلة لهذا السؤال
