Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef struct nodo *lista;
- typedef struct nodo {
- char *nome;
- lista next;
- } nodo;
- lista creaLista (void);
- lista inserTesta (lista L, char elem[]);
- void inserCoda (lista L, char elem[]);
- void stampaLista (lista L);
- lista ultimoNodo (lista L);
- void elidi (lista L, char c);
- void elidi_aux (nodo *N);
- int main (void) {
- lista L;
- char c;
- L = creaLista();
- stampaLista(L);
- printf("\nQual รจ l'iniziale da cancellare? ");
- scanf(" %c", &c);
- elidi(L, c);
- stampaLista(L);
- }
- void elidi (lista L, char c) {
- while (L != NULL) {
- if (L->nome[0] == c)
- elidi_aux(L);
- L = L->next;
- }
- }
- void elidi_aux (nodo *N) {
- int i, lun;
- char *M;
- for (i=0; N->nome[i] != '\0'; i++)
- N->nome[i] = N->nome[i+1];
- lun = strlen(N->nome);
- M = calloc(lun+1, sizeof(char));
- strcpy(M, N->nome);
- free(N->nome);
- N->nome = M;
- }
- lista creaLista (void) {
- lista L = NULL;
- nodo N;
- int primo = 1;
- char buffer[21];
- while (1) {
- printf("Inserisci il prossimo valore ($ per terminare): ");
- scanf("%s", buffer);
- if (strcmp(buffer, "$") == 0) {
- break;
- }
- else {
- if (primo == 1) {
- L = inserTesta (L, buffer);
- primo = 0;
- }
- else {
- inserCoda (L, buffer);
- }
- }
- }
- return L;
- }
- lista inserTesta (lista L, char elem[21]) {
- nodo *N;
- int lun;
- char *nome;
- N = malloc(sizeof(nodo));
- lun = strlen(elem);
- nome = calloc(lun, sizeof(char));
- if (N == NULL || nome == NULL) {
- printf("Memoria insufficiente\n");
- }
- else {
- N->nome = nome;
- N->next = L;
- strcpy(N->nome, elem);
- L = N;
- }
- return L;
- }
- void inserCoda (lista L, char elem[21]) {
- nodo *N, *M;
- int lun;
- char *nome;
- N = malloc(sizeof(nodo));
- lun = strlen(elem);
- nome = calloc(lun, sizeof(char));
- if (N == NULL || nome == NULL) {
- return;
- }
- else {
- N->nome = nome;
- M = ultimoNodo (L);
- M->next = N;
- strcpy(N->nome, elem);
- N->next = NULL;
- }
- }
- lista ultimoNodo (lista L) {
- while (L->next != NULL) {
- L = L->next;
- }
- return L;
- }
- void stampaLista (lista L) {
- printf("\nI nomi inseriti sono:\n");
- while (L != NULL) {
- printf("%s ", L->nome);
- L = L->next;
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement