Ver Mensaje Individual
  #1 (permalink)  
Antiguo 28/03/2012, 11:03
Avatar de bichomen
bichomen
 
Fecha de Ingreso: junio-2003
Ubicación: Barcelona y alrededores, España
Mensajes: 877
Antigüedad: 21 años
Puntos: 2
Pilas y colas

Estoy haciendo un ejercicio para clase sobre pilas y colas, os pongo todo el código pero principalmente quiero que me ayudéis con el constructor de la clase Stack (pila) que creo que me estoy liando, cualquier ayuda sera bienvenida, gracias de antemano:

Código:
import random

#################################
#Movie

#Creamos la clase Movie
class Movie:
        
        def __init__(self,title="emptyTitle",
                                          director=["emptyDirector"],
                                          cast = ["emptyCast"],
                                          producer = ["emptyProducer"],
                                          writer = ["emptyWriter"],
                                          country = ["emptyCountry"],
                                          language = ["emptyLanguage"],
                                          year = "emptyYear",
                                          genres = ["emptyGenres"],
                                          votes = "emptyVotes",
                                          rating = "emptyRating",
                                          runtime = ["emptyRuntime"],
                                          plot = ["emptyPlot"],
                                          cover_url = "emptyCorver"):
                
                self.title = title
                self.director = director
                self.cast = cast
                self.producer = producer
                self.writer = writer
                self.country = country
                self.language = language
                self.year = year
                self.genres = genres
                self.votes = votes
                self.rating = rating
                self.runtime = runtime
                self.plot = plot
                self.cover_url = cover_url

        def GetTitle(self):
                return self.title




#################################
#función loadMovieList

def loadMovieList(filename):
        peliculas = abrirarchivo(filename)
        movieList = []
        
        for peli in range(len(peliculas)):
                pelicula = peliculas[peli].split("|")
            
                p = Movie(pelicula[0],pelicula[1],pelicula[2],pelicula[3],pelicula[4],pelicula[5],pelicula[6],pelicula[7],pelicula[8],pelicula[9],pelicula[10],pelicula[11],pelicula[12],pelicula[13])
                movieList.append(p)

        movieList.sort()

        return movieList

#################################
#Abrimos el archivo

def abrirarchivo(db):
    file = open(db,"r")
    peliculas = file.readlines()
    file.close()

    return peliculas

movieList = loadMovieList('peliculas100.dat')

#Reducimos la lista de peliculas a 21 elementos
movieList = movieList[0:20]


#################################
#Nodo
        
#Creamos la clase Nodo
class Node:

        def __init__(self,data,root="none"):
                data = data
                self.root = "none"
                
#################################
#Stack
        
#Creamos la clase Stack
class Stack:

        #El constructor de la clase Stack
        def __init__(self):
                tmp = 0
                self.data =  movieList
                head = Node(tmp, len(data))
                
        #Si la pila esta vacia, que apunte a None
        def isEmpty(head):
                head.data = "none"
                
        #Imprimimos el titulo de la pelicula actual
        def printStack(self):
                #Meto el nodo actual en una variable temporal
                tmp = head.data
                #Guado en una variable a traves del metodo de la clase Movie, el titulo de la pelicula
                titulo = tmp.GetTitle()
                #Hago que la variable temporal, apunte a la siguiente pelicula
                tmp = tmp.next

                return titulo
                        
        #Añadimos un nodo
        def push(self,node):
                #Metemos el nuevo nodo en la variable temporal
                tmp = newNode
                #Hacemos que la variable temporal, su siguiente enlace sea el ultimo elemento de la pila
                tmp.next = head.data
                #Hacemos que head, apunte al nuevo nodo
                head = tmp

                return head.data
                                
        #Quitamos un nodo
        def pop(self):
                #Meto el ultimo nodo en una variable temporal
                tmp = head.data
                #Hago que head apunte al siguiente nodo
                head = head.next
                #Hago que el ultimo nodo apunte none
                tmp.next = "none"
                #Guardo el ultimo nodo, en otra variable
                output = tmp.data
                #Vacio la variable temporal
                tmp = "none"
                
                return output

#################################
#Queure
        
#Creamos la clase Queure
#class Queure:

 #       def __init__(self):

  #      def isEmpty(self):

   #     def printQ(self):

    #    def enqueue(self,node):

     #   def dequeue(self):
            


#################################
#Test de la clase Stack()

newStack = Stack()
newStack.printStack()

newNode = Node(movieList[0])
newStack.push(newNode)
newStack.printStack()

newStack.push(Node(movieList[1]))
newStack.printStack()

newStack.push(Node(movieList[2]))
newStack.printStack()

m1 = newStack.pop()
newStack.printStack()

m2 = newStack.pop()
newStack.printStack()

m3 = newStack.pop()
newStack.printStack()

m4 = newStack.pop()
newStack.printStack()

print m1.GetTitle()
print m2.GetTitle()
print m3.GetTitle()
##print m4.GetTitle()  GUESS WHAT HAPPENS...

#################################
#Test de la clase Queure()

#newQ = Queue()
#newQ.printQ()

#newNode = Node(movieList[0])
#newQ.enqueue(newNode)
#newQ.printQ()

#newQ.enqueue(Node(movieList[1]))
#newQ.printQ()

#newQ.enqueue(Node(movieList[2]))
#newQ.printQ()

#m1 = newQ.dequeue()
#newQ.printQ()

#m2 = newQ.dequeue()
#newQ.printQ()

#m3 = newQ.dequeue()
#newQ.printQ()

#m4 = newQ.dequeue()
#newQ.printQ()

#print m1.GetTitle()
#print m2.GetTitle()
#print m3.GetTitle()
#print m4.GetTitle()  GUESS WHAT HAPPENS...

bichomen
__________________
"Se sabe con exactitud, con cuanta imprecisión, se sabe algo"
Linux Registered User #320332