I Need help on writing a program using linked lists. Please read carefullly sinc
ID: 3558683 • Letter: I
Question
I Need help on writing a program using linked lists. Please read carefullly since this question has three parts(a, b and c). Since in previous help many users left either section b or c out.
PART A
Write a program that allows the user to enter the las names of five candidates in a lcoal election and the votes received by each candidate. The program should then output each candidate's name, the votes received by that candidate, and the percentage of the total votes received by that candidat(write an appropriate formula to calculate the % of total votes). Your program shoud also output the winner of the election. A Sample output is:
Candidate Votes Received % of Total Votes
Johnson 5000 25.91
Miller 4000 20.72
Duffy 6000 31.09
Robinson 2500 12.95
Ashton 1800 9.33
Total 19300
The winner of the election is Duffy.
PART B
After completing part A, use linked list to display the candidate's name, votes received and % of total votes for each candidate, from highest to lowest.
PART C
After a recount, it was discovered that Robinson received 500 more votes than recorded. Use a binary search to find Robinson and update his vote total. It was also reported that Miller's final vote count is 3775 and 4000. Use binary search to find MIller and update his final vote count as well. Display the final results updated.
Explanation / Answer
Program Code:
// linkdList.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<string>
#include<iostream>
using namespace std;
class LinkList
{
private:
struct Node
{
int data;
Node* link;
}*p;
struct Node2
{
string st;
Node2* links;
}*s;
public:
LinkList();
~LinkList();
void Print(); // Prints the contents of linkedlist
void Append(int num); // Adds a new node at the end of the linkedlist
void Append(string st);
};
LinkList::LinkList()
{
p = NULL;
}
LinkList::~LinkList()
{
if (p == NULL)
return;
Node* tmp;
while(p != NULL)
{
tmp = p->link ;
delete p;
p = tmp;
}
}
// Prints the contents of linkedlist
void LinkList::Print()
{
if (p == NULL)
{
cout<< "EMPTY";
return;
}
//Traverse
Node* tmp = p;
while(tmp != NULL)
{
cout<<tmp->data<<endl;
tmp = tmp->link ;
}
Node2* tmp1 = s;
while(tmp != NULL)
{
cout<<tmp1->st<<endl;
tmp1 = tmp1->links ;
}
}
// Adds a new node at the end of the linkedlist
void LinkList::Append(int num)
{
Node *newNode;
newNode = new Node;
newNode->data = num;
newNode->link = NULL;
if(p == NULL)
{
//create first node
p = newNode;
}
else
{
//Traverse
Node *tmp = p;
while(tmp->link != NULL)
{
tmp = tmp->link;
}
//add node to the end
tmp->link = newNode;
}
}
void LinkList::Append(string st)
{
Node2 *newNode;
newNode = new Node2;
newNode->st = st;
newNode->links = NULL;
if(s == NULL)
{
//create first node
s = newNode;
}
else
{
//Traverse
Node2 *tmp1 = s;
while(tmp1->links != NULL)
{
tmp1 = tmp1->links;
}
//add node to the end
tmp1->links = newNode;
}
}
int main()
{
string names[5];
int votes[5];
double pervotes[5];
int total=0;
cout<<"Enter the last names of five candidates: "<<endl;
for(int i=0;i<5;i++)
cin>>names[i];
cout<<endl;
cout<<"Entre the number of votes earned by each candidate: "<<endl;
for(int i=0;i<5;i++)
cin>>votes[i];
cout<<endl;
for(int i=0;i<5;i++)
total+=votes[i];
cout<<"Total= "<<total<<endl;
for(int i=0;i<5;i++)
pervotes[i]=(votes[i]/total)*100;
cout<<"Candidate Votes % of total Votes"<<endl;
for(int i=0;i<5;i++)
cout<<names[i]<<" "<<votes[i]<<" "<<pervotes[i]<<endl;
LinkList* p = new LinkList();
for(int i=0;i<5;i++)
{
p->Append(names[i]);
p->Append(votes[i]);
p->Print();
}
system("pause");
return 0;
}
Sample Output:
Enter the last names of five candidates:
we
er
wer
df
sdf
Entre the number of votes earned by each candidate:
324
56
3
45
56
Total= 484
Candidate Votes % of total Votes
we 324 0
er 56 0
wer 3 0
df 45 0
sdf 56 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.