Home Forums C Programming Structures and Algorithms in C++

Viewing 6 reply threads
  • Author
    Posts
    • #2236
      StephanDuke
      Participant

      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 “!

      :)

    • #3620
      GWILouisaxwzkla
      Participant

      could try:


      /****************************************************************
      * File Name : cgt.cpp
      * Date : April,10,2007
      * Comments : new project
      * Compiler/Assembler : VC++ 6.0
      *
      *
      *
      *
      *
      * Program Shell Generated At: 2:59:58
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


      #include < iostream >
      //#include < string.h >
      //#include < conio.h >
      //#include < math.h >

      using namespace std;

      struct node
      {

      int data;
      node * next;
      node * back;

      };


      //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@

      void readList ( node ** list );
      bool listsAreEqual ( node * left , node * right );
      node * copyList ( node * list );
      void printList ( node * list );
      void destroyList ( node ** list );
      bool listsContainEqualMembers ( node * left , node * right );

      //##################################################################################

      //main function ******************************

      int main ( )
      {

      node * head1 = 0;
      node * head2 = 0;

      cout << "enter first list :" << endl;
      readList ( & head1 );
      cout << "enter second list: " << endl;
      readList ( & head2 );

      if ( listsAreEqual ( head1 , head2 ) )
      cout << endl << "The lists are equal" << endl;
      else
      cout << endl << "The lists are not equal" << endl;

      cout << endl;
      if ( listsContainEqualMembers ( head1 , head2 ) )
      cout << endl << "lists contain equal members" << endl;
      else
      cout << endl << "lists do not contain equal members " << endl;


      node * copy = copyList ( head1 );
      cout << endl << "copy of first list is: ";
      printList ( copy );
      destroyList ( & head1 );
      destroyList ( & head2 );
      destroyList ( & copy );
      return 0 ;
      }


      /******************************* FUNCTION DEFINITION ******************************

      Name : readList
      Parameters :



      Returns: Void type
      Comments:



      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void readList ( node ** list )
      {

      cout << "enter an integer ( -1 to stop ) ";
      int i;
      cin >> i;
      node * temp = 0;

      while ( i != -1 )
      {

      temp = new node;
      if ( temp == 0 )
      {
      cout << " allocation error" ;
      ( * list ) = 0;
      return;
      }
      temp -> data = i ;
      if ( ( * list ) == 0 )
      {
      ( * list ) = temp;
      temp -> next = 0;
      temp -> back = temp;

      }
      else
      {
      ( * list ) -> back -> next = temp;
      temp -> next = temp -> back = 0;
      ( * list ) -> back = temp;
      }
      cout << "enter a number ( -1 to stop ) ";
      cin >> i;
      }

      return;
      }
      /******************************* FUNCTION DEFINITION ******************************

      Name : listsAreEqual
      Parameters :



      Returns: user defined type , bool

      Comments:



      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      bool listsAreEqual ( node * left , node * right )
      {

      bool matches = true;

      while ( left != 0 && right != 0 && matches )
      {

      if ( left -> data != right -> data )
      matches = false;
      else
      {
      left = left -> next;
      right = right -> next;
      }
      }
      if ( ! left && ! right && matches )
      return true;

      return false;
      }
      /******************************* FUNCTION DEFINITION ******************************

      Name : copyList
      Parameters :



      Returns: user defined type , node *

      Comments:



      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      node * copyList ( node * list )
      {

      node * temp = 0;
      node * newList = 0;

      while ( list )
      {

      temp = new node;
      if ( ! temp )
      {
      cout << "allocation error! ";
      return 0;
      }
      if ( newList == 0 )
      {
      newList = temp;
      temp -> data = list -> data;
      newList -> next = 0;
      newList -> back = temp;
      }
      else
      {
      temp -> data = list -> data;
      newList -> back -> next = temp;
      newList -> back = temp;
      temp -> next = temp -> next = 0;
      }
      list = list -> next;
      }
      return newList;
      }
      void printList ( node * temp )
      {


      cout << "list read is " ;
      while ( temp )
      {
      cout << temp -> data << " ";
      temp = temp -> next;
      }
      cout << endl;
      }
      /******************************* FUNCTION DEFINITION ******************************

      Name : destroyList
      Parameters :



      Returns: user defined type , void

      Comments:



      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      void destroyList ( node ** list )
      {


      node * temp = ( * list );
      node * temp2 = 0;
      while ( temp )
      {

      temp2 = temp;
      temp = temp -> next;
      delete temp2;
      }

      ( * list ) = 0;
      }
      /******************************* FUNCTION DEFINITION ******************************

      Name : listsContainEqualMembers
      Parameters :



      Returns: Void type
      Comments:



      ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
      bool listsContainEqualMembers ( node * left , node * right )
      {


      node * temp1 = left;
      node * temp2 = right;
      bool matches ;
      int length1 = 0 , length2 = 0;
      int numItems1 = 0 , numItems2 = 0;

      while ( temp1 && matches )
      {
      temp2 = right;
      matches = false;
      length1 ++; //get length of first list
      while ( temp2 && ! matches )
      {
      if ( temp1 -> data == temp2 -> data )
      matches = true;
      if ( ! matches )
      temp2 = temp2 -> next;
      }
      if ( matches )
      temp1 = temp1 -> next;
      }
      temp2 = right;
      while ( temp2 )
      {
      length2 ++ ;
      temp2 = temp2 -> next;
      }
      if ( length1 == length2 && matches )
      return true;
      return false;
      }

    • #3621
      StephanDuke
      Participant

      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….???!!!

    • #3622
      GWILouisaxwzkla
      Participant

      Heres a simple version ( the items are entered into the list from the back )


      /****************************************************************
      * File Name : c:programstempCG.cpp
      * Date : January,8,2010
      * Comments : new project
      * Compiler/Assembler : Visual C++ 6.0
      * Modifications :
      *
      *
      *
      *
      *
      * Program Shell Generated At: 4:13:24 p.m.
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


      #include < iostream >
      //#include < string.h >
      //#include < conio.h >
      //#include < math.h >
      //#include < iomanip >
      //#include < ctype.h >

      using namespace std;

      //node for the list
      struct node
      {
      int data;
      node * next;
      };



      //main function ******************************

      int main ( )
      {

      cout << "enter numbers ( 0 to stop ) " << endl << endl;

      node * front = NULL; //front of the linked list
      node * temp = NULL;
      node * back = NULL;

      int input;

      cout << "enter number: ";
      cin >> input;

      //######### READ THE LIST FROM THE KEYBOARD

      while ( input != 0 )
      {
      temp = new node; //make a new node in the list
      if ( temp == NULL ) //check that node was allocated properly
      {
      cout << "allocation error , node not stored! " << endl;
      continue;
      }
      temp -> data = input;//copy data
      temp -> next = NULL; //new item has no next item
      if ( front == NULL ) //if the list is empty add a new node
      {
      front = temp; //front of list points to temp
      back = temp; //back of list pointer points to temp
      }
      else //add the item to the back of the list
      {
      back -> next = temp;//next item in the list is the item read
      back = temp; //new item is the new last item
      }
      cout << "enter number: "; //get the next number
      cin >> input;
      }


      cout << "Printed list: " << endl << endl;

      temp = front; //start at the front of the list

      //############ PRINT THE LIST

      while ( temp ) //while not at the end of the list
      {
      cout << temp -> data << " " ; //output data in list item
      temp = temp -> next; //go to next item in the list
      }

      //######## DELETE THE LIST

      while ( front )
      {
      temp = front; //save the address of the front node
      front = front -> next; //make the next item first
      delete temp; //deallocate memory for the old front node
      }


      return 0 ;
      }




    • #3623
      StephanDuke
      Participant

      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

    • #3624
      GWILouisaxwzkla
      Participant

      Not sure what your doing. Does this relate to linked lists?

    • #3625
      GrantFinej
      Participant

      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/

Viewing 6 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.