monoteen

Struct_Pointer

Oct 8th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int main(void)
  5. {
  6.     struct list_item {
  7.         char item;
  8.         struct list_item *link;
  9.     };
  10.  
  11.     struct list_item *header;
  12.     struct list_item item1;
  13.     struct list_item *temp;
  14.  
  15.     header = &item1;
  16.  
  17.     item1.item = 'A';
  18.     item1.link = NULL;
  19.  
  20.     printf("item : %c \n", item1.item);
  21.  
  22.     (*header).item = 'B';
  23.     printf("item : %c \n", item1.item);
  24.  
  25.     header->item = 'A';
  26.     printf("item : %c \n", item1.item);
  27.  
  28.     temp = (struct list_item *) malloc(sizeof(struct list_item));
  29.     temp->item = 'B';
  30.     temp->link = header;
  31.     header = temp;
  32.  
  33.     temp = (struct list_item *) malloc(sizeof(struct list_item));
  34.     temp->item = 'C';
  35.     temp->link = header;
  36.     header = temp;
  37.  
  38.     temp = (struct list_item *) malloc(sizeof(struct list_item));
  39.     temp->item = 'U';
  40.     temp->link = header;
  41.     header = temp;
  42.  
  43.     temp = (struct list_item *) malloc(sizeof(struct list_item));
  44.     temp->item = 'D';
  45.     temp->link = header;
  46.     header = temp;
  47.  
  48.     temp = header;
  49.  
  50.     printf("\nheader -> ");
  51.     while(temp != NULL)
  52.     {
  53.         printf("item : %c -> ", temp->item);
  54.         temp = temp->link;
  55.     }
  56.     printf(" NULL \n");
  57.    
  58.     printf("item : %c \n", header->link->item);
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment