السلام عليكم ورحمة الله
لدي هذه المسألة البرمجية وقد صعب علي حلها
حاولت القيام بالحل ولكن دائما هناك بعض الاخطاء
المسألة هي:
المطلوب كتابة صف عمومي buffer بحجم محدود يعتمد استراتيجية FIFO مع التوابع التالية:
add يضيف عنصر الى نهاية ال Buffer
remove تحذف وتعيد العنصر الموجود في نهاية ال buffer
conta تحدد اذا ما كان العنصر موجود في الbuffer او لا
max تعيد اكبر عنصر موجود في الbuffer
اختبر الصف buffer بعمل 4 buffers واحذ هي من وثائق Microsoft Office و ثاني هي سلاسل المحارف و ثالث هي أعاد
صحيحة و رابغ هي نقاط من الفضاء ثلاثي البعد
وهذا هو البرنامج الذي قمت بكتابته ولكن دائما يوجد خطأ لا اعرف كيف اقوم باصلاحه
#include <iostream>
using namespace std;
template<typename T>
class Node
{
//friend class List; // make List a friend
public:
Node( const T & ); // constructor
T getData() const; // return data in node
public:
T data; // data
Node *nextPtr; // next node in list
};// end class Node
//constructor
template<typename T>
Node<T>::Node( const T &info)
: data( info ), nextPtr( 0 )
{
// empty body
} // end Node constructor
// return copy of data in node
template<typename T>
T Node<T>::getData() const
{
return data;
} // end function getData
template<typename T>
class List
{
//friend class Node;
public:
List(); // constructor
//~List(); // destructor
void insertAtFront( const T& , Node<T> * );
bool removeFromBack( T &, Node<T> *,Node<T> * );
bool search(T, Node<T> * );
void max(Node<T> * );
bool isEmpty() const;
void print( Node<T> *) const;
private:
Node<T> *firstPtr; // pointer to first node
Node<T> *lastPtr; // pointer to last node
//utility function to allocate new node
Node<T> *getNewNode( const int & );
}; // end class List
//default constructor
template<typename T>
List<T>::List ()
: firstPtr( 0 ), lastPtr( 0 )
{
// empty body
} // end List constructor
// destructor
//template<typename T>
/*List<T>::~List()
{
if ( !isEmpty() ) // List is not empty
{
cout << "Destroying nodes ...\n";
Node<T> *currentPtr = firstPtr;
Node<T> *tempPtr;
while ( currentPtr != 0 ) // delete remaining nodes
{
tempPtr = currentPtr;
cout<< tempPtr->data << '\n';
currentPtr = currentPtr->nextPtr;
delete tempPtr;
}//end while
}// end if
cout << "All nodes destroyed\n\n";
}// end List destructor
// insert node at front of list*/
template<typename T>
void List<T>::insertAtFront( const T &value, Node<T> *newPtr)
{
newPtr = getNewNode( value ); // new node
if ( isEmpty() ) // List is empty
firstPtr = lastPtr = newPtr; // new list has only one node
else // List is not empty
{
newPtr->nextPtr = firstPtr; // point new node to previous1st node
firstPtr = newPtr; // aim firstPtr at new node
} //end else
}// end function insertAtFront
// delete node from back of list
template<typename T>
bool List<T>::removeFromBack( T &value, Node<T> *tempPtr , Node<T> *currentPtr)
{
if ( isEmpty() ) // List is empty
return false; // delete unsuccessful
else
{
tempPtr = lastPtr; // hold tempPtr to delete
if ( firstPtr == lastPtr ) // List has one element
firstPtr = lastPtr = 0; // no nodes remain after removal
else
{
currentPtr = firstPtr;
// locate second-to-last element
while ( currentPtr->nextPtr != lastPtr )
currentPtr = currentPtr->nextPtr; // move to next node
lastPtr = currentPtr; // remove last node
currentPtr->nextPtr = 0; // this is now the last node
}// end else
value = tempPtr->data; // return value from old last node
delete tempPtr; // reclaim former last node
return true; // delete successful
}// end else
}//end function removeFromBack
// is List empty?
template<typename T>
bool List<T>::isEmpty() const
{
return firstPtr == 0;
}//end function isEmpty
//return pointer to newly allocated node
/*Node *List::getNewNode( const int &value )
{
return new Node( value);
}//end function getNewNode
// display contents of List*/
template<typename T>
bool List<T>::search( T x, Node<T> *p )
{
if (firstPtr == 0) return false;
p = firstPtr;
bool found = false;
while ((p) && (!found))
if (p->data == x)
found = true;
else
p = p->nextPtr;
return found;
}
template<typename T>
void List<T>::max( Node<T> *p )
{
p = firstPtr;
int max = p->data;
while (p->nextPtr != lastPtr){
if (p->data > max) max = p->data;
p = p->nextPtr;}
cout<<max;
}
template<typename T>
void List<T>::print(Node<T> *currentPtr) const
{
if ( isEmpty() ) // List is empty
{
cout << "The list is empty\n\n";
return;
}//end if
currentPtr = firstPtr;
cout << "The list is:";
while ( currentPtr != 0 ) // get element data
{
cout << currentPtr->data <<' ';
currentPtr = currentPtr->nextPtr;
}// end while
cout << "\n\n";
}//end function pri
#include <string>
// List class definition
using namespace std;
// function to test a List
int main()
{
Node<int> *p;
Node<int> *p1;
List<int> listObject;
cout << "Enter one of the following:\n"
<<" 1 to insert at beginning of list\n "
<<" 4 to delete from end of list\n "
<<" 5 to end list processing\n";
// test List of int values
cout << "Testing a List \n";
// display instructions
int choice; // store user choice
int value; // store input value
do // perform user-selected actions
{
//Cout<<"? ";
cin >> choice;
switch ( choice )
{
case 1: // insert at beginning
cout << "Enter an integer: ";
cin >> value;
listObject.insertAtFront( value,p );
listObject.print(p);
break;
case 2: // insert at beginning
cout << "max: ";
listObject.max( p );
break;
case 4: // remove from end
if ( listObject.removeFromBack( value,p,p1 ))
cout << value << " removed from list\n";
listObject.print(p);
break;
} //end switch
} while ( choice != 5 ); // end do...while
cout << "End list test\n\n";
return 0;
}// end main
