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

[ تمت الإجابة ]المكدس

مغلق
بدأه بسام العبسي في 9 يوليو 2012 · 6 رد · 605 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

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

#include <iostream>
using namespace std ;

const int size = 10 ;


//prototypes
int IsFull(struct stack s) ;
int IsEmpty(struct stack s) ;

void push(stack &s, int c) ;
int pop(stack &s) ;

void inv(stack &s) ;
void print(stack s) ;


//struct defenation
struct stack{
int top ;
int iteam[size] ;
};

int main ()
{

    cout << "\n\n\t-------------------------------------------" ;
    cout << "\n\t|                                          |" ;
    cout << "\n\t|                                          |" ;
    cout << "\n\t|     This program for REVERSE the         |" ;
    cout << "\n\t|            Elements of stack             |" ;
    cout << "\n\t|                                          |" ;
    cout << "\n\t|                                          |" ;
    cout << "\n\t|                                          |" ;
    cout << "\n\t--------------------------------------------\n\n\t" ;



    stack s ;
    s.top = -1 ;

    int x ;

    cout << "Enter the Value of the orginal stack : \n\n\t" ;

    for (int i = 0 ; i < size  ; i++)
    {
         cin >> x ;
         push (s, x) ;
    }

    inv(s) ;
    print(s) ;

        return 0 ;
}

int IsFull(stack s)
{
    return (s.top == size - 1) ;
}

int IsEmpty(stack s)
{
    return (s.top == -1) ;
}

void push (stack &s, int c)
{
    s.iteam[++s.top] = c ;
}

int pop(stack &s)
{
    return (s.iteam[s.top--]) ;
}

void inv(stack &s)
{
    stack s1, s2,  ;
    s1.top = -1 ;
    s2.top = -1 ;

    while(!IsEmpty(s))
         push(s1, pop(s)) ;

    while(!IsEmpty(s1))
         push(s2, pop(s1)) ;

    while(!IsEmpty(s2))
         push(s, pop(s2)) ;
}

void print(stack s)
{
    cout << "\n\n\tThe shpae of stack after REVERSE  the orginal stack : \n\n\t" ;
    while(!IsEmpty(s))
    cout << pop(s)<< " " ;
    cout << "\n\n" ;
}

البرنامج لايعكس المكدس الاصلي رغم اني راجعت الخوارزمية اكثر من مره وتأكدت من فعاليتها

عندما اقوم بتعديل هذه الدالة الى الشكل التالي

void inv(stack &s)
{
    stack s1, s2, s3 ;
    s1.top = -1 ;
    s2.top = -1 ;
    s3.top = -1 ;

    while(!IsEmpty(s))
         push(s1, pop(s)) ;

    while(!IsEmpty(s1))
         push(s2, pop(s1)) ;

    while(!IsEmpty(s2))
         push(s3, pop(s2)) ;

    while(!IsEmpty(s3))
         push(s, pop(s3));
}

يطبع عكس المكدس

ارجوا ايضاح خطأي

وشكراً مقدماً

تم تعديل هذه المشاركة بواسطة بسام العبسي في 10 يوليو 2012 في 00:06

#2

عدل الوظيفة لتصبح :

void inv(stack &s)
{
    int first   = s.iteam[0];
    int last    = s.iteam[s.top];

    s.iteam[0]      = last;
    s.iteam[s.top]  = first;

    int i;
    for(i = 1 ; i < s.top ; i++)
        s.iteam[s.top - i] = s.iteam;
}

        -------------------------------------------
        |                                          |
        |                                          |
        |     This program for REVERSE the         |
        |            Elements of stack             |
        |                                          |
        |                                          |
        |                                          |
        --------------------------------------------

        Enter the Value of the orginal stack :

        1 2 3 4 5


        The shpae of stack after REVERSE  the orginal stack :

        1 2 3 2 5

---

كتبت أمس خورزمية للمكدس وأضفت عليها مثل هذه الوظيفة, لعل أحد يستفيد منه :

#include<stdio.h>
#include<stdlib.h>

typedef struct _StackObject {
    int stack_top;
    int *stack;
} StackObject;

int stack_init(StackObject *object, int size);
int stack_destroy(StackObject *object);
void stack_push(StackObject *object, int value);
int stack_pop(StackObject *object);
void stack_inverse(StackObject *object);

int main(int argc, char **argv) {

    StackObject stack;
    stack_init(&stack, 10);

    stack_push(&stack, 11);
    stack_push(&stack, 22);
    stack_push(&stack, 33);

    printf("1- %d\n", stack_pop(&stack));
    printf("2- %d\n", stack_pop(&stack));
    printf("3- %d\n", stack_pop(&stack));


    stack_push(&stack, 11);
    stack_push(&stack, 22);
    stack_push(&stack, 33);

    stack_inverse(&stack);

    printf("1- %d\n", stack_pop(&stack));
    printf("2- %d\n", stack_pop(&stack));
    printf("3- %d\n", stack_pop(&stack));

    stack_destroy(&stack);
    return(0);
}



