Programa Aula
Programa Aula
h>
#include <stdlib.h>
return node;
}
void printList(t_node *list){
if(list == NULL){
printf("Empty list.\n");
return;
}
do{
printf("%d", list->data);
list = list->next;
return node;
}
void tailInsert(t_node **list, t_node *node){
t_node *last = *list;
if(*list == NULL){
*list = node;
return;
}
while(last->next != NULL)
last = last->next;
last->next = node;
}
t_node *tailRemove(t_node **list){
t_node *beforeLast = *list;
t_node *node;
if(*list == NULL){
printf("Error removing. Empty list.\n");
return NULL;
}
if((*list)->next == NULL){
node = *list;
*list = NULL;
return node;
}
while(beforeLast->next->next != NULL)
beforeLast = beforeLast->next;
node = beforeLast->next;
beforeLast->next = NULL;
return node;
}
void sortInsert(t_node **list, t_node *node){
if(*list == NULL || (*list)->data >= node->data)
headInsert(list, node);
else
sortInsert(&((*list)->next), node);
}
t_node *removeByVal(t_node **list, t_data val){
t_node *node;
if(*list == NULL)
return NULL;
if((*list)->data == val){
node = headRemove(list);
return node;
}
removeByVal(&((*list)->next), val);
}
int main(void){
t_node *theList = NULL;
t_node *node;
t_data val=0;
int *p = NULL;
int op;
do{
printf("1: Inserir\n");
printf("2: Remover\n");
printf("0: Sair\n");
scanf("%d", &op);
switch(op){
case 1:
printf("Elemento a introduzir na lista: ");
scanf("%d", &val);
node = newNode(val);
sortInsert(&theList, node);
break;
case 2:
printf("Elemento a remover da lista: ");
scanf("%d", &val);
removeByVal(&theList, val);
break;
case 0: break;
default: printf("Opcao invalida.\n");
}
printList(theList);
} while(op != 0);
}