Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct elemento {
- int valore;
- struct elemento *next;
- };
- void printList(struct elemento *lis){
- printf("Lista: \n");
- while(lis!=NULL){
- printf("%d\n",lis->valore);
- lis=lis->next;
- }
- printf("------\n\n");
- return;
- }
- void append(struct elemento **lis, int val){
- struct elemento *e = malloc(sizeof(struct elemento));
- e->valore = val;
- e->next = NULL;
- if((*lis)==NULL){
- (*lis) = e;
- return;
- }
- if((*lis)->next == NULL){
- (*lis)->next = e;
- return;
- }
- struct elemento *t = *lis;
- while(t->next!=NULL){
- t=t->next;
- if(t->next==NULL){
- t->next = e;
- return;
- }
- }
- }
- void deleteFirst(struct elemento **lis, int val){
- if(*lis==NULL){
- return;
- }
- struct elemento *l;
- l = *lis;
- struct elemento *l2;
- l2 = *lis;
- while(l!=NULL){
- if(l==(*lis) && l->valore==val){
- struct elemento *el;
- el=(*lis)->next;
- free(*lis);
- (*lis)=el;
- return;
- } else if(l->valore == val){
- struct elemento *el;
- el=l->next;
- free(l);
- l2->next = el;
- return;
- }
- if(l!=*lis){
- l2=l2->next;
- }
- l=l->next;
- }
- return;
- }
- void deleteAll(struct elemento **lis, int val){
- if(*lis==NULL){
- return;
- }
- struct elemento *l;
- l = *lis;
- struct elemento *l2;
- l2 = *lis;
- while(l!=NULL){
- if(l==(*lis) && l->valore==val){
- struct elemento *el;
- el=(*lis)->next;
- free(*lis);
- (*lis)=el;
- l2=el;
- l=el;
- continue;
- } else if(l->valore == val){
- struct elemento *el;
- el=l->next;
- free(l);
- l2->next = el;
- l=l2->next;
- continue;
- }
- if(l!=*lis){
- l2=l2->next;
- }
- l=l->next;
- }
- return;
- }
- int main(){
- // creo una lista
- struct elemento *lis = NULL;
- append(&lis,12);
- append(&lis,2);
- append(&lis,12);
- append(&lis,12);
- printList(lis);
- deleteFirst(&lis,12);
- deleteFirst(&lis,10);
- printf("\ndeleteFirst(&lis,12)\ndeleteFirst(&lis,10)\n");
- printList(lis);
- append(&lis,22);
- append(&lis,12);
- printf("\nappend(&lis,22)\nappend(&lis,12)\n");
- printList(lis);
- deleteAll(&lis,10);
- printf("\ndeleteAll(&lis,10)\n");
- printList(lis);
- deleteAll(&lis,12);
- printf("\ndeleteAll(&lis,12)\n");
- printList(lis);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement