السلام عليكم ورحمة الله وبركاته
عندي برنامج C++ مطلوبه هو:
We need to create a linked list class (called linklist )
The individual data items, or links, are represented by structures of type link. Each structure contains an integer- representing the object’s single data item and a pointer to the next link.
The list itself stores a pointer to the link at the head of the list.
The class should contain functions that do the following:
1. Construct the list, leaving it empty.
2. Overload the constructor that permits to make the first node with a given data.
3. Determine whether the list is empty or not.
4. Find the size of the list.
5. Insert an entry at a specified position of the list.
6. Retrieve an entry at a specified position of the list.
7. Remove an entry from a specified position in the list.
8. Return a pointer to a node that contains the first occurrence of a specified item.
9. Destructor
Write a main function to test all the previous functions.
Write a function that permits to replace the contents of the node that contains a specified item with a given item.
Write a function that counts the number of occurrence for a specified item.
حسنا..البرنامج يمشي بشكل سليم عدا عند النقطة الاخيره اللي هي عباره عن حساب عدد وجود data معينه
حاولت بطريقتين الا ان النتيجة لاشي :(
حيث انه عندما اقوم بادخال ال data يتوقف المؤشر (المحاوله الاولى)
هذا هو البرنامج
#include<windows.h>
#include<string.h>
#include<conio.h>
#include<iostream>
using namespace std;
void print_menue();
void clrscr()
{
COORD coordScreen = { 0, 0 };
DWORD cCharsWritten;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConSize;
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten);
GetConsoleScreenBufferInfo(hConsole, &csbi);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten);
SetConsoleCursorPosition(hConsole, coordScreen);
}
void textcolor(int color)
{
SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
color <<4);
}
//------------------------------class linked list-------------------------------------
struct link
{
link *nextptr;
int data_item;
};
typedef link* linkptr;
class linkedlist
{
public:
static int count;
linkedlist();
linkedlist(int);
linkedlist(const linkedlist &);
~linkedlist(){delete head;}
int size_of_list();
bool isempty();
void insert();
bool retrieve(int &);
void remove();
linkptr f_accurance(linkptr,int);
private:
linkptr head;
};
int linkedlist::count=0;
void replac(linkedlist&);
void count(linkedlist&);
//----------------------------------constructor----------------------------------------
linkedlist::linkedlist()
{
head=NULL;
}
//--------------------------- ----copy constructor--------------------------------------
linkedlist::linkedlist(const linkedlist & op)
{
head=NULL;
}
//----------------------------overloading constroctore----------------------------------
linkedlist::linkedlist(int data)
{
head=new (link);
head->data_item=data;
head->nextptr=NULL;
}
//-----------------------------------list is empty--------------------------------------
bool linkedlist::isempty()
{
return (head==NULL?true:false);
}
//------------------------------------size of list--------------------------------------
int linkedlist::size_of_list()
{
int i=0;
linkptr temp=new(link);
temp=head;
while(temp!=NULL)
{
++i;
temp=temp->nextptr;
}
return i;
}
//--------------------------------------inserting------------------------------------------
void linkedlist::insert()
{
int data,index=0,i;
char ch;
linkptr newnode,curr,prev,temp,temp2;
newnode=new(link);
temp=new(link);
temp=head;
temp2=new(link);
cout<<" Enter the Data Item : ";
cin>>data;
if(isempty())
{
cout<<" List is emty right now,,in any case,,this is the first data item in the list .\n";
newnode->data_item=data;
newnode->nextptr=NULL;
head=newnode;
++count;
cout<<" Press Enter to continue.. : ";
getch();
return;
}
prev=NULL;
curr=head;
cout<<" Where do u want to insert ur data item ? !\n";
cout<<" In the begening , middel or in the end ? ! (B,M,E)\n";
cin>>ch;
switch(ch)
{
case 'B':
newnode->data_item=data;
newnode->nextptr=curr;
head=newnode;
curr=newnode;
++count;
while(curr!=NULL)
{
cout<<" -> ";
cout<<curr->data_item;
cout<<"\n";
curr=curr->nextptr;
}
cout<<" ->NULL\n ";
cout<<" Press enter to continue..";
getch();
break;
case 'M':
cout<<" Enter the index : ";
cin>>index;
--index;
if (index==0)
{
cout<<" Not in the meddele ,,,\n";
insert();
break;
}
if(index>count)
{
cout<<"u cann't enter a data item in this index,,plz choos another betwee 0 and "<< count;
insert();
}
for(i=0;i<index;++i)
{
prev=curr;
curr=curr->nextptr;
}
prev->nextptr=newnode;
newnode->data_item=data;
newnode->nextptr=curr;
++count;
while(temp!=NULL)
{
cout<<" -> ";
cout<<temp->data_item;
cout<<"\n";
temp=temp->nextptr;
}
cout<<" ->NULL\n ";
++count;
cout<<" Press enter to continue..";
getch();
break;
case 'E':
while(curr->nextptr!=NULL)
{
prev=curr;
curr=curr->nextptr;
}
curr->nextptr=newnode;
newnode->data_item=data;
newnode->nextptr=NULL;
temp2=head;
while(temp2!=NULL)
{
cout<<" -> ";
cout<<temp2->data_item;
cout<<"\n";
temp2=temp2->nextptr;
}
cout<<" ->NULL\n ";
++count;
cout<<" Press enter to continue..";
getch();
break;
default:
cout<<" invalied,,,,,try againe : \n";
insert();
break;
}
}
//---------------------------------------retrieve----------------------------------------
bool linkedlist::retrieve(int& data)
{
int index;
cout<<" Enter the index to rertrieve it's entry : ";
cin>>index;
--index;
if(isempty() || index>=count)
{
cout<<",,, list is empty or the index u entered is not exist\n";
cout<<" Press enter to continue..";
getch();
return false;
}
linkptr temp=new(link);
temp=head;
int i=0;
while(i!=index)
{
temp=temp->nextptr;
++i;
}
data=temp->data_item;
return true;
}
//--------------------------------------remove node--------------------------------------
void linkedlist::remove()
{
int index,i=0;
cout<<"Enter the index of the node that u want to delet it : ";
cin>>index;
--index;
if(isempty() || index>=count)
{
cout<<",,, the list is empty or the index u entered is not exist,,,\n";
cout<<" Press enter to continue..";
getch();
}
else
{
linkptr temp,curr,prev,temp2;
if(index==0)
{
temp=head;
head=head->nextptr;
delete(temp);
--count;
cout<<" ,,,Deleting compleet,,,\n";
cout<<"List after deleting item : \n";
temp2=head;
while(temp2!=NULL)
{
cout<<" -> ";
cout<<temp2->data_item;
cout<<"\n";
temp2=temp2->nextptr;
}
cout<<" ->NULL\n ";
cout<<" Press enter to continue..";
getch();
}
else
{
--index;
prev=head;
curr=head->nextptr;
while(i++<index)
{
prev=curr;
curr=curr->nextptr;
}
temp=curr;
prev->nextptr=curr->nextptr;
delete(temp);
--count;
cout<<" ,,,Deleting complete,,,\n";
cout<<"List after deleting item : \n";
temp2=head;
while(temp2!=NULL)
{
cout<<" -> ";
cout<<temp2->data_item;
cout<<"\n";
temp2=temp2->nextptr;
}
cout<<" ->NULL\n ";
cout<<" Press enter to continue..";
getch();
}
}
}
// ---------------------return the first accurance of specified item----------------------
linkptr linkedlist::f_accurance(linkptr ptr,int data)
{
if (isempty())
return ptr=NULL;
linkptr temp=head;
while(temp!=NULL && temp->data_item!=data)
temp=temp->nextptr;
if(temp==NULL)
ptr=NULL;
else
ptr=temp;
return ptr;
}
//---------------------------------------replace-----------------------------------------
void replac(linkedlist& op1)
{
int item,chane_item;
linkptr ptr=new(link);
cout<<" Enter the data to find the first accurance for it in the list : ";
cin>>item;
if(op1.f_accurance(ptr,item))
{
cout<<" Enter data to change already item:";
cin>>chane_item;
op1.f_accurance(ptr,item)->data_item=chane_item;
cout<<" changing complete,,,\n";
}
else
cout<<" Data not fond,,\n";
cout<<" Press Enter to continue \n";
getch();
}
//----------------------------------------count-------------------------------------------
void count(linkedlist& op)//is it wrong?
{
int data,cnt=0;
linkptr ptr=new(link);
cout<<" Enter the data : \n";
cin>>data;
if(op.f_accurance(ptr,data)==NULL)
cout<<" LIST IS EMTY\n";
else
{
ptr=op.f_accurance(ptr,data);
while(ptr!=NULL)
{
if(ptr->data_item==data)
++cnt;
}
}
cout<<" number of accurance of ur data item is : "<<cnt<<"\n";
cout<<" Press Enter to continue : ";
getch();
delete(ptr);
}
//---------------------------------------print_menue----------------------------------------
void print_menue()
{
static int i=1,j=16;
if(i==j)
{
j+=i;
++i;
}
textcolor(i++);
clrscr();
cout<<"\n\n WELCOME TO MY PROGRAMM\n";
cout<<"\n\n **********************************************************************\n";
cout<<" * Sellect one of the folowing choices to perform ur request : *\n";
cout<<" * 1-To insert an entry at specified position of the list. *\n";
cout<<" * 2-To retrieve an entry at specified position of the list. *\n";
cout<<" * 3-To remove an entry at specified position in the list. *\n";
cout<<" * 4-To return a pointer to the first accurance of specified item. *\n";
cout<<" * 5-To replace already data item. *\n";
cout<<" * 6-To find the size of the list. *\n";
cout<<" * 7-To count the number of accurance of spicifed item .*\n";
cout<<" * 8-To Exit. *\n";
cout<<" **********************************************************************\n";
cout<<" Enter ur choice : ";
}
//--------------------------------------main programm----------------------------------------
int main()
{
int choic,flag=0,data;
print_menue();
cin>>choic;
linkedlist op1;
linkptr ptr=new(link);
while(!flag)
{
switch(choic)
{
case 1:
op1.insert();
break;
case 2:
if(op1.retrieve(data))
{
cout<<" Data in that index is : "<<data<<endl;
cout<<" Press Enter to continue :";
getch();
}
break;
case 3:
op1.remove();
break;
case 4:
cout<<" Enter the data to find the first accurance for it in the list : ";
cin>>data;
if(ptr=op1.f_accurance(ptr,data))
{
cout<<" ,,data founed,,ptr->data=";
cout<<ptr->data_item;
}
else
cout<<" ,,data not founed,,ptr->data='?'";
cout<<" Press enter to continue..";
getch();
break;
case 5:
replac(op1);
break;
case 6:
cout<<" This is the size of the list : ";
cout<<op1.size_of_list();
cout<<"\n Press Enter to continue.. : \n";
getch();
break;
case 7:
count(op1);
break;
case 8:
clrscr();
cout<<"\n\n\n\n\n\n\n\n\t\t *********** \n";
cout<<"\t\t **The End** \n";
cout<<"\t\t *********** \n";
cout<<"\t\t ";
exit(0);
break;
default:
cout<<" Invaled entry,,try againe..\n";
cout<<" Press enter to continue: ";
getch();
print_menue();
cin>>choic;
break;
}
print_menue();
cin>>choic;
}
delete(ptr);
return 0;
}وال function في المحاولة الاخرى كالتالي حيث جعلتها friend ل class وهنا يقوم بايجاد عدد وجود ال data عندما تكون ال list تحوي فقط على نفس الرقم,,,,فيما عدا ذلك يتوقف المؤشر ايضا ؟؟؟
void count(linkedlist& op)
{
linkptr ptr;
ptr=op.head;
int data,cut=0;
if(ptr==NULL)
cout<<" LIST IS EMPTY\n";
else
{
cout<<" Enter the data item :";
cin>>data;
while(ptr!=NULL)
{
if(ptr->data_item==data)
{
ptr=ptr->nextptr;
++cut;
}
}
cout<<" number of accurance of this item is : "<<cut<<"\n";
}
cout<<" Press Enter to continue : ";
getch();
}