Guest User

Untitled

a guest
Oct 17th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. /*
  2.  * List.c
  3.  *
  4.  *  Created on: Sep 5, 2012
  5.  *      Author: michal
  6.  */
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. typedef struct node node;
  10. struct node {
  11.     int data;
  12.     node *next;
  13.     node *previus;
  14.  
  15. };
  16. struct list {
  17.     int countNodes;
  18.     int sum;
  19.     node *head;
  20.     node *lastNode;
  21. };
  22. typedef struct node node;
  23. typedef struct list list;
  24.  
  25. void newList(list *l)
  26. {
  27.     l->sum=0;
  28.     l->countNodes=0;
  29. }
  30. void insert(int x,list* l)
  31. {
  32.     node *temp;
  33.     temp=malloc( sizeof(*temp));
  34.     temp->data=x;
  35.     temp->next=l->head;
  36.     if(l->head==NULL)
  37.         temp->previus=NULL;
  38.     else
  39.         l->head->previus=temp;
  40.     l->head=temp;
  41.     ++(l->countNodes);
  42.     if(l->countNodes==1)
  43.     {
  44.         l->lastNode=temp;
  45.     }
  46.     l->sum+=x;
  47. }
  48. void deletAll(list*l)
  49. {
  50.     node *d;
  51.     d=l->head;
  52.     while(l!=0)
  53.     {
  54.         free(d);
  55.         d=l->head;
  56.     }
  57. }
  58. void deletNode(node *n,list*l)
  59. {
  60.     l->sum-=n->data;
  61.  
  62.     node*t=n->previus;
  63.     t->next=n->next;
  64.     --l->countNodes;
  65. }
  66. int getSize(list *l)
  67. {
  68.     return l->countNodes;
  69. }
  70. node *getHead(list *l)
  71. {
  72.     return l->head;
  73. }
  74. node *getLast(list *l)
  75. {
  76.     return l->lastNode;
  77. }
  78. node *getNext(node *n)
  79. {
  80.     return n->next;
  81. }
  82. node *getBefor(node *n)
  83. {
  84.     return n->previus;
  85. }
  86. float getAvg(list*l)
  87. {
  88.     return (float)(l->sum)/(float)(l->countNodes);
  89. }
  90. void show(list *l)
  91. {
  92.     node *t=l->head;
  93.     while(t!=NULL)
  94.     {
  95.         printf("%d \n",t->data);
  96.         t=t->next;
  97.     }
  98. }
  99. main()
  100. {
  101.     list *l=malloc( sizeof(*l));
  102.     newList(l);
  103.     /*list *l2=malloc( sizeof(*l2));
  104.         newList(l2);*/
  105.     int i=0;
  106.     for(;i<10;++i)
  107.     {
  108.         insert(i,l);
  109.     }
  110.         show(l);
  111.         //printf("%d/n",l->sum);
  112.         //deletNode(l->head->next,l);
  113.         printf("%f",getAvg(l));
  114.         deletAll(l);
  115.         show(l);
  116.     //node *t =n->next->next;
  117.     //deletAspecificNode(t);
  118.     //show();
  119.     //printf("%f",getAvg());
  120.  
  121.     return 0;
  122. }
Add Comment
Please, Sign In to add comment