Advertisement
feihung

lista c

Jan 19th, 2015
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.18 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct struktura {
  5.     int id;
  6.     struct struktura *next;
  7. };
  8.  
  9.  
  10. void show( struct struktura *head ){
  11.     struct struktura *p;
  12.     p = head;
  13.  
  14.     while ( p != NULL ){
  15.         printf("%d ", p->id);
  16.         p = p->next;
  17.     }
  18. }
  19. void add( struct struktura **head, int id ){
  20.     struct struktura *p, *q;
  21.     if ( *head == NULL ){
  22.         p = (struct struktura*)malloc(sizeof( struct struktura ));
  23.         *head = p;
  24.         p->id = id;
  25.         p->next = NULL;
  26.     } else {
  27.         p = *head;
  28.         while ( p->next != NULL ){
  29.             p = p->next;
  30.         }
  31.         q = (struct struktura*)malloc(sizeof( struct struktura ));
  32.         p->next = q;
  33.         q->next = NULL;
  34.         q->id = id;
  35.  
  36.     }
  37. }
  38. void pushFront( struct struktura **head, int id ){
  39.     struct struktura *nowy;
  40.     nowy = (struct struktura*)malloc(sizeof( struct struktura ));
  41.     nowy->id = id;
  42.     nowy->next = *head;
  43.     *head = nowy;
  44. }
  45. int main(){
  46.     struct struktura *head;
  47.     head = NULL;
  48.  
  49.     add(&head, 1);
  50.     add(&head, 2);
  51.     add(&head, 3);
  52.     add(&head, 4);
  53.     add(&head, 5);
  54.     pushFront(&head, 6);
  55.     show(head);
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement