السلام عليكم انا كان عندي برنامجين في c++ كلهم عن ال linked list وكنت عايزه اجمعهم في برنامج واحد البرنامج عن الاسم والعمر فقط بس طلع عندي اخطاء كثيره المطلوب في البرنامج
*الاضافه من الامام والوسط والخير والحذف من البدايه والوسط والاخير والطباعه*
اتمنى حد يصحح ليا الاخطاء ويوضحهم ليا منشان الامتحان لازم افهم ايش هي الاخطاء ..
وشكرا ليكم سلفا..
هذا البرنامج:
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>
struct person {
char name[25];
int age;
struct person *Next_Person;
};
struct linkedlist {
struct person *head;
struct person *tail;
int nodeCount;
};
void initialize(linkedlist *);
void InsertFront(linkedlist *);
void InsertMid (linkedlist *):
void InsertRear(linkedlist *);
void DeleteRear(linkedlist *);
void DeleteMid(linkedlist *);
void PrintList(linkedlist *);
void getData(person *);
void main ()
{
linkedlist ll;
initialize(&ll);
int choice = 0;
do
{
cout << " 1: insert item in front" << endl;
cout << " 2: insert item in mid" << endl;
cout << " 3: Insert item in rear" << endl;
cout << " 4: Delete item from front" << endl;
cout << " 5: Delete item from mid" << endl;
cout << " 6: Delete item from rear" << endl;
cout << " 7: Print List" << endl;
cout << " 8: Exit" << endl;
cin >> choice;
if (choice == 1)
InsertFront(&ll);
if (choice == 2)
Insertmid(&ll);
if (choice == 3)
InsertRear(&ll);
if (choice == 4)
//DeleteFront(&ll);
if (choice == 5)
cout<<" ";
if (choice == 6)
cout<<" ";
if (choice == 7)
PrintList(&ll);
}while (choice != 8);
}
void initialize(linkedlist *ll)
{
ll->head = NULL;
ll->tail = NULL;
ll->nodeCount = 0;
}
void InsertFront(linkedlist *ll)
{
if (ll->head == NULL && ll->nodeCount == 0) //means empty list
{
person *p;
p = (person*) malloc(sizeof(person));
getData(p);
ll->head = p;
ll->tail = p;
ll->nodeCount++;
}
else
{
person *p;
p = (person*) malloc(sizeof(person));
getData(p);
p->Next_Person = ll->head ;
ll->head = p;
ll->nodeCount++; //increment counter
}
}
void InsertMid (linkedlist *ll)
{
node_ptr p,q;
p=first;
while(p->num!=m && p->next!=NULL)
{
p=p->next;
}
q=(node *)malloc(sizeof(node));
q->num=n;
strcpy(q->name,name);
q->next=p->next;
p->next =q;
}
void InsertRear(linkedlist *ll)
{
if (ll->head == NULL && ll->nodeCount == 0) //means empty list
{
person *p;
p = (person*) malloc(sizeof(person));
getData(p);
ll->head = p;
ll->tail = p;
ll->nodeCount++;
}
else
{
person *p;
p = (person*) malloc(sizeof(person));
getData(p);
p->Next_Person = NULL; //rear insertion... hence points to NULL.
ll->tail->Next_Person = p; //now point tail of second last element to last
ll->tail = p;//yes tail is now the new element inserted
ll->nodeCount++; //increment counter
}
void DelMid(linkedlist *ll)
{
node_ptr p,d;
p=q;
while(p->next->num!=m &&p->next->next!=NULL)
{
p=p->next;
}
d=p->next;
p->next=d->next;
delete d;
}
void DeleteRear(linkedlist *ll)
{
person *tempNext;
person *tempPrevious;
tempNext = ll->head ;
if (ll->nodeCount > 0 )
{ //we can use for loop with nodeCount or the following method
while (tempNext->Next_Person != NULL)
{
tempPrevious = tempNext;
tempNext = tempNext->Next_Person ;
}
tempPrevious->Next_Person = NULL;
free(tempNext);
ll->nodeCount --;
}
}
void PrintList(linkedlist *ll)
{
int i = 0;
struct person *tempNode;
tempNode = ll->head ;
cout << "The linked list is..." << endl;
for (i = 0; i < ll->nodeCount ; i++)
{
cout << tempNode->name << endl; ;
tempNode = tempNode->Next_Person ;
}
}
void getData(person *p)
{
cin >> p->name ;
cin >> p->age ;
p->Next_Person = NULL; //just to initialize
}
}