博客统计信息

用户名:marcel
文章数:5
评论数:10
访问量:9361
无忧币:20
博客积分:526
博客等级:2
注册日期:2007-05-21

我的技术圈(0)

更多>>
c语言的一结构数据的堆栈实现问题
2007-07-02 21:10:15
已经有编好的通用类型的类似堆栈程序,分别是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: 相当于堆栈顶点的意思
分享至
更多
一键收藏,随时查看,分享好友!
0人
了这篇文章
类别:未分类┆技术圈()┆阅读()┆评论() ┆ 推送到技术圈返回首页

文章评论

 
2007-07-02 21:14:16
刚才想编辑一下的,没想到又点到删除了,真是够郁闷的。
想提个建议,删除键能否添加个确认对话框,这样就可以避免误操作了。

2007-07-02 21:40:59
是该加个确认对话框来着,有些不方便。

point 格式没有错吧?坐下来慢慢看

2007-07-02 23:15:32
抱歉这个小隐患给大家添麻烦了~
我会把它加入下期的改进计划的!

2007-07-04 07:05:21
已经找到解决办法,主要是我用malloc的时候没有用在循环里,下面是改正的程序:
主程序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;

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 = malloc(sizeof *p1);
    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 (2.000000, 2.000000)
Depiler le point (2.000000, 2.000000)
Depiler le point (1.000000, 1.000000)

2007-07-06 11:08:00
果然学海无涯!

 

发表评论            

【技术门诊】专家解析:软考重点难点及应试技巧
昵  称:
登录  快速注册
验证码:

请点击后输入验证码博客过2级,无需填写验证码

内  容: