Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.15 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. typedef struct node
  6. {
  7.     unsigned char type;
  8.     union
  9.     {
  10.         struct {
  11.             char name[21];
  12.             int id;
  13.         }   description;
  14.  
  15.         struct {
  16.             double price;
  17.             int amount;
  18.         } stock;
  19.     } product;
  20.  
  21.     struct node *next;
  22. } Node;
  23. typedef Node *Nodeptr;
  24.  
  25. Nodeptr L;
  26.  
  27. void insert(char *cname, int id, double price, int amount)
  28. {
  29.     Nodeptr n;
  30.     n = (Node *) malloc(sizeof(Node *));
  31.     n->next = L;
  32.     n->type = 's';
  33.     strcpy(n->product.description.name, cname);
  34.     n->product.description.id = id;
  35.     L=n;
  36. }
  37.  
  38. void printlist(Nodeptr L)
  39. {
  40.     while (L!=NULL){
  41.         printf("%s %d \n", L->product.description.name, L->product.description.id);
  42.         L = L->next;
  43.     }
  44. }
  45.  
  46. int main()
  47. {
  48.     FILE *f;
  49.     L = NULL;
  50.     f = fopen("date.in", "r");
  51.  
  52.     int n,i;
  53.     int id, amount;
  54.     char cname[21];
  55.     double price;
  56.     fscanf(f,"%d", &n);
  57.     for(i=0; i<n; i++)
  58.     {
  59.         fscanf(f, "%s %d %f %d", cname, &id, &price, &amount);
  60.         insert(cname, id, price, amount);
  61.     }
  62.     printlist(L);
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement