.

Définitions

.

Les structures de pile et de file permettent toutes les deux de stocker des ensembles d'objets et fournissent des opérations permettant d'ajouter ou de retirer des objets un à un. Les éléments retirés ne sont cependant, ni dans une structure ni dans l'autre, retirés dans un ordre arbitraire, et chacune de ces structures à sa règle.

Pile : Dans une pile (en anglais stack), chaque opération de retrait retire l'élément arrivé le plus récemment. Nommée dernier entré, premier sorti (DEPS), ou plus couramment en anglais LIFO (last in, first out).

File : Dans une structure de file (en anglais queue), chaque opération de retrait retire l'élément qui avait été ajouté le premier. On ne peut accéder qu’a un objet, celui qui est situé au début de la file.

L'objectif est de construire l'application ci-dessous, on peut télécharger l'application

Télécharger pileetfile téléchargement pour Utiliser l'application sur windows .

_images/pilefile.PNG

Vidéo

html5 video converter by EasyHtml5Video.com v4.0

.

Pile

.

html5 video loop by EasyHtml5Video.com v4.0
from tkinter import * from random import * num =[ '❶' , '❷' , '❸' , '❹' , '❺' , '❻' , '❼' , '❽' , '❾' , '❿' , '⓫' , '⓬' , '⓭' , '⓮' , '⓯' , '⓰' , '⓱' , '⓲' , '⓳' , '⓴' ] col =[ 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' ] def empiler ( Pile , valeur ): global donnees Pile , item1 , item11 = donnees [ 0 ], donnees [ 1 ], donnees [ 2 ] fin = len ( Pile ) if fin <= 13 : k = randrange ( 0 , valeur ) Pile . append ( num [ k ]) donnees =[ Pile , item1 , item11 ] affiche ( can , 1 ) def deplier ( can ): global donnees Pile , item1 , item11 = donnees [ 0 ], donnees [ 1 ], donnees [ 2 ] longueur = len ( Pile ) if longueur > 0 : del Pile [ longueur - 1 ] fin = len ( Pile ) donnees =[ Pile , item1 , item11 ] affiche ( can , 2 ) def affiche ( can , j ): global donnees Pile , item1 , item11 = donnees [ 0 ], donnees [ 1 ], donnees [ 2 ] longueur = len ( Pile ) couleur = col [ longueur ] if j == 1 : can . itemconfigure ( item12 , fill = 'gold' ) can . itemconfigure ( item12 , text = "Etat de la Pile\nEMPILER" ) for i in range ( 12 ): can . itemconfigure ( item1 [ i ], text = ' ' ) for i in range ( len ( Pile )): texte = Pile [ i ] can . itemconfigure ( item1 [ i ], text = texte ) fin = len ( Pile ) donnees =[ Pile , item1 , item11 ] if j == 2 : can . itemconfigure ( item12 , fill = 'red' ) can . itemconfigure ( item12 , text = "Etat de la Pile:\nDEPILER" ) for i in range ( len ( Pile )+ 1 ): can . itemconfigure ( item1 [ i ], text = ' ' ) for i in range ( len ( Pile )): texte = Pile [ i ] can . itemconfigure ( item1 [ i ], text = texte ) fin = len ( Pile ) donnees =[ Pile , item1 , item11 ,] fenetre = Tk () Pile =[] compte1 =[] compte2 =[] debut = 0 fin = 0 faire = 0 can = Canvas ( fenetre , height = 1000 , width = 400 , bg = 'blue' ) item11 = can . create_text ( 40 , 150 , text = "" , font = "verdana 25 bold" , fill = 'light green' ) item2 =[] item1 =[] fenetre . geometry ( "550x1000" ) fenetre . configure ( bg = 'light blue' ) item12 = can . create_text ( 200 , 40 , text = ' ' , font = 'verdana 25 bold' , fill = col [ 1 ]) Pile =[] for i in range ( 13 ,- 1 ,- 1 ): can . create_rectangle ( 150 , 90 + i * 60 , 210 , 150 + i * 60 , fill = 'white' , outline = 'red' ) de = can . create_text ( 175 , 120 + i * 60 , text = ' ' , font = 'verdana 25 bold' , fill = col [ i ]) item1 . append ( de ) donnees =[ Pile , item1 , item11 ] item21 = Button ( fenetre , text = "Piles\nempile" , font = 'verdana 20 bold' , fg = 'red' , command = lambda : empiler ( can , 15 )) can . grid ( row = 0 , column = 0 , rowspan = 2 ) item21 . grid ( row = 0 , column = 1 ) item21 = Button ( fenetre , text = "Piles\ndépile" , font = 'verdana 20 bold' , fg = 'red' , command = lambda : deplier ( can )) item21 . grid ( row = 1 , column = 1 ) fenetre . mainloop ()

.

File

.

html5 video loop by EasyHtml5Video.com v4.0
from tkinter import * from random import * num =[ '❶' , '❷' , '❸' , '❹' , '❺' , '❻' , '❼' , '❽' , '❾' , '❿' , '⓫' , '⓬' , '⓭' , '⓮' , '⓯' , '⓰' , '⓱' , '⓲' , '⓳' , '⓴' ] col =[ 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' ] def enfiler ( File , valeur ): global donnees File , item1 , item11 = donnees [ 0 ], donnees [ 1 ], donnees [ 2 ] k = randrange ( 0 , valeur ) File . append ( num [ k ]) fin = len ( File ) donnees =[ File , item1 , item11 ] affiche ( can , 1 ) def defiler ( can ): global donnees File , item1 , item11 = donnees [ 0 ], donnees [ 1 ], donnees [ 2 ] longueur = len ( File ) if longueur > 0 : for i in range ( longueur - 1 ): File [ i ]= File [ i + 1 ] del File [ longueur - 1 ] fin = len ( File ) donnees =[ File , item1 , item11 , item12 , compte1 , compte2 , debut , fin ] affiche ( can , 2 ) def affiche ( can , j ): global donnees File , item1 , item11 = donnees [ 0 ], donnees [ 1 ], donnees [ 2 ] longueur = len ( File ) couleur = col [ longueur ] if j == 1 : can . itemconfigure ( item12 , fill = 'gold' ) can . itemconfigure ( item12 , text = "Etat de la file:\nEnfiler\n" ) for i in range ( 15 ): can . itemconfigure ( item1 [ i ], text = ' ' ) for i in range ( len ( File )): texte = File [ i ] can . itemconfigure ( item1 [ i ], text = texte ) fin = len ( File ) donnees =[ File , item1 , item11 ] if j == 2 : can . itemconfigure ( item12 , fill = 'red' ) can . itemconfigure ( item12 , text = "Etat de la file:\nDéfiler\n" ) for i in range ( 15 ): can . itemconfigure ( item1 [ i ], text = ' ' ) for i in range ( len ( File )): texte = File [ i ] can . itemconfigure ( item1 [ i ], text = texte ) fin = len ( File ) donnees =[ File , item1 , item11 ] fenetre = Tk () File =[] compte1 =[] compte2 =[] debut = 0 fin = 0 faire = 0 can = Canvas ( fenetre , height = 500 , width = 1000 , bg = 'blue' ) item11 = can . create_text ( 600 , 150 , text = "" , font = "verdana 25 bold" , fill = 'light green' ) item2 =[] item1 =[] fenetre . geometry ( "1000x550" ) fenetre . configure ( bg = 'light blue' ) item12 = can . create_text ( 600 , 80 , text = ' ' , font = 'verdana 25 bold' , fill = col [ 1 ]) File =[] for i in range ( 15 ): can . create_rectangle ( 50 + i * 60 , 150 , 110 + i * 60 , 210 , fill = 'white' , outline = 'red' ) de = can . create_text ( 90 + i * 60 , 175 , text = ' ' , font = 'verdana 25 bold' , fill = col [ i ]) item1 . append ( de ) donnees =[ File , item1 , item11 , item12 , compte1 , compte2 , debut , fin ] item21 = Button ( fenetre , text = "Files enfile" , font = 'verdana 20 bold' , fg = 'red' , command = lambda : enfiler ( can , 15 )) can . grid ( row = 0 , column = 0 , columnspan = 2 ) item21 . grid ( row = 1 , column = 0 ) item21 = Button ( fenetre , text = "Files défile" , font = 'verdana 20 bold' , fg = 'red' , command = lambda : defiler ( can )) item21 . grid ( row = 1 , column = 1 ) fenetre . mainloop ()

.

Pile et File

.

html5 video converter by EasyHtml5Video.com v4.0

code Pile et File

from tkinter import * from random import * num =[ '❶' , '❷' , '❸' , '❹' , '❺' , '❻' , '❼' , '❽' , '❾' , '❿' , '⓫' , '⓬' , '⓭' , '⓮' , '⓯' , '⓰' , '⓱' , '⓲' , '⓳' , '⓴' ] col =[ 'red' , 'blue' , 'slate blue' , 'red' , 'blue' , 'black' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' , 'red' , 'blue' , 'gold' ] def enfiler ( File , valeur , numero ): global donnees don = donnees if numero == 1 : lafile = don [ 0 ] fin = len ( lafile ) if numero == 2 : lafile = don [ 3 ] fin = len ( lafile ) if fin < 10 : k = randrange ( 0 , valeur ) lafile . append ( num [ k ]) if numero == 1 : don [ 0 ]= lafile del lafile donnees = don if numero == 2 : don [ 3 ]= lafile del lafile donnees = don affiche1 ( can , 1 , numero ) def defiler ( can , numero ): global donnees don = donnees if numero == 1 : lafile , it1 = don [ 0 ], don [ 1 ] if numero == 2 : lafile , it1 = don [ 3 ], don [ 4 ] longueur = len ( lafile ) couleur = col [ longueur ] fal =[] if longueur > 0 : for i in range ( longueur - 1 ): fal . append ( lafile [ i + 1 ]) del lafile lafile = fal if numero == 1 : don [ 0 ], don [ 1 ]= lafile , it1 if numero == 2 : don [ 3 ], don [ 4 ]= lafile , it1 donnees = don affiche1 ( can , 2 , numero ) def affiche1 ( can , j , numero ): global donnees don = donnees if numero == 1 : lafile , it1 = don [ 0 ], don [ 1 ], if numero == 2 : lafile , it1 = don [ 3 ], don [ 4 ] longueur = len ( lafile ) if j == 1 and longueur <= 10 : longueur = len ( lafile ) for i in range ( 10 ): can . itemconfigure ( it1 [ i ], text = ' ' ) for i in range ( longueur ): texte = lafile [ i ] can . itemconfigure ( it1 [ i ], text = texte ) if j == 2 : longueur = len ( lafile ) for i in range ( 10 ): can . itemconfigure ( it1 [ i ], text = ' ' ) for i in range ( len ( lafile )): texte = lafile [ i ] can . itemconfigure ( it1 [ i ], text = texte ) if numero == 1 : don [ 0 ], don [ 1 ]= lafile , it1 del lafile if numero == 2 : don [ 3 ], don [ 4 ]= lafile , it1 del lafile donnees = don def empiler ( can , valeur , numero ): global donnees don = donnees if numero == 1 : lapile = don [ 6 ] if numero == 2 : lapile = don [ 9 ] fin = len ( lapile ) if fin <= 10 : k = randrange ( 0 , valeur ) lapile . append ( num [ k ]) if numero == 1 : don [ 6 ]= lapile del lapile if numero == 2 : don [ 9 ]= lapile del lapile donnees = don affiche ( can , 1 , numero ) def deplier ( can , numero ): global donnees don = donnees if numero == 1 : lapile = don [ 6 ] if numero == 2 : lapile = don [ 9 ] fin = len ( lapile ) if fin > 0 : del lapile [ fin - 1 ] if numero == 1 : don [ 6 ]= lapile if numero == 2 : don [ 9 ]= lapile donnees = don affiche ( can , 2 , numero ) def affiche ( can , j , numero ): global donnees don = donnees if numero == 1 : lapile , pit2 = don [ 6 ], don [ 7 ] if numero == 2 : lapile , pit2 = don [ 9 ], don [ 10 ] longueur = len ( lapile ) couleur = col [ longueur ] if j == 1 : longueur = len ( lapile ) for i in range ( len ( lapile )): texte = lapile [ i ] can . itemconfigure ( pit2 [ i ], text = texte ) if numero == 1 : don [ 6 ], don [ 7 ]= lapile , pit2 del lapile if numero == 2 : don [ 9 ], don [ 10 ]= lapile , pit2 del lapile if j == 2 : for i in range ( len ( lapile )+ 1 ): can . itemconfigure ( pit2 [ i ], text = ' ' ) for i in range ( len ( lapile )): texte = lapile [ i ] can . itemconfigure ( pit2 [ i ], text = texte ) if numero == 1 : don [ 6 ], don [ 7 ]= lapile , pit2 if numero == 2 : don [ 9 ], don [ 10 ]= lapile , pit2 donnees = don fenetre = Tk () Pile =[] File =[] Pile2 =[] File2 =[] compte1 =[] compte2 =[] compte201 =[] compte202 =[] fenetre . geometry ( "1050x860" ) fenetre . configure ( bg = 'light blue' ) fenetre . grid () can = Canvas ( fenetre , height = 800 , width = 950 , bg = 'blue' ) can . grid ( row = 0 , column = 0 , columnspan = 6 , rowspan = 7 ) can . create_rectangle ( 40 , 150 , 660 , 350 , fill = 'sky blue' , outline = 'red' ) can . create_rectangle ( 40 , 490 , 660 , 690 , fill = 'sky blue' , outline = 'red' ) item12 = can . create_text ( 200 , 350 , text = "" , font = "verdana 25 bold" , fill = 'light green' ) item2 =[] item2b =[] can . create_text ( 900 , 40 , text = 'PILE\n 2 ' , font = 'verdana 14 bold' , fill = 'white' ) can . create_text ( 750 , 40 , text = 'PILE\n 1 ' , font = 'verdana 14 bold' , fill = 'white' ) item12 = can . create_text ( 300 , 80 , text = 'FILE\n 2' , font = 'verdana 14 bold' , fill = 'black' ) item12b = can . create_text ( 300 , 410 , text = 'FILE\n 1' , font = 'verdana 14 bold' , fill = 'white' ) item1 =[] item1b =[] for i in range ( 10 ): can . create_rectangle ( 50 + i * 60 , 550 , 110 + i * 60 , 610 , fill = 'white' , outline = 'red' ) de = can . create_text ( 80 + i * 60 , 580 , text = ' ' , font = 'verdana 25 bold' , fill = col [ i ]) item1 . append ( de ) can . create_rectangle ( 50 + i * 60 , 250 , 110 + i * 60 , 310 , fill = 'white' , outline = 'red' ) deb = can . create_text ( 80 + i * 60 , 280 , text = ' ' , font = 'verdana 25 bold' , fill = col [ i ]) item1b . append ( deb ) for i in range ( 10 ,- 1 ,- 1 ): can . create_rectangle ( 720 , 90 + i * 60 , 780 , 150 + i * 60 , fill = 'white' , outline = 'red' ) de1 = can . create_text ( 750 , 120 + i * 60 , text = ' ' , font = 'verdana 25 bold' , fill = col [ i ]) item2 . append ( de1 ) can . create_rectangle ( 840 , 90 + i * 60 , 900 , 150 + i * 60 , fill = 'white' , outline = 'red' ) de1b = can . create_text ( 870 , 120 + i * 60 , text = ' ' , font = 'verdana 25 bold' , fill = col [ i ]) item2b . append ( de1b ) donnees =[ File , item1 , '' , File2 , item1b , '' , Pile , item2 , '' , Pile2 , item2b , '' ] itemp = Button ( fenetre , text = "File 1\nenfile" , font = 'verdana 10 bold' , fg = 'red' , bg = 'yellow green' , command = lambda : enfiler ( can , 11 , 1 )) itemp . grid ( row = 9 , column = 0 ) item22a = Button ( fenetre , text = "File 1\ndefile" , font = 'verdana 10 bold' , fg = 'red' , bg = 'yellow green' , command = lambda : defiler ( can , 1 )) item22a . grid ( row = 9 , column = 2 ) bitet = Button ( fenetre , text = "File 2\nenfile" , font = 'verdana 10 bold' , fg = 'red' , bg = 'yellow green' , command = lambda : enfiler ( can , 11 , 2 )) bitet . grid ( row = 9 , column = 1 ) item22a = Button ( fenetre , text = "File 2\ndefile" , font = 'verdana 10 bold' , fg = 'red' , bg = 'yellow green' , command = lambda : defiler ( can , 2 )) item22a . grid ( row = 9 , column = 3 ) item21a = can . create_text ( 150 , 750 , text = "Files enfile" , font = 'verdana 20 bold' , fill = 'red' ) can . create_text ( 500 , 750 , text = "Files défile" , font = 'verdana 20 bold' , fill = 'red' ) itemm = Button ( fenetre , text = "Pile 1\nempile" , font = 'verdana 10 bold' , fg = 'red' , bg = 'yellow green' , command = lambda : empiler ( can , 11 , 1 )) itemm . grid ( row = 1 , column = 9 ) item22ta = Button ( fenetre , text = "Pile 1\ndepile" , font = 'verdana 10 bold' , fg = 'red' , bg = 'yellow green' , command = lambda : deplier ( can , 1 )) item22ta . grid ( row = 4 , column = 9 ) item21tab = Button ( fenetre , text = "Pile 2\nempile" , font = 'verdana 10 bold' , fg = 'red' , bg = 'yellow green' , command = lambda : empiler ( can , 11 , 2 )) item21tab . grid ( row = 2 , column = 9 ) item22ta = Button ( fenetre , text = "Pile 2\ndepile" , font = 'verdana 10 bold' , fg = 'red' , bg = 'yellow green' , command = lambda : deplier ( can , 2 )) item22ta . grid ( row = 5 , column = 9 ) item21b = Button ( fenetre , text = "Piles\nempile" , font = 'verdana 10 bold' , fg = 'red' ) item21b . grid ( row = 0 , column = 9 ) item22b = Button ( fenetre , text = "Piles\ndépile" , font = 'verdana 10 bold' , fg = 'red' ) item22b . grid ( row = 3 , column = 9 ) fenetre . mainloop ()