Stack implementation of structured data in C Language

Source: Internet
Author: User

There are compiled general type similar stack programs, namely Piles. h and Piles. c. The header files are listed here, mainly to understand the call method:
# Ifndef PILE_H
# Define PILE_H
# Include <stdbool. h>
Typedef struct _ pile * Pile;
Typedef enum {
PILE_PAS_D_EXCEPTION,
PILE_VIDE,
PILE_IMPOSSIBLE_D_ALLOUER_MEMOIRE
} Predictionpile;
//--------------------------------------
// Constructeur:
// Retourne une pile vide.
// Exception:
// PILE_IMPOSSIBLE_D_ALLOUER_MEMOIRE, l 'allocation n' a pas fonctionner.
Pile creerPile (predictionpile *);
//--------------
// Manipulateur:
// Ajouter un element sur le sommet de la pile.
// Exception:
// PILE_IMPOSSIBLE_D_ALLOUER_MEMOIRE, l 'allocation n' a pas fonctionner.
Void empiler (Pile, void * element, ExceptionPile *);
// Enleve le sommet de la pile.
// Exception:
// PILE_VIDE, depiler a ete appele sur une pile vide.
Void depiler (Pile, ExceptionPile *);
//-------------
// Observateur:
// Lire le sommet de la pile.
// Exception:
// PILE_VIDE, sommet a ete appeler sur une pile vide.
Void * sommet (Pile, ExceptionPile *);
// Retourne true si la pile est vide.
Bool estVide (Pile); # ifdef tests_unitmetadata
// Routine ubuntuant les tests unit.pdf.
// Cette routine terminre normalement si les tests
// Unitreceivdetectent une erreurs.
// Sinon cette routine termine anormalement affichant
// L 'erreur trouvee (assert ).
Void testerPiles ();
# Endif
# Endif
 
In addition, I have created a structural data Point to represent the points on a coordinate axis.
The structure data is defined as follows:
/* Point. h */
# Ifndef _ POINT_H
# Define _ POINT_H
// Structure point pour presenter un point
Typedef struct point {
Float x;
Float y;
} Point;
# Endif
 
Another main program main. c is used to test the stack program:
# Include <stdio. h>
# Include <stdlib. h>
# Include "point. h"
# Include "Piles. h" int main (int argc, char * argv [])
{
 
Pile p_nouveau;
Predictionpile exp = 0;
Int I, j;
Int * element;
Point * p1 = malloc (sizeof * p1 );

If (argc = 1)
{


For (I = 0; I <4; I ++ ){

Element = (int *) I;
Printf ("Empiler l' element: % d \ n", element );
Empiler (p_nouveau, element, & exp );

}

For (I = 0; I <4; I ++)
{
Element = sommet (p_nouveau, & exp );
Printf ("Depiler l 'element est: % d \ n", element );
Depiler (p_nouveau, & exp );}

For (I = 0; I <4; I ++ ){

P1-> x = (float) I;
P1-> y = (float) I;
Printf ("Empiler le point (% f, % f) \ n", p1-> x, p1-> y );
Empiler (p_nouveau, p1, & exp );

}

For (I = 0; I <4; I ++)
{
P1 = sommet (p_nouveau, & exp );
Printf ("Depiler le point (% f, % f) \ n", p1-> x, p1-> y );
Depiler (p_nouveau, & exp );

}


}

Return 0;
}
 
In linux, the gcc is compiled and the execution result is as follows:
Empiler l 'element: 0
Empiler l 'element: 1
Empiler l 'element: 2
Empiler l 'element: 3
Depiler l 'element est: 3
Depiler l 'element est: 2
Depiler l 'element est: 1
Depiler l 'element est: 0
Empiler le point (0.000000, 0.000000)
Empiler le point (1.000000, 1.000000)
Empiler le point (2.000000, 2.000000)
Empiler le point (3.000000, 3.000000)
Depiler le point (3.000000, 3.000000)
Depiler le point (3.000000, 3.000000)
Depiler le point (3.000000, 3.000000)
Depiler le point (3.000000, 3.000000)
 
Problem:
The test is correct when the integer is run on the stack. However, when the structural data point is run out of the stack, the data that is last written into the stack is always displayed. I don't know where the problem is, I hope you can help us solve this problem. Thank you very much!
In addition, many terms are in French. I hope you don't mind:
Pile: that is, the meaning of stack.
Empiler: equivalent to push
Depiler: equivalent to pop
Element: equivalent to dada
Sommet: equivalent to the meaning of stack vertices

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.