Home › Forums › C Programming › Structures and Algorithms in C++
- This topic has 6 replies, 3 voices, and was last updated 14 years, 10 months ago by GrantFinej.
- AuthorPosts
- January 6, 2010 at 8:35 pm #2236StephanDukeParticipant
Can anyone help me…?
I am beginner in this….. for example this : “One function that builds full list of numbers, values read from the keypad “!:)
- January 6, 2010 at 9:40 pm #3620GWILouisaxwzklaParticipant
could try:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303<br />/****************************************************************<br />* File Name : cgt.cpp<br />* Date : April,10,2007<br />* Comments : new project<br />* Compiler/Assembler : VC++ 6.0<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 2:59:58<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include < iostream ><br />//#include < string.h ><br />//#include < conio.h ><br />//#include < math.h ><br /><br />using namespace std;<br /><br />struct node<br />{<br /><br />int data;<br />node * next;<br />node * back;<br /><br />};<br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />void readList ( node ** list );<br />bool listsAreEqual ( node * left , node * right );<br />node * copyList ( node * list );<br />void printList ( node * list );<br />void destroyList ( node ** list );<br />bool listsContainEqualMembers ( node * left , node * right );<br /><br />//##################################################################################<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />node * head1 = 0;<br />node * head2 = 0;<br /><br />cout << "enter first list :" << endl;<br />readList ( & head1 );<br />cout << "enter second list: " << endl;<br />readList ( & head2 );<br /><br />if ( listsAreEqual ( head1 , head2 ) )<br />cout << endl << "The lists are equal" << endl;<br />else<br />cout << endl << "The lists are not equal" << endl;<br /><br />cout << endl;<br />if ( listsContainEqualMembers ( head1 , head2 ) )<br />cout << endl << "lists contain equal members" << endl;<br />else<br />cout << endl << "lists do not contain equal members " << endl;<br /><br /><br />node * copy = copyList ( head1 );<br />cout << endl << "copy of first list is: ";<br />printList ( copy );<br />destroyList ( & head1 );<br />destroyList ( & head2 );<br />destroyList ( & copy );<br />return 0 ;<br />}<br /><br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : readList<br />Parameters :<br /><br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void readList ( node ** list )<br />{<br /><br />cout << "enter an integer ( -1 to stop ) ";<br />int i;<br />cin >> i;<br />node * temp = 0;<br /><br />while ( i != -1 )<br />{<br /><br />temp = new node;<br />if ( temp == 0 )<br />{<br />cout << " allocation error" ;<br />( * list ) = 0;<br />return;<br />}<br />temp -> data = i ;<br />if ( ( * list ) == 0 )<br />{<br />( * list ) = temp;<br />temp -> next = 0;<br />temp -> back = temp;<br /><br />}<br />else<br />{<br />( * list ) -> back -> next = temp;<br />temp -> next = temp -> back = 0;<br />( * list ) -> back = temp;<br />}<br />cout << "enter a number ( -1 to stop ) ";<br />cin >> i;<br />}<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : listsAreEqual<br />Parameters :<br /><br /><br /><br />Returns: user defined type , bool<br /><br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />bool listsAreEqual ( node * left , node * right )<br />{<br /><br />bool matches = true;<br /><br />while ( left != 0 && right != 0 && matches )<br />{<br /><br />if ( left -> data != right -> data )<br />matches = false;<br />else<br />{<br />left = left -> next;<br />right = right -> next;<br />}<br />}<br />if ( ! left && ! right && matches )<br />return true;<br /><br />return false;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : copyList<br />Parameters :<br /><br /><br /><br />Returns: user defined type , node *<br /><br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />node * copyList ( node * list )<br />{<br /><br />node * temp = 0;<br />node * newList = 0;<br /><br />while ( list )<br />{<br /><br />temp = new node;<br />if ( ! temp )<br />{<br />cout << "allocation error! ";<br />return 0;<br />}<br />if ( newList == 0 )<br />{<br />newList = temp;<br />temp -> data = list -> data;<br />newList -> next = 0;<br />newList -> back = temp;<br />}<br />else<br />{<br />temp -> data = list -> data;<br />newList -> back -> next = temp;<br />newList -> back = temp;<br />temp -> next = temp -> next = 0;<br />}<br />list = list -> next;<br />}<br />return newList;<br />}<br />void printList ( node * temp )<br />{<br /><br /><br />cout << "list read is " ;<br />while ( temp )<br />{<br />cout << temp -> data << " ";<br />temp = temp -> next;<br />}<br />cout << endl;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : destroyList<br />Parameters :<br /><br /><br /><br />Returns: user defined type , void<br /><br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void destroyList ( node ** list )<br />{<br /><br /><br />node * temp = ( * list );<br />node * temp2 = 0;<br />while ( temp )<br />{<br /><br />temp2 = temp;<br />temp = temp -> next;<br />delete temp2;<br />}<br /><br />( * list ) = 0;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : listsContainEqualMembers<br />Parameters :<br /><br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />bool listsContainEqualMembers ( node * left , node * right )<br />{<br /><br /><br />node * temp1 = left;<br />node * temp2 = right;<br />bool matches ;<br />int length1 = 0 , length2 = 0;<br />int numItems1 = 0 , numItems2 = 0;<br /><br />while ( temp1 && matches )<br />{<br />temp2 = right;<br />matches = false;<br />length1 ++; //get length of first list<br />while ( temp2 && ! matches )<br />{<br />if ( temp1 -> data == temp2 -> data )<br />matches = true;<br />if ( ! matches )<br />temp2 = temp2 -> next;<br />}<br />if ( matches )<br />temp1 = temp1 -> next;<br />}<br />temp2 = right;<br />while ( temp2 )<br />{<br />length2 ++ ;<br />temp2 = temp2 -> next;<br />}<br />if ( length1 == length2 && matches )<br />return true;<br />return false;<br />}<br /><br /> - January 7, 2010 at 1:54 pm #3621StephanDukeParticipant
Thank you so much but,I’m sorry…bcz i said that i am Beginner can u write a smaller code and to explain me these code…..if it’s not a big problem for you…or some tutorial for Structures and Algorithms in C++ for Beginners…..? Thanks!
Hacking is not a CRIME….???!!!
- January 8, 2010 at 9:47 pm #3622GWILouisaxwzklaParticipant
Heres a simple version ( the items are entered into the list from the back )
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : January,8,2010<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 4:13:24 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include < iostream ><br />//#include < string.h ><br />//#include < conio.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br /><br />using namespace std;<br /><br />//node for the list<br />struct node<br />{<br />int data;<br />node * next;<br />};<br /><br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />cout << "enter numbers ( 0 to stop ) " << endl << endl;<br /><br />node * front = NULL; //front of the linked list<br />node * temp = NULL;<br />node * back = NULL;<br /><br />int input;<br /><br />cout << "enter number: ";<br />cin >> input;<br /><br />//######### READ THE LIST FROM THE KEYBOARD<br /><br />while ( input != 0 )<br />{<br />temp = new node; //make a new node in the list<br />if ( temp == NULL ) //check that node was allocated properly<br />{<br />cout << "allocation error , node not stored! " << endl;<br />continue;<br />}<br />temp -> data = input;//copy data<br />temp -> next = NULL; //new item has no next item<br />if ( front == NULL ) //if the list is empty add a new node<br />{<br />front = temp; //front of list points to temp<br />back = temp; //back of list pointer points to temp<br />}<br />else //add the item to the back of the list<br />{<br />back -> next = temp;//next item in the list is the item read<br />back = temp; //new item is the new last item<br />}<br />cout << "enter number: "; //get the next number<br />cin >> input;<br />}<br /><br /><br />cout << "Printed list: " << endl << endl;<br /><br />temp = front; //start at the front of the list<br /><br />//############ PRINT THE LIST<br /><br />while ( temp ) //while not at the end of the list<br />{<br />cout << temp -> data << " " ; //output data in list item<br />temp = temp -> next; //go to next item in the list<br />}<br /><br />//######## DELETE THE LIST<br /><br />while ( front )<br />{<br />temp = front; //save the address of the front node<br />front = front -> next; //make the next item first<br />delete temp; //deallocate memory for the old front node<br />}<br /><br /><br />return 0 ;<br />}<br /><br /><br /><br /><br /> - January 9, 2010 at 4:45 pm #3623StephanDukeParticipant
I had in exam in Structures and Algorithms in C++ for this code to wrote but i don’t know how to wrote in C++…..Pls anyone to help me :
1) Are given a days courses, a currency exchange in the penny, the course (i) = 1,2,3, … 365 for 2007. The average month found that there were higher average savings. .
Anyone for help…?
Maybe have some gramatically mistakes….sorry…bcz i don’t know so English Gramar
- January 9, 2010 at 7:12 pm #3624GWILouisaxwzklaParticipant
Not sure what your doing. Does this relate to linked lists?
- January 21, 2010 at 6:14 am #3625GrantFinejParticipant
Hello,
Since you are a beginner, so you better study the following site. which will help you understand this more better.
https://www.mycplus.com/tag/data-structure/
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.