الخميس، 31 ديسمبر 2009

linked list application

بسم الله الرحمن الرحيم
السلام عليكم ورحمة الله وبركاته
اليوم اقدم لكم برنامج بسيط جدا تطبيق علي linked list بناء علي طلب اصحابي سنه تانيه علشان امتحان العملي دي حاجه بسيطه انا عملتها اتمني الاستفاده البرنامج بيوضح العمليات التي يمكن تطبيقها علي ابسط انواع linked list وهي

عمليه الانشاء
عمليه الاضافه في اول القائمه
عمليه الاضافه في اخر القائمه
عمليه البحث
عمليه الترتيب
عمليه الحذف
سوف نقوم بعمل برنامج يطبق هذه العمليات البرنامج بسيط جدا عن الطلاب هندخل بيانات طالب او اكتر ونطبق العمليات السابقه عليه



class student
{
private:
char name [50];
int age;

public:

int id;
void enter_data()
{
cout<<" ** enter name ** ";
cin>>name;
cout<<" ** enter the age ** ";
cin>>age;
cout<<" ** enter the id ** ";
cin>>id;
cout<<" ******** //////****** ";

} //end function
void display_data()
{
cout<
cout<<"------------------ ";
cout<
cout<<" ------------------- ";
cout<
cout<<" ----------------- ";
} //end function

int print_id() //this function used to return student id
{
return id;

} //end function
};//end class

struct node
{
student data ; //data contain name,id ,age.
node *next; //pointer
};//end struct


class list
{
private:
node *head;
node *tail;
public:
list() //constructor
{
head=Null;
tail=Null;
} //end constructor

void insert_last(student d)
{
node *nn=new node;
nn->data=d;
if(head==Null)
head=nn;
else
tail->next=nn;
tail=nn;
nn->next=Null;
} //end function

void insert_first(student d) //add node at the first list
{
node *r=new node;
r->data=d;
if(head==Null)//list empty.
{
tail=r;
tail->next=Null;
}
else
r->next=head;
head=r;
}//end function

int search_id(int m) // function to search alinked list
{
int l=0;
node *temp=head;
while(( temp->data.print_id()!=m)&&(temp!=Null))
{
l++; //location
temp=temp->next;

}//end loop
if(temp->data.print_id()==m)
return l;
else
return -1;
}//end function

void sort()
{
node *temp1;
node *temp2;

for(temp1=head;temp1!=Null;temp1=temp1->next)
{

for(temp2=temp1->next;temp2!=Null;temp2=temp2->next)
{
if(temp1->data.id >temp2->data.id)
{
student t=temp1->data;
temp1->data=temp2->data;
temp2->data=t;
} //end if
} //endinner loop
} //end outer loop
} //end fun

void delete_student() //this function includes all types of delete in the linked list
{
node *temp=head;

int h;
node* temp1;
if(head==Null)
cout<<"empty list" <
else if
(h==temp->data.id)
{
head=head->next;
temp->next=Null;
delete temp; //delete the first item in the list
}
else if(h==tail->data.id)
{

while(temp->next!=tail)

temp=temp->next;
tail=temp;
temp=temp->next;
tail->next=Null;
delete temp; //delete the last irem in the list
}
else
{
while((temp->next!=Null) &&(h==temp->data.id))
temp1->next=temp->next;
delete temp; //delete any item betweem them
}//end else
} //end function


int countnode() //this function the number of nodes in the list
{
int count=0;
node *temp=head;
while(head!=Null)
{
count++;
temp=temp->next;
return count;
}
} //end function


void display() //this function to display all actions whitch i do
{
node *current;
current=head;
while(current!=Null)
{
current->data.display_data();
current=current->next;

} //end loop
} //end function
} //end class
void main()
{
int option;

student x[3];
student d;
list l;
int i,m,loc,h,w;
while(option!=0

{
cout<<" ------------------------------------------ ";
cout<<" 1.add new student at the linked list at last ";
cout<<" 2.add new student at the linked list at first ";
cout<<" 3.show all studentes data "<
cout<<" 4.search for student by id in any postion in the list ";
cout<<" 5.sort student by id ";
cout<<" 6.delete the student by id in any postion in the list ";
cout<<" 7.to know the number of nodes in the linked list ";
cout<<" 8. to exit t the program ";
cout<<" "<
cout<<" --------------------------------------- ";
cout<<" *** *** ** * welcom ********* *** ";
cout<<" enter your option ";

cin>>option;
switch(option)
{
case 1:
for(i=0;i<3;i++)
{
d.enter_data();l.insert_last(d);

} //end loop
break;
case 2:
for(i=0;i<3;i++)
{
d.enter_data();
l.insert_first(d);
}//end loop
break;
case 3:
cout<<" the students are ";
cout<<" ******////***** ";
l.display();
break;
case 4:
cout<<" enter the id ";
cin>>m;
loc=l.search_id(m);
if(loc==-1)
cout<<" the student not found ";
else
cout<<" the student is found at : ";
cout<<" ----------------------------- ";
break;
case 5:
cout<<" the sorted list is: ";
l.sort();
l.display();
break;
case 6:
cout<<"enter the id";
cin>>h;
l.delete_student();
l.display();
break;
case 7:
cout<<" the number of nodes in the list is ";
w=l.countnode();
cout<<<>
break;
case 8:
cout<< " the program ended ";
cout<<" "<
cout<<" & & & & & & GOOD LUCK & & & & & & & ";

break;
}//end loop
}//end switch
getch();
}//end main

ودي نتيجه تنفيذ البرنامج









واخيرا ابقوا عدلوا شكل البرنامج علشان اللخبطه اللي بتحصل اثناء الرفع
والسلام عليكم ورحمة الله وبركاته

ليست هناك تعليقات:

إرسال تعليق