Show Bid Request
linked lists
Bid Request Id: 21316
|
|
|
Posted by: |
zak_12000 (1 ratings)
(Software buyer rating 10)
|
Non-action Ratio: |
Very Good - 0.00%
|
Buyer Security Verifications: |
Good
|
Approved on: |
Jul 10, 2002 3:36:59 PM EDT
|
Bidding Closes: |
Jul 12, 2002 EDT
|
Viewed (by coders): |
55 times
|
Deadline: |
7/12/2002
TIME EXPIRED
|
|
|
|
Description:
1. generate a linked list based on the folwng program
2. Add two member functions to the List class performing two separate sorting operations.
3. Use the functions in step (2) to sort the linked list of step (1).
/* Object-Oriented Linked List
This program defines a linked list and inserts 10 elements in the list. There are a number of functions such as Insert, Delete, Print, and Length, which we
are already provided with. We are asked to add the following functions: Sum: adds up the numbers in the linked list. Average: finds the average of the numers in the list. Reverse: reverses the order of the elements in the linked list. Swap: swaps the contents of two cells
*/
#include <iostream.h>
//typedef int bool; // if your compiler doesn't support boolean type uncomment this
typedef int ListElement;
struct cell // the basic building block of a linked list
{
ListElement item; // the data in this cell
struct cell *next; // the pointer to link to the next cell
};
typedef struct cell* cellptr; // define the type of the pointer to a cell
class List
{
private:
cellptr header; // the header pointer of the linked list
public:
List();
~List();
bool IsListEmpty();
int Length();
bool ListInsert(ListElement);
bool ListDelete(ListElement);
cellptr Retrieve(ListElement);
void PrintList();
int SumList();
float AverageList();
bool Swap (cellptr p1, cellptr p2);
bool Reverse ();
};
List::List():header(NULL)
{
};
bool List::IsListEmpty()
{
return(header == NULL);
};
List::~List()
{
cellptr temp = header;
while(temp!=NULL)
{
header = temp->next;
temp->next = NULL;
delete temp;
temp = header;
}
header = NULL;
};
int List::Length()
{
cellptr temp = header;
int counter=0;
while(temp!=NULL)
{
counter++;
temp=temp->next;
}
return(counter);
};
bool List::ListInsert(ListElement n)
{
cellptr temp = new struct cell;
if(temp==NULL)
{
cout << "Not enough memory!" << endl;
return(0);
}
else
{
temp->item=n;
temp->next=header;
header=temp;
return(1);
}
}
continues on next page
Deliverables: bool List::ListDelete(ListElement n)
{
cellptr temp1 = header, temp2;
if(temp1==NULL)
{
cout << "Empty list!" << endl;
return(0);
}
else if(temp1 -> item==n)
{
header=temp1 -> next;
temp1->next=NULL;
delete temp1;
return(1);
}
else
{
temp2=temp1;
temp1=temp1->next;
while((temp1 != NULL)&&(temp1 -> item != n))
{
temp2=temp1;
temp1=temp1->next;
}
if(temp1==NULL)
{
cout << "Element " << n << " not found!" << endl;
return(0);
}
else
{
temp2->next = temp1->next;
temp1->next=NULL;
delete temp1;
return(1);
}
}
}
// The retrieve function locates a list element in the list and returns
// the pointer to the element. If the element is not found it returns NULL.
cellptr List::Retrieve(ListElement n)
{
cellptr temp = header;
if(temp==NULL)
{
cout << "Empty list!" << endl;
return(NULL);
}
else
{
while((temp != NULL)&&(temp -> item != n))
temp=temp->next;
if(temp==NULL)
{
cout << "Element " << n << " not found!" << endl;
return(NULL);
}
else
return(temp);
}
}
void List::PrintList()
{
cellptr temp = header;
cout << "Showing the list:" << endl;
while(temp!=NULL)
{
cout << temp -> item << endl;
temp=temp->next;
}
}
//Finds the sum of elements in the linked list
int List::SumList()
{
int sum;
cellptr temp = header;
sum = 0;
while(temp != NULL)
{
sum = sum + temp -> item;
temp = temp -> next;
}
cout<< "The sum of the elements in the linked list is: ";
cout<<sum<<endl;
return sum;
}
//Finds the average of elements in the linked list
float List::AverageList()
{
float Avg;
Avg = float (SumList())/Length();
cout<<"The list average is :"<<Avg<<endl;
return Avg;
}
//Swaps the element pointed by p1 with that pointed by p2
bool List::Swap ( cellptr p1, cellptr p2)
{
cellptr temp = header;
int temporary;
temporary = p2 -> item;
p2 -> item = p1 -> item;
p1 -> item = temporary;
return 1;
}
//Reverses the elements in the linked list
bool List::Reverse ()
{
cellptr temp = header;
int size = Length(), i;
for (i=0; i<size/2; i++)
{
Swap(Retrieve(i), Retrieve(size-1-i));
temp = temp ->next;
}
return 1;
}
main()
{
List l;
for(int i=0; i<10; i++)
l.ListInsert(i);
l.PrintList();
/*l.ListDelete(5);
l.ListDelete(9);
l.ListDelete(11);
l.ListInsert(300);
l.ListInsert(101);
l.PrintList();
*/
//Finds the sum of elements in the linked list
l.SumList();
l.PrintList();
//Finds the average of the elements in the linked list
l.AverageList();
l.PrintList();
//Reverses the elements in the linked list
cout<<"Here is the reversed list - "<<endl;
l.Reverse();
l.PrintList();
//Swaps the fifth element with the 7th element
cout<<"Here is the list when elements 5 and 7 are swapped - "<<endl;
l.Swap ( l.Retrieve (5), l.Retrieve (7));
l.PrintList();
}
/*
Showing the list:
0
1
2
3
4
5
6
7
8
9
Here is the list when elements 5 and 7 are swapped -
Showing the list:
0
1
2
3
4
7
6
5
8
9
*/
temp->next=header
Platform:
win win 2000 or win 95
Must be 100% finished and received by buyer on:
Jul 12, 2002 EDT
Deadline legal notes: All times are expressed in the time zone of the site EDT (UT - 5). If the buyer omitted a time, then the deadline is 11:59:59 PM EDT on the indicated date.
Remember that contacting the other party outside of the site (by email, phone, etc.) on all business projects < $500 (before the buyer's money is escrowed) is a violation of both the software buyer and seller agreements.
We monitor all site activity for such violations and can instantly expel transgressers on the spot, so we thank you in advance for your cooperation.
If you notice a violation please help out the site and report it. Thanks for your help.
|
|
Bidding/Comments:
|
All monetary amounts on the site are in United States dollars.
Rent a Coder is a closed auction, so coders can only see their own bids and comments. Buyers can view every posting made on their bid requests. |
See all rejected bids (and all comments)
Name |
Bid Amount |
Date |
Coder Rating |
|
|
This bid was accepted by the buyer!
|
$7 (USD)
|
Jul 10, 2002 4:30:57 PM EDT
|
8.51
(Superb)
|
|
|
Can deliver it to you in a few hours as i already have it implemented. I am a part time computer science teacher at my university. I will give you clean, error free and well commented source code. |
|
|
|
|
|