C Helpdesk
USUWANIE PODANEGO ELEMENTU Z LISTY
#include <stdlib.h> #include <stdio.h> typedef struct element element; struct element { int x; struct element *next; }; void wyswietl(element *lista) { while (lista) { printf("[%d] -> ", lista->x); lista=lista->next; } printf("[NULL]"); } void usun(element **lista, int y) { element *e, *tmp, *tmp1; e=*lista; tmp1=*lista; tmp=tmp1; while (tmp1) { if (*lista == NULL) { printf("LISTA PUSTA"); getchar(); getchar(); exit (0); } if ((*lista)->x == y) { e=(*lista)->next; free(*lista); *lista=e; } else { if (tmp1->x == y) { tmp->next = tmp1->next; free(tmp1); tmp1=tmp; } tmp=tmp1; tmp1=tmp1->next; } } } main() { element *lista, *e; int i,y; lista=NULL; for(i=0; i<5; i++) { e=malloc(sizeof(element)); scanf("%i", &e->x); e->next=lista; lista=e; } wyswietl(lista); printf("\n\npodaj co chcesz usunac\n"); scanf("%i", &y); usun(&lista,y); wyswietl(lista); getchar(); getchar(); }
Uwaga!
Wypada jeszcze po sobie posprzątać . To znaczy na końcu należy wyczyścić całą listę !
Algorytm created by mmiles and mieczyk Company
Ostatnio edytowany przez mieczyk (05-21-2007 17:02:44)
Offline
DODAWANIE NA KONIEC LISTY
#include <stdio.h> #include <stdlib.h> typedef struct element element; struct element { int x; struct element *next; }; void dodaj(element **lista, int y) { element *e, *tmp; e = malloc(sizeof(element)); e->x=y; if (*lista == NULL) { e->next = *lista; *lista = e; } else { tmp = *lista; while (tmp->next != NULL) tmp=tmp->next; tmp->next = e; e->next = NULL; } } void wyswietl(element *lista) { printf("\n\n"); while(lista) { printf("[ %i ] ->", lista->x); lista=lista->next; } printf("[NULL]"); getchar(); getchar(); } void kasuj(element *lista) { element *e; while(!lista) { e=lista->next; free(lista); lista=e; } printf("\n\nlista skasowana"); } main () { element *lista, *e; int i, k; lista=NULL; for (i=0; i<5; i++) { printf("Podaj liczbe\n"); scanf("%i", &k); dodaj(&lista, k); } wyswietl(lista); kasuj(lista); getchar(); }
powinno działać a jak nie to pisać sie poprawi
Offline
SORTOWANIE LISTY
#include <stdio.h> #include <stdlib.h> typedef struct lista lista; struct lista { int a; struct lista *next; }; void sort(lista **pocz) { lista *e, *tmp, *nowa; nowa=NULL; while (*pocz) { e=malloc(sizeof(lista)); e->a = (*pocz)->a; if (nowa == NULL) { e->next=nowa; nowa=e; } else { if((e->a) < (nowa->a)) { e->next=nowa; nowa=e; } else { tmp=nowa; while((tmp->next) != NULL) { if((tmp->next->a) > (e->a)) break; tmp=tmp->next; } e->next = tmp->next; tmp->next=e; } } (*pocz)=(*pocz)->next; } *pocz=nowa; } int main() { lista *pocz, *e; int i,b; char a; pocz=NULL; for(i=0; i<5; i++) { e=malloc(sizeof(lista)); scanf("%d", &(e->a)); e->next=pocz; pocz=e; } sort(&pocz); e=pocz; printf("\n"); while (e) { printf("%d\n",e->a); e=e->next; } getchar(); getchar(); return 0; }
Offline
WYCIAGANIE NAJWIEKSZEGO ELEMENTU NA POCZATEK LISTY
#include <stdio.h> #include <stdlib.h> typedef int DANE; struct lista { DANE d; struct lista *nast; }; typedef struct lista ELEMENT; typedef ELEMENT *lista; void usun(lista *st, int maks) { lista e, tmp, tmp1; e=*st; tmp1=*st; tmp=tmp1; while (tmp1) { if (*st == NULL) { printf("LISTA PUSTA"); getchar(); getchar(); exit (0); } if ((*st)->d == maks) { e=(*st)->nast; free(*st); *st=e; } else { if (tmp1->d == maks) { tmp->nast = tmp1->nast; free(tmp1); tmp1=tmp; } tmp=tmp1; tmp1=tmp1->nast; } } } void max(lista *st) { lista e, tmp; int maks, licznik=0, licznik1; /*szukanie elemenetu najwiekszego maks*/ tmp=*st; maks=tmp->d; while(tmp) { if (tmp->d >= maks) maks=tmp->d; tmp=tmp->nast; } /* sprawdzanie ile jest elementow najwiekszych*/ tmp=*st; while (tmp) { if (maks == tmp->d) licznik++; tmp=tmp->nast; } usun(st,maks); /*wstawianie maksa na początek*/ tmp=*st; while (licznik !=0) { e=malloc(sizeof(ELEMENT)); e->d=maks; e->nast=tmp; tmp=e; licznik--; } *st=tmp; } void wyswietl(lista pocz) { while (pocz) { printf("[ %i ] -> ", pocz->d); pocz=pocz->nast; } printf("[NULL]\n\n"); } main() { lista e, pocz; int i; pocz=NULL; for (i=0; i<5; i++) { e=malloc(sizeof(ELEMENT)); printf("podaj liczbe: "); scanf("%i",&e->d); e->nast=pocz; pocz=e; } wyswietl(pocz); max(&pocz); wyswietl(pocz); while (pocz) { e=pocz->nast; free(pocz); pocz=e; } getchar(); getchar(); printf("lista skasowana"); getchar(); }
algorytm jest dosyć długi mozna napewno zrobić to krócej ale działa fukcja usuwania z listy jest identyczna jak w poscie mieczyka a że pisaliśmy ją razem bez obaw użyłem ją do tego programu
Offline