Login
...or Sign up
Home
Search
Search Code Snippets
Search People
12 list 0.9.1
#include
#include
typedef struct _list { struct _list *prev, *next; int item; } list; list *head=NULL, *tail=NULL; void dodaj (int item, int k); int sprawdz (); int usun (int n, int k); int wstaw (int item, int n); int wypisz (int n); void modyfikuj (int item, int n); void czysc (); void free (int k); int main () { int item,ocb,n,k; printf ("program operuje na listach\n" "prosze podaj wartosc dla 1 elementu listy\n"); scanf ("%d",&item); dodaj (item, 0); printf ("\ngratulacje! wlasnie dodales 1 element do listy\n"); do { k=sprawdz(); printf ("lista posiada aktualnie %d elementow, co chcesz teraz zrobic\n", k); printf ("1 dodac element na poczatek listy\n" "2 dodac element do konca listy\n" "3 wstaw element do listy\n" "4 skasowac jeden z elementow listy\n" "5 wypisac n-ty element listy\n" "6 zmienic wartosc elementu listy\n" "7 wyczyscic (wyzerowac) liste\n" "8 wyjsc z programu\n"); scanf ("%d",&ocb); switch (ocb) { default: continue; case 1: printf ("\npodaj wartosc elementu ktory chcesz dodac na poczatek listy\n"); scanf ("%d",&item); dodaj (item,1); break; case 2: printf ("\npodaj wartosc elementu ktory chcesz dodac na koniec listy\n"); scanf ("%d",&item); dodaj (item,2); break; case 3: printf ("podaj wartosc elementu ktory chcesz dodac do listy\n"); scanf ("%d",&item); printf ("podaj numer elementu po ktorym chcesz wstawic nowy element listy\n"); scanf ("%d",&n); if (n<1||n>=k) { printf ("podales numer elementu ze zlego przedzialu" "powinienes podac liczbe z zakresu od 1 do %d",(k-1)); continue; } wstaw (item,n); break; case 4: printf ("podaj numer elementu ktory ma zostac skasowany\n"); scanf ("%d",&n); if (n<1||n>k) { printf ("podales numer elementu ze zlego przedzialu\n" "powinienes podac liczbe z zakresu od 1 do %d\n",k); continue; } usun (n,k); break; case 5: printf ("podaj numer elementu listy ktory chcesz wypisac\n"); scanf ("%d",&n); if (n<1||n>k) { printf ("podales numer elementu ze zlego przedzialu\n" "powinienes podac liczbe z zakresu od 1 do %d\n",k); continue; } printf ("\n%d\n",wypisz (n)); break; case 6: printf ("podaj numer elementu listy ktory chcesz zmodyfikowac\n"); scanf ("%d",&n); if (n<1||n>=k) { printf ("podales numer elementu ze zlego przedzialu\n" "powinienes podac liczbe z zakresu od 1 do %d\n",(k-1)); continue; } printf ("podaj wartosc elementu ktory chcesz dodac do listy\n"); scanf ("%d",&item); modyfikuj (item,n); break; case 7: czysc (); break; case 8: printf ("tymczasem"); free (k); return 0; break; } } while (1); return 0; } void dodaj (int item, int k) { list *current,*e; if (head==NULL) { head=tail=(list*)malloc(sizeof(list)); head->next=NULL; head->prev=NULL; head->item=item; } if (k==2) { current=tail; e=(list*)malloc(sizeof(list)); current->next=e; e->prev=current; e->next=NULL; e->item=item; tail=e; } if (k==1) { current=head; e=(list*)malloc(sizeof(list)); current->prev=e; e->next=current; e->prev=NULL; e->item=item; head=e; } } int wypisz (int n) { list *current; int i; current=head; for (i=1;i
next==NULL) break; current=current->next; } return current->item; } int wstaw (int item, int n) { list *current,*e; int i; current=head; for (i=1;i
next==NULL) { e=(list*)malloc(sizeof(list)); current->next=e; e->prev=current; e->next=NULL; e->item=item; tail=e; return 0; } current=current->next; } e=(list*)malloc(sizeof(list)); current->next->prev=e; e->next=current->next; current->next=e; e->prev=current; e->item=item; return 0; } int usun (int n,int k) { list *current,*e; int i; current=head; if (k==1) { free (head); head=NULL; } else if (n==1) { current=current->next; current->prev=NULL; free (head); head=current; return 0; } else if (n==k) { current=tail->prev; current->next=NULL; free (tail); tail=current; return 0; } else { for (i=1;i
next; } current->next->next->prev=current; e=current->next; current->next=current->next->next; free (e); } return 0; } int sprawdz () { list *current; int n=0; if (head==NULL) { return 0; } else { current=head; do { current=current->next; n++; } while (current!=NULL); return n; } } void modyfikuj (int item, int n) { list *current; int i; current=head; for (i=1;i
next; } current->item=item; } void czysc () { list *current; int i; current=head; do { current->item=0; current=current->next; } while (current->next!=NULL); } void free (int k) { list *current; if (k==1) { free (head); } else { current=head; do { current=current->next; free (head); head=current; } while (current->next!=NULL); free (current); } }
Score: 0/10
Would you like to comment?
Sign up
for a free account or
sign in
(if you are already a member).
URL:
Embed:
Uploaded on 5/20/2009 3:09:33 PM
by
migiel
Programming Language:
C
Tags:
Viewed 158 times
Help
Help by email
What is Naslu?
|
About Naslu
|
Contact
|
Terms of Use
|
Downloads
|
Webprogramacion.com
|
Blog
|
Top
Links
Select language:
Español
|
English
© 2009 naslu.com