-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpiletable.c
More file actions
104 lines (74 loc) · 1.4 KB
/
piletable.c
File metadata and controls
104 lines (74 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include<stdio.h>
#include<stdlib.h> // pour malloc
// pile: last in first out
typedef struct
{
int *tab ;
int sommet ; //sommet indice du dernier element
int capacite;
}pile ;
//on a 5 fonction init , vide ,pleine, sommet , empiler , depiler
// intialisation d'une pile
void init ( pile * p , int c)
{
p->tab = (int*) malloc(c*sizeof(int));
p->capacite = c;
p->sommet = -1;
}
// vérification si la pile est vide
int vide(pile p )
{
return p.sommet == -1;
}
// verfication si la pile est pleine
int pleine (pile p )
{return p.sommet == p.capacite - 1;
}
// retourner la derniere valeur
int sommet(pile p)
{return p.tab[p.sommet];}
//empiler
void empiler(pile * p , int x)
{
p->tab[++p->sommet]=x;
}
//depiler
int depiler(pile * p )
{
int x=p->tab[p->sommet --];
return x;
}
// inverser un tableau a l'aide d'une pile
void Inversetab(int tab [] , int n )
{
int i;
pile p ;
init(&p , n ) ;
printf("avant : \n");
for (i =0 ; i<n ; i++ )
{
empiler(&p,tab[i]);
printf("%d \t",tab[i]);}
printf("\n aprés : \n");
for (i =0 ; i<n ; i++ )
{
tab[i]= depiler(&p);
printf("%d \t",tab[i]);}
}
/// programme principale
void main()
{
pile p ;
init(&p , 6);
printf("vide: %d \n",vide(p));
empiler(&p , 1);
empiler(&p , 2);
empiler(&p , 3);
empiler(&p , 4);
empiler(&p , 5);
empiler(&p , 6);
printf("pleine: %d \n",pleine(p));
printf("sommet: %d \n",sommet(p));
int tab[] = { 1, 2, 3 };
Inversetab(tab, 3 );
}