Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * List.c
- *
- * Created on: Sep 5, 2012
- * Author: michal
- */
- #include <stdlib.h>
- #include <stdio.h>
- typedef struct node node;
- struct node {
- int data;
- node *next;
- node *previus;
- };
- struct list {
- int countNodes;
- int sum;
- node *head;
- node *lastNode;
- };
- typedef struct node node;
- typedef struct list list;
- void newList(list *l)
- {
- l->sum=0;
- l->countNodes=0;
- }
- void insert(int x,list* l)
- {
- node *temp;
- temp=malloc( sizeof(*temp));
- temp->data=x;
- temp->next=l->head;
- if(l->head==NULL)
- temp->previus=NULL;
- else
- l->head->previus=temp;
- l->head=temp;
- ++(l->countNodes);
- if(l->countNodes==1)
- {
- l->lastNode=temp;
- }
- l->sum+=x;
- }
- void deletAll(list*l)
- {
- node *d;
- d=l->head;
- while(l!=0)
- {
- free(d);
- d=l->head;
- }
- }
- void deletNode(node *n,list*l)
- {
- l->sum-=n->data;
- node*t=n->previus;
- t->next=n->next;
- --l->countNodes;
- }
- int getSize(list *l)
- {
- return l->countNodes;
- }
- node *getHead(list *l)
- {
- return l->head;
- }
- node *getLast(list *l)
- {
- return l->lastNode;
- }
- node *getNext(node *n)
- {
- return n->next;
- }
- node *getBefor(node *n)
- {
- return n->previus;
- }
- float getAvg(list*l)
- {
- return (float)(l->sum)/(float)(l->countNodes);
- }
- void show(list *l)
- {
- node *t=l->head;
- while(t!=NULL)
- {
- printf("%d \n",t->data);
- t=t->next;
- }
- }
- main()
- {
- list *l=malloc( sizeof(*l));
- newList(l);
- /*list *l2=malloc( sizeof(*l2));
- newList(l2);*/
- int i=0;
- for(;i<10;++i)
- {
- insert(i,l);
- }
- show(l);
- //printf("%d/n",l->sum);
- //deletNode(l->head->next,l);
- printf("%f",getAvg(l));
- deletAll(l);
- show(l);
- //node *t =n->next->next;
- //deletAspecificNode(t);
- //show();
- //printf("%f",getAvg());
- return 0;
- }
Add Comment
Please, Sign In to add comment