Advertisement
Anilto

Lista dupla

Oct 31st, 2020
2,138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <malloc.h>
  4.  
  5. using namespace std;
  6.  
  7. typedef struct _node {
  8.     int x;
  9.     struct _node *prox;
  10. } node;
  11.  
  12. node *head;
  13. node *tail;
  14.  
  15. node* cria() {
  16.     node *aux;
  17.     aux = (node*)malloc(sizeof(node));
  18.     head = aux;
  19.     tail = head;
  20.     return head;
  21.  
  22. }
  23.  
  24. node* insereFIM(node *lst, int x) {
  25.     node *aux;
  26.     aux = (node*)malloc(sizeof(node));
  27.     aux->x = x;
  28.     aux->prox = NULL;
  29.     tail->prox = aux;
  30.     tail = aux;
  31.     return head;
  32.  
  33. }
  34.  
  35. node* insereINI(node *lst, int x) {
  36.     node *aux;
  37.     aux = (node*)malloc(sizeof(node));
  38.     aux->prox = head;
  39.     head->x = x;
  40.     head = aux;
  41.     return head;
  42. }
  43.  
  44. void mostra(node *lst) {
  45.     node *aux;
  46.     aux = lst->prox;
  47.     while (aux != NULL) {
  48.         cout << aux->x << "\n";
  49.         aux = aux->prox;
  50.            
  51.     }
  52.  
  53. }
  54.  
  55. node* removeINI(node *lst) {
  56.     node *aux;
  57.     cout << "\n Elemento removido: " << lst->prox->x << "\n";
  58.     aux = head;
  59.     head = head->prox;
  60.     free(aux);
  61.     return head;
  62. }
  63.  
  64.  
  65. node* removeFIM(node *lst) {
  66.     node *aux;
  67.     node *ant;
  68.     aux = lst->prox;
  69.     while (aux->prox != NULL) {
  70.         ant = aux;
  71.         aux = aux->prox;
  72.     }
  73.    
  74.     cout << "\n Elemento removido: " << aux->x << "\n";
  75.    
  76.     tail = ant;
  77.     tail->prox = NULL;
  78.     free(aux);
  79.     return head;
  80.  
  81. }
  82.  
  83. int main() {
  84.     system("MODE CON: LINES=50");
  85.     system("CLS");
  86.  
  87.     node *lista;
  88.     int x, y;
  89.    
  90.     lista = cria();
  91.    
  92.     for (int i=0; i<5; i++){
  93.         cout << "\n Digite o valor " << i+1 << ": ";
  94.         cin >> x;
  95.         lista = insereFIM(lista, x);
  96.     }
  97.    
  98.     mostra(lista);
  99.     lista = removeFIM(lista);
  100.     mostra(lista);
  101.     cout << "\n";
  102.    
  103.     for (int i=0; i<5; i++){
  104.         cout << "\n Digite o valor " << i+1 << ": ";
  105.         cin >> x;
  106.         lista = insereINI(lista, x);
  107.     }
  108.     cout << "\n\n";
  109.     mostra(lista);
  110.     lista = removeINI(lista);
  111.     mostra(lista);
  112.     cout << "\n\n";
  113.     // lĂŞ lista de um arquivo
  114.     ifstream txt("num.txt");
  115.     while(txt >> y) {
  116.         lista = insereINI(lista, y);
  117.     }
  118.     mostra(lista);
  119.    
  120.     system("pause");
  121.    
  122. }
  123.  
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement