Buscar

Lista Encadeada

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

{
 "cells": [
 {
 "cell_type": "code",
 "execution_count": 7,
 "id": "e47976b3",
 "metadata": {},
 "outputs": [
 {
 "name": "stdout",
 "output_type": "stream",
 "text": [
 "1 2 3 4 5 "
 ]
 }
 ],
 "source": [
 "class Node:\n",
 " def __init__(self, data=None, next=None):\n",
 " self.data = data\n",
 " self.next = next\n",
 " \n",
 " # I changed head to an arg since\n",
 " # it's the easiest way to pass it to the Node object\n",
 " def printLinkedlist(self, head):\n",
 " temp = head\n",
 " while temp:\n",
 " print(temp.data, end=' ')\n",
 " temp = temp.next\n",
 "\n",
 "class Linkedlist:\n",
 " def __init__(self):\n",
 " self.head = None\n",
 "\n",
 " def append(self, new_data):\n",
 " new_node = Node(new_data)\n",
 "\n",
 " if self.head == None:\n",
 " self.head = new_node\n",
 " return\n",
 "\n",
 " last_node = self.head \n",
 " \n",
 " while last_node.next: \n",
 " last_node = last_node.next\n",
 "\n",
 " last_node.next = new_node\n",
 "\n",
 "\n",
 "a = Linkedlist()\n",
 "a.append(1)\n",
 "a.append(2)\n",
 "a.append(3)\n",
 "a.append(4)\n",
 "a.append(5)\n",
 "# here we get the head node object you stored within your LinkedList class\n",
 "head_node = a.head\n",
 "# the node object DOES have a printLinkedlist method\n",
 "# so we can call it on that\n",
 "head_node.printLinkedlist(head_node)"
 ]
 },
 {
 "cell_type": "code",
 "execution_count": null,
 "id": "550e18dd",
 "metadata": {},
 "outputs": [],
 "source": []
 }
 ],
 "metadata": {
 "kernelspec": {
 "display_name": "Python 3",
 "language": "python",
 "name": "python3"
 },
 "language_info": {
 "codemirror_mode": {
 "name": "ipython",
 "version": 3
 },
 "file_extension": ".py",
 "mimetype": "text/x-python",
 "name": "python",
 "nbconvert_exporter": "python",
 "pygments_lexer": "ipython3",
 "version": "3.8.8"
 }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Continue navegando

Outros materiais