程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> c語言的一結構數據的堆棧實現問題

c語言的一結構數據的堆棧實現問題

編輯:關於C語言

已經有編好的通用類型的類似堆棧程序,分別是Piles.h和Piles.c,這裡列出頭文件,主要是了解調用方法:
#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
} ExceptionPile;
// --------------------------------------
// constructeur :
// retourne une pile vide.
// exception :
//   PILE_IMPOSSIBLE_D_ALLOUER_MEMOIRE, l'allocation n'a pas fonctionner.
Pile creerPile( ExceptionPile * );
// --------------
// 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_UNITAIRES
// routine effectuant les tests unitaires.
// cette routine terminre normalement si les tests
// unitaires detectent une erreurs.
// sinon cette routine termine anormalement affichant
// l'erreur trouvee (assert).
void testerPiles();
#endif
#endif  
 
另外我建立了一結構數據Point,表示一座標軸上的點。
結構數據定義如下:
/*point.h*/
#ifndef _POINT_H
#define _POINT_H
//structure point pour presenter un point
typedef struct point{
  float x;
  float y;
} Point;
#endif
 
另外一主程序main.c用來測試堆棧程序:
#include <stdio.h>
#include <stdlib.h>
#include "point.h"
#include "Piles.h" int main(int argc, char * argv[])
{
 
  Pile p_nouveau;
  ExceptionPile 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;
}
 
在linux下gcc編譯通過,執行結果如下:
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)
 
問題:
在對整數進行堆棧運行時,測試正確,但對結構數據point進行出棧執行時,總是顯示最後進棧的數據,不知道問題出在什麼地方,希望大家能幫忙解決一下,不甚感激!
另外很多名詞是法語的,希望大家不要介意:
pile:也就是stack的意思
empiler:相當於push的意思
depiler:相當於pop的意思
element:相當於dada的意思
sommet: 相當於堆棧頂點的意思

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved