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

أمر في data structures

بدأه الأمل2011 في 23 أبريل 2011 · 1 رد · 1,307 مشاهدة · في C++.Net
مشاركة: واتساب 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;
}

تم تعديل هذه المشاركة بواسطة الأمل2011 في 23 أبريل 2011 في 21:48

#2

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

اختي اذا كنتي تريدين الارقام تطلع زي ما ادخلتيهن فاستخدمي ال Queue وليس ال Stack لأنه كذا طريقة عمله LIFO ... اما بالنسبه لإضافه ارقام اخرى ممكن بس افضل ان تقومي بتغيير المصفوفه الى LinkedList لتكون مرنه وتضيفي عناصر اثناء زمن التشغيل

.:: Wish to become better and better ::.

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