#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NODE struct node
#define HEAD struct head
NODE
{
int data;
NODE *next;
};
HEAD
{
int count;
NODE *first;
NODE *last;
NODE *sum;
NODE *max;
NODE *min;
NODE *avg;
};
void init(HEAD *head)
{
head->count = 0;
head->first = NULL;
head->last = NULL;
head->sum = NULL;
}
void addNode(HEAD *head, int data)
{
HEAD *s;
NODE *b;
int min, n;
int avg;
s = (HEAD *) malloc (sizeof(HEAD));
b->data = n;
if (data < min)
min = data;
b->next = s->first;
s->first = b;
s->sum += b->data;
s->count ++;
s->avg = s->sum / s->count;
}
void appendNode(HEAD *head, int data)
{
HEAD *q;
NODE *b;
int max, n;
int avg;
q = (HEAD *) malloc (sizeof(HEAD));
b->data = n;
b->next = NULL;
if (data > max)
max = data;
if (emptyList(head))
head->first = b;
else
q->last->next = b;
q->last = b;
q->count ++;
q->sum += b->data;
q->avg = q->sum / q->count;
}
int emptyList(HEAD *head)
{
if (head->count==0)
return 1;
else
return 0;
}
int listCount(HEAD *head)
{
return (head->count);
}
void printList(HEAD *head)
{
int i;
NODE *prnNode;
prnNode = head->first;
for (i=1; i<=head->count; i++)
{
printf("%d\n", prnNode->data);
prnNode = prnNode->next;
}
}
void prnList(HEAD *head)
{
NODE *prnNode;
prnNode = head->first;
while (prnNode != NULL)
{
printf("%d\n", prnNode->data);
prnNode = prnNode->next;
}
}
void main()
{
HEAD *q, *s;
int i, n;
q = (HEAD *) malloc (sizeof(HEAD));
s = (HEAD *) malloc (sizeof(HEAD));
init(q);
init(s);
srand(time(NULL));
for (i=1; i<20; i++)
{
n = rand() % 201 + 100;
if(n % 2 == 0)
appendNode(q, n);
else
addNode(s, n);
}
printf("\n Queue: \n");
prnList(q);
printf("\n Stack: \n");
prnList(s);
printf("\n\n\t count \t max \t min \t avg \n");
printf("Queue:\t %d \t %d \t \t %.2f \n", q->count, q->max, q->avg);
printf("Stack:\t %d \t %d \t \t %.2f \n", s->count, s->min, s->avg);
printf("\n\n");
}
求帮忙看看怎么改错啊!谢谢你啦!
asd8532
相关分类