Concluído

linked lists

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 //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; inext; } return 1; } main() { List l; for(int i=0; i<10; i++) [url removed, login to view](i); [url removed, login to view](); /*[url removed, login to view](5); [url removed, login to view](9); [url removed, login to view](11); [url removed, login to view](300); [url removed, login to view](101); [url removed, login to view](); */ //Finds the sum of elements in the linked list [url removed, login to view](); [url removed, login to view](); //Finds the average of the elements in the linked list [url removed, login to view](); [url removed, login to view](); //Reverses the elements in the linked list cout<<"Here is the reversed list - "<<endl; [url removed, login to view](); [url removed, login to view](); //Swaps the fifth element with the 7th element cout<<"Here is the list when elements 5 and 7 are swapped - "<<endl; [url removed, login to view] ( [url removed, login to view] (5), [url removed, login to view] (7)); [url removed, login to view](); } /* 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

Habilidades: Programação C, Engenharia, MySQL, PHP, Arquitetura de software, Teste de Software

Veja mais: struct c programming, sort function c, return oriented programming, public programming, programming define, programming compiler, programming 101, pointer programming, object oriented programming class, object based programming, linked on, linked n, linked it, linked int, linked i, int size c, int i, function block programming, float programming, float c programming

Acerca do Empregador:
( 1 comentário ) United States

ID do Projeto: #2859909

Concedido a:

muresanrobert

See private message.

$4 USD em 14 dias
(62 Comentários)
4.2

2 freelancers estão ofertando em média $8 para esse trabalho

narphorium

See private message.

$11.05 USD in 14 dias
(29 Comentários)
3.6