Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct nodo *lista;
- typedef struct nodo {
- int val;
- lista next;
- } nodo;
- lista creaLista (void);
- lista inserTesta (lista L, int elem);
- void inserCoda (lista L, int elem);
- lista ultimoNodo (lista L);
- void stampaLista (lista L);
- lista ricercaNodo (lista L, int key);
- void inserMezzo (lista L, int elem, int key);
- int lunghezzaLista (lista L);
- void inserisciInPosizione (lista L, int elem, int pos);
- void cancellaLista(lista L);
- void cancTesta (lista *L);
- void cancCoda (lista L);
- lista invertiLista (lista L);
- int main (void) {
- lista L, F;
- L = creaLista();
- stampaLista(L);
- stampaLista(L);
- }
- lista creaLista (void) {
- lista L = NULL, past = NULL;
- nodo N;
- int buffer, primo = 1, ris, key, pos;
- while (1) {
- printf("\n--------------------------------------------------\nInserisci il prossimo valore (0 per terminare): ");
- scanf("%d", &buffer);
- if (buffer == 0) {
- break;
- }
- else {
- if (primo == 1) {
- past = L;
- L = inserTesta (L, buffer);
- if (past == L) {
- printf("Inserimento in testa fallito\n");
- return L;
- }
- primo = 0;
- printf("--------------------------------------------------\n\n");
- }
- else {
- printf("\nPosizioni d'inserimento disponibili:\n1. In testa\n2. In coda\n3. All'interno\n4. In una posizione specificata\n\nDove vuoi inserire l'elemento? ");
- scanf("%d", &ris);
- if (ris == 1) {
- L = inserTesta (L, buffer);
- }
- else if (ris == 2) {
- inserCoda (L, buffer);
- }
- else if (ris == 3) {
- printf("\nDopo quale elemento vuoi inserire? ");
- scanf("%d", &key);
- inserMezzo(L, buffer, key);
- }
- else if (ris == 4) {
- printf("In che posizione vuoi inserire l'elemento? ");
- scanf("%d", &pos);
- inserisciInPosizione(L, buffer, pos);
- }
- printf("--------------------------------------------------\n\n");
- }
- }
- }
- printf("--------------------------------------------------\n\n");
- return L;
- }
- lista inserTesta (lista L, int elem) {
- nodo *N;
- N = malloc(sizeof(nodo));
- if (N == NULL) {
- printf("Memoria insufficiente\n");
- }
- else {
- N->next = L;
- N->val = elem;
- L = N;
- }
- return L;
- }
- void inserCoda (lista L, int elem) {
- nodo *N;
- nodo *M;
- N = malloc(sizeof(nodo));
- if (N == NULL) {
- return;
- }
- else {
- M = ultimoNodo (L);
- M->next = N;
- N->val = elem;
- N->next = NULL;
- }
- }
- lista ultimoNodo (lista L) {
- while (L->next != NULL) {
- L = L->next;
- }
- return L;
- }
- lista ricercaNodo (lista L, int key) {
- while ((L != NULL) && (L->val != key)) {
- L = L->next;
- }
- if (L == NULL) {
- printf("Valore non trovato\n");
- }
- return L;
- }
- void inserMezzo (lista L, int elem, int key) {
- nodo *F, *N;
- F = ricercaNodo(L, key);
- if (F == NULL) {
- return;
- }
- else {
- N = malloc(sizeof(nodo));
- if (N == NULL) {
- return;
- }
- else {
- N->next = F->next;
- F->next = N;
- N->val = elem;
- }
- }
- }
- int lunghezzaLista (lista L) {
- int lunghezza = 1;
- if (L == NULL) {
- return 0;
- }
- else {
- while (L->next != NULL) {
- L = L->next;
- lunghezza++;
- }
- return lunghezza;
- }
- }
- void inserisciInPosizione (lista L, int elem, int pos) {
- int i;
- nodo *M;
- if (pos == 1) {
- printf("Per inserire in testa, usa l'opzione 1\n");
- return;
- }
- else if ((pos < 1) || (pos > lunghezzaLista(L))) {
- printf("Valore non ammesso\n");
- return;
- }
- else {
- for (i=2; i<pos; i++) {
- L = L->next;
- }
- M = malloc(sizeof(nodo));
- if (M == NULL) {
- printf("Memoria insufficiente\n");
- return;
- }
- else {
- M->next = L->next;
- L->next = M;
- M->val = elem;
- }
- }
- }
- void cancellaLista (lista L) {
- }
- void cancTesta (lista *L) {
- nodo *N;
- if ((*L) != NULL) {
- N = (*L);
- (*L) = (*L)->next;
- free(N);
- }
- else
- return;
- }
- void cancCoda (lista L) {
- nodo *penultimo = L;
- nodo *M;
- M = ultimoNodo(L);
- while ((penultimo->next)->next != NULL) {
- penultimo = penultimo->next;
- }
- penultimo->next = NULL;
- free(M);
- }
- lista invertiLista (lista L) {
- lista L1 = NULL;
- nodo *succ;
- while (L != NULL) {
- succ = L->next;
- L->next = L1;
- L1 = L;
- L = succ;
- }
- return L1;
- }
- void stampaLista (lista L) {
- nodo *N;
- N = L;
- printf("\n--------------------------------------------------\nI valori inseriti sono:\n\n");
- while (N != NULL) {
- printf("%d ", N->val);
- N = N->next;
- }
- printf("\n--------------------------------------------------\n\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement