الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

أمر بسيط في data structures

بدأه الأمل2011 في 23 أبريل 2011 · 3 رد · 584 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ورحمة الله وبركاته .....

لو سمحتم عندي هذا الكود باستخدام الـ stack ابغا أسأل ... كيف أخلي البيانات تطلع لي بنفس ترتيب دخولها ...

مثلا دخلت الأرقام [1 2 3 4 5] تطلع لي بنفس هالشكل [1 2 3 4 5] مو LIFO

كمان سؤال ثاني قبل نهاية البرنامج ... أبغى أخير المستخدم إذا يبغى يدخل أرقام من جديد أو لا

وإذا قال ايوا يعيد تنفيذ كل الخطوات من جديد ؟؟؟

هذي هي الأكواد على ثلاث ملفات ملف stackType.h

// Specification file
class StackType  {
public:
	StackType( );  
	void MakeEmpty( );
	bool IsEmpty( ) const;
	bool IsFull( ) const;
	void Push( int item );
	void Pop( int&  item );
	void DisplayStack();

private:
	int       top;
	int  items[10];
};

الملف الثاني ....

//The implementation file of the stack
#include<iostream>
#include"stackType.h"
using namespace std;
StackType::StackType()
{ 
 	top = -1;
}

void StackType::MakeEmpty( ) 
		//---------------------------------------------------
		// PRE:   None.
		// POST:  Stack is empty.
		//---------------------------------------------------
{
	top = -1;
}
bool StackType::IsEmpty( ) const
		//---------------------------------------------------
		// PRE:   Stack has been initialized.
		// POST:  Function value = (stack is empty)
		//---------------------------------------------------
{
	return ( top == -1 );
}

bool StackType::IsFull( ) const
		//---------------------------------------------------
		// PRE:   Stack has been initialized.
		// POST:  Function value = (stack is full)
		//---------------------------------------------------
{
	return  ( top == 9 );
}
void StackType::Push ( int newItem )
		//------------------------------------------------------
		// PRE:  Stack has been initialized and is not full.
		// POST: newItem is at the top of the stack.
		//------------------------------------------------------
{
	top++;
	items[top] = newItem;	
}
void StackType::Pop ( int&  item )
		//------------------------------------------------------
		// PRE:  Stack has been initialized and is not empty.
		// POST: Top element has been removed from stack.
		//	  item is a copy of removed element.
		//------------------------------------------------------
{
	item = items[top];
	top--;
}
void StackType::DisplayStack() {
	cout << "top -->";
	for ( int i = top; i >= 0; i--) 
		cout << "\t|\t" << items << "\t|" << endl;
	cout << "\t|---------------|" << endl;
}

الثالث Stack.cpp

#include<iostream>
#include"stackType.h"
using namespace std;
int main()
{
    StackType r;
	int size;
	cout<< "Enter the number of elements (less than 10) to push them into the stack: ";
	cin >> size;
	int temp=0;
	for(int i = 1; i<= size; i++){
		cout<< "Enter the element number " << i << " to push it into the stack: ";
	    cin >> temp;
        r.Push(temp);
        cout<<endl;
	}

	cout<<endl<< "Displying the elements in the stack: "<< endl;
	r.DisplayStack();
	int key;
	cout<<endl<< "pop the elements from the stack: "<<endl;
	while (!r.IsEmpty( )){
        r.Pop(key);
	    cout<< "      "<<key <<" is removed from stack."<< endl;
    }

  	cout<<endl<< "Push 7 and then 6 into the stack. ";
	r.Push(7);
    r.Push(6);
	cout<<endl<< "Displying the elements in the stack: "<< endl;
    r.DisplayStack();

	cout<<endl<< "Make the stack empty"<< endl;
	r.MakeEmpty();
	cout<<endl<< "Check whether the stack is empty or not"<< endl;
	if(r.IsEmpty())
		cout<<"Stack is Empty" << endl;
	else
		cout<<"Stack isn't Empty"<< endl;

	char ch = getchar();
	return 0;
}
#2

ما بقدر أساعدك بالكود لانه ما عندي البرنامج

لكن الفكرة هيك

الأول ،، اذا انت عامل الستاك على الاري فهذا سهل كثير

اعمل فور لوووب ببلش من count(عدد عناصر الستاك) و بالشرط اكبر او يساوري صفر و يكون متناقص

و اعمل طباعة للقيمة الي بالاندكس الخاص فيه

مثلا

for(int i=count ;i>=0 ; i--)

cout<<stack ;

و هيك رح يطلعو مرتبين ان شاء الله

بالنسبة للثانية

بتقدر تعمل جملة الدو وايل بحيث يكون جواتها كل العمل الخاص بالاضافة :lol:

#3

بدل محتويات الملف الثاني بالتالي:

//The implementation file of the stack
#include<iostream>
#include"stackType.h"
using namespace std;
StackType::StackType()
{ 
        top = 9;
}

void StackType::MakeEmpty( ) 
                //---------------------------------------------------
                // PRE:   None.
                // POST:  Stack is empty.
                //---------------------------------------------------
{
        top = 9;
}
bool StackType::IsEmpty( ) const
                //---------------------------------------------------
                // PRE:   Stack has been initialized.
                // POST:  Function value = (stack is empty)
                //---------------------------------------------------
{
        return ( top == 9 );
}

bool StackType::IsFull( ) const
                //---------------------------------------------------
                // PRE:   Stack has been initialized.
                // POST:  Function value = (stack is full)
                //---------------------------------------------------
{
        return  ( top == 0 );
}
void StackType::Push ( int newItem )
                //------------------------------------------------------
                // PRE:  Stack has been initialized and is not full.
                // POST: newItem is at the top of the stack.
                //------------------------------------------------------
{
        items[top] = newItem;   
        top--;
}
void StackType::Pop ( int&  item )
                //------------------------------------------------------
                // PRE:  Stack has been initialized and is not empty.
                // POST: Top element has been removed from stack.
                //        item is a copy of removed element.
                //------------------------------------------------------
{
        item = items[top];
        top++;
}
void StackType::DisplayStack() {
        cout << "top -->";
        for ( int i = top; i < 10; i--) 
                cout << "\t|\t" << items << "\t|" << endl;
        cout << "\t|---------------|" << endl;
}

و الله ولي التوفيق

1

مدونتي: C++ Tips and Tricks

#4

أو بيصير طريقة ثانية , غير عن طريقة أستاذي محمد علاء الدين

يمكن وضع مكدس أخر في الملف الثالث

بعد الحلقة الاولى ( حلقة الإدخال )

قم بوضع حلقة لعمل pop لعناصر المكدس الأول إلى push المكدس الثاني

و اطبع المكدس الثاني بدلاً من الأول

أتمنى أن تكون الفكرة واضحة

مواضيع مشابهة