Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include "TADabas.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- typedef struct no_ NO;
- typedef struct lista_ LISTA;
- void lista_imprimir(LISTA *lista);
- struct no_{
- char nome[40];
- char URL[512];
- NO* prox;
- };
- struct lista_{
- NO* inicio;
- NO* fim;
- int size;
- };
- LISTA *lista_criar(void){
- LISTA *lista = (LISTA*)malloc(sizeof(LISTA));
- lista->inicio = NULL;
- lista->fim == NULL;
- lista->size = 0;
- return lista;
- }
- void lista_inserir_fim(LISTA *lista, char *nome, char *URL){
- NO *p = (NO*)malloc(sizeof(NO));
- strcpy(p->nome, nome);
- strcpy(p->URL, URL);
- if (lista != NULL){
- if (lista->inicio == NULL){ //se for o primeiro elemento a ser inserido
- p->prox = NULL;
- lista->inicio = p;
- }
- else {
- lista->fim->prox = p;
- p->prox = NULL;
- }
- lista->fim = p;
- lista->size++;
- return;
- }
- return;
- }
- void lista_inserir_ordenado(LISTA *lista, NO* no, int i){
- NO *n = lista->inicio;
- int flag = 0;
- int j = 0;
- if (lista->inicio == NULL){
- no->prox = NULL;
- lista->inicio = no;
- printf ("%s\n", lista->inicio->nome);
- return;
- }
- if (i == 1){
- lista->inicio = no;
- no->prox = n;
- flag = 1;
- printf ("%s\n", lista->inicio->nome);
- lista->size++;
- return;
- }
- if (flag == 0){
- for (j = 1; j < (lista->size); j++){
- if (j == i-1){
- NO* p = n->prox;
- n->prox = no;
- no->prox = p;
- flag = 1;
- lista->size++;
- break;
- }
- n = n->prox;
- }
- }
- if (flag == 0){
- lista->fim->prox = no;
- no->prox = NULL;
- lista->fim = no;
- lista->size++;
- }
- return;
- }
- void lista_apagar(LISTA **ptr);
- void lista_remover(LISTA *lista, char *nome){
- NO *p, *q;
- p = lista->inicio;
- q = NULL;
- if (!strcmp(nome, p->nome)){
- lista->inicio = p->prox;
- lista->size--;
- free(p);
- return;
- }
- while (p != NULL && strcmp(nome, p->nome)){
- q = p;
- p = p->prox;
- }
- if (p==NULL){
- return;
- }
- q->prox = p->prox;
- lista->size--;
- free (p);
- return;
- }
- void encontrar_no(LISTA* lista, char *nome, NO* no){
- NO *p, *q;
- p = lista->inicio;
- q = NULL;
- if (!strcmp(nome, p->nome)){
- strcpy(no->nome, p->nome);
- strcpy(no->URL, p->URL);
- return;
- }
- int count = 0;
- while (p->prox != NULL && strcmp(nome, p->nome)){
- q = p;
- p = p->prox;
- count++;
- }
- if (p==NULL){
- return;
- }
- strcpy(no->nome, p->nome);
- strcpy(no->URL, p->URL);
- free (p);
- return;
- }
- void trocar_posicao(LISTA *lista, char *nome, int i){
- NO *aux = lista->inicio;
- NO *p = (NO*)malloc(sizeof(NO));
- encontrar_no(lista, nome, p);
- printf ("\nNO encontrado: %s\n", p->nome);
- lista_remover(lista, nome);
- printf("\nLISTA (NO removido):\n");
- lista_imprimir(lista);
- printf("\n");
- lista_inserir_ordenado(lista, p, i);
- printf("LISTA (NO inserido):\n");
- lista_imprimir(lista);
- printf("\n");
- return;
- }
- void lista_imprimir(LISTA *lista){
- NO* p = lista->inicio;
- for (int j = 0; p!=NULL; j++){
- printf("%s %s\n", p->nome, p->URL);
- p = p->prox;
- }
- }
- int main(){
- LISTA* l = lista_criar();
- char nome1[40], URL[512], nome2[40];
- int N;
- printf("MENU:\nIncluir nova aba (1)\nAlterar posição da aba (2)\nImprimir (3)\nSair (4)\n");
- scanf("%d", &N);
- while (N!=4){
- if (N == 1){
- scanf("%s", nome1);
- scanf("%s", URL);
- lista_inserir_fim(l, nome1, URL);
- }
- if (N == 2){
- int i;
- scanf("%s", nome2);
- scanf("%d", &i);
- trocar_posicao(l, nome2, i);
- }
- if (N == 3){
- lista_imprimir(l);
- }
- scanf("%d", &N);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement