Write a program that inserts 25 random integers from 0 to 100 in sorted order in a linked list.
The program should calculate the sum of the elements and the floating-point average of the
elements.
#include<conio.h>
#include <iostream>
# include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include <cmath>
#include<dos.h>
using namespace std;
#define MAX 100
#define MIN 0
#define COUNT 25
struct Node
{
int data;
struct Node *next;
};
struct Node* head = NULL;
void InsertData(int NewtData)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = NewtData;
new_node->next = head;
head = new_node;
}
void DisplayLinkedList(void)
{
struct Node* ptr; ptr=head;
cout<<"\n\nSorted Linked List: ";
while (ptr != NULL)
{
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
main()
{
int n,temp,Nums[COUNT];
srand(time(0));
for(n=0;n<COUNT;n++)
{
temp = rand();
while(temp<MIN || temp>MAX) temp=rand();
Nums[n] = temp;
// cout<<Nums[n]<<" ";
}
for (int i = 0; i < n; ++i)
{
for (int j = i + 1; j < n; ++j)
{
if (Nums[i] > Nums[j])
{
temp = Nums[i];
Nums[i] = Nums[j];
Nums[j] = temp;
}
}
}
for(n=0;n<COUNT;n++) InsertData(Nums[n]);
DisplayLinkedList();
}
Comments
Leave a comment