int stack_init(StackObject *object, int size) {
    object->stack_top   = -1;
    object->stack       = (int *) malloc(sizeof(int) * size);

    if(object->stack == NULL)
        return(-1);

    return(0);
}

int stack_destroy(StackObject *object) {
    free(object->stack);
    return(0);
}

void stack_push(StackObject *object, int value) {
    object->stack_top   = object->stack_top++;
    object->stack[object->stack_top]    = value;
}


int stack_pop(StackObject *object) {
    int temp    = object->stack[object->stack_top];
    object->stack_top   = object->stack_top--;
    return(temp);
}


void stack_inverse(StackObject *object) {
    int first   = object->stack[0];
    int last    = object->stack[object->stack_top];

    object->stack[0]                    = last;
    object->stack[object->stack_top]    = first;

    int i;
    for(i = 1 ; i < object->stack_top ; i++)
        object->stack[object->stack_top - i] = object->stack;
}

تم تعديل هذه المشاركة بواسطة Mr.B في 10 يوليو 2012 في 00:43

2
#3

اخي العزيز شكراً لك لكن مااردته حقاً هو كالتالي

  -------------------------------------------
        |                                          |
        |                                          |
        |     This program for REVERSE the         |
        |            Elements of stack             |
        |                                          |
        |                                          |
        |                                          |
        --------------------------------------------

        Enter the Value of the orginal stack :

        1 2 3 4 5


        The shpae of stack after REVERSE  the orginal stack :

        5 4 3 2 1

ريد انا عكس المكدس

#4

لاتعكس المكدس وسيكون كما أردت. إذا أدخلت في المكدس :

123

عن طريق push فسيكون في المكدس :

+---+
| 3 |
+---+
| 2 |
+---+
| 1 |
+---+

وإذا أردت إستخراجها من المكدس وطباعتها فستظهر :

321

كما ترى, آخر من دخل هو أول من يخرج. عكس المكدس يعني إظهار البيانات بنفس الصورة التي أدخلت فيها لأنه أصلاً معكوس (معكوس معكوس الصواب = صواب).

#5

فهمت انا كلامك بس انا اريد ان احول قيمة المكدس من

+---+
| 3 |
+---+
| 2 |
+---+
| 1 |
+---+

الى

+---+
| 1 |
+---+
| 2 |
+---+
| 3 |
+---+

اتمنى ان تكون فهمت مقصدي الان

#6

أخي شغل هذا البرنامج وستفهم ما أقصده :

#include <iostream>
using namespace std ;

const int size = 10 ;


//prototypes
int IsFull(struct stack s) ;
int IsEmpty(struct stack s) ;

void push(stack &s, int c) ;
int pop(stack &s) ;

void inv(stack &s) ;
void print(stack s) ;


//struct defenation
struct stack{
int top ;
int iteam[size] ;
};

int main ()
{


    stack s ;
    s.top = -1 ;

    cout << "Before :" << endl;

    push(s, 11);
    push(s, 22);
    push(s, 33);

    cout << pop(s) << endl;
    cout << pop(s) << endl;
    cout << pop(s) << endl;

    cout << "After :" << endl;

    push(s, 11);
    push(s, 22);
    push(s, 33);

    inv(s);

    cout << pop(s) << endl;
    cout << pop(s) << endl;
    cout << pop(s) << endl;

    return 0 ;
}

int IsFull(stack &s)
{
    return (s.top == size - 1);
}

int IsEmpty(stack s)
{
    return (s.top == -1) ;
}

void push (stack &s, int c)
{
    s.iteam[++s.top] = c ;
}

int pop(stack &s)
{
    return (s.iteam[s.top--]) ;
}

void inv(stack &s)
{
    int first   = s.iteam[0];
    int last    = s.iteam[s.top];

    s.iteam[0]      = last;
    s.iteam[s.top]  = first;

    int i;
    for(i = 1 ; i < s.top ; i++)
        s.iteam[s.top - i] = s.iteam;
}

void print(stack s)
{
    cout << "\n\n\tThe shpae of stack after REVERSE  the orginal stack : \n\n\t" ;
    while(!IsEmpty(s))
    cout << pop(s)<< " " ;
    cout << "\n\n" ;
}

أدخلنا :

11
22
33

المخرجات قبل عكس المكدس :

33
22
11

بعد عكسه:

11
22
33

لاحظ أنه بعد العكس ظهرت كما أدخلناها.

#7

شكراً اخي فهمت مقصدك الان

وعفواً على ازعاحك

هذا الموضوع مغلق.

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…