Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <malloc.h>
- using namespace std;
- typedef struct _node {
- int x;
- struct _node *prox;
- } node;
- node *head;
- node *tail;
- node* cria() {
- node *aux;
- aux = (node*)malloc(sizeof(node));
- head = aux;
- tail = head;
- return head;
- }
- node* insereFIM(node *lst, int x) {
- node *aux;
- aux = (node*)malloc(sizeof(node));
- aux->x = x;
- aux->prox = NULL;
- tail->prox = aux;
- tail = aux;
- return head;
- }
- node* insereINI(node *lst, int x) {
- node *aux;
- aux = (node*)malloc(sizeof(node));
- aux->prox = head;
- head->x = x;
- head = aux;
- return head;
- }
- void mostra(node *lst) {
- node *aux;
- aux = lst->prox;
- while (aux != NULL) {
- cout << aux->x << "\n";
- aux = aux->prox;
- }
- }
- node* removeINI(node *lst) {
- node *aux;
- cout << "\n Elemento removido: " << lst->prox->x << "\n";
- aux = head;
- head = head->prox;
- free(aux);
- return head;
- }
- node* removeFIM(node *lst) {
- node *aux;
- node *ant;
- aux = lst->prox;
- while (aux->prox != NULL) {
- ant = aux;
- aux = aux->prox;
- }
- cout << "\n Elemento removido: " << aux->x << "\n";
- tail = ant;
- tail->prox = NULL;
- free(aux);
- return head;
- }
- int main() {
- system("MODE CON: LINES=50");
- system("CLS");
- node *lista;
- int x, y;
- lista = cria();
- for (int i=0; i<5; i++){
- cout << "\n Digite o valor " << i+1 << ": ";
- cin >> x;
- lista = insereFIM(lista, x);
- }
- mostra(lista);
- lista = removeFIM(lista);
- mostra(lista);
- cout << "\n";
- for (int i=0; i<5; i++){
- cout << "\n Digite o valor " << i+1 << ": ";
- cin >> x;
- lista = insereINI(lista, x);
- }
- cout << "\n\n";
- mostra(lista);
- lista = removeINI(lista);
- mostra(lista);
- cout << "\n\n";
- // lĂŞ lista de um arquivo
- ifstream txt("num.txt");
- while(txt >> y) {
- lista = insereINI(lista, y);
- }
- mostra(lista);
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement