circular link list in c++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #61
    Originally posted by saraSS
    how do I do this? pointers are not my good subjets lol
    But you had it already (almost):

    Code:
    struct headernode{
            char x;
            listNodeType *next;
    };
    Replace listNodeType by ListNodeClass. Note that you have to change all access to a list, since you have an array of header structs now...

    Comment

    • saraSS
      New Member
      • Oct 2006
      • 69

      #62
      going in circles here cant make it work with those pointers another way to do that?

      Comment

      • arne
        Recognized Expert Contributor
        • Oct 2006
        • 315

        #63
        Originally posted by saraSS
        going in circles here cant make it work with those pointers another way to do that?
        Since the vector has a constant number of elements, you could establish some mapping:

        a is 0
        b is 1
        c is 2 and so on.

        If you need set b, you know its the second (index 1) in your vector. But do not change the vector then or you will run in trouble ;-)

        Alternatively, you could extend your nodes by another member. Instead of storing the identifier in a header you can store it in every node of the list. This is somewhat ugly, but you can go on with your current design ...

        Comment

        • saraSS
          New Member
          • Oct 2006
          • 69

          #64
          I dont know nothing is working

          Comment

          • saraSS
            New Member
            • Oct 2006
            • 69

            #65
            class ListNodeClass
            {
            private:
            ItemType Info;
            ListNodeClass * Next;
            public:
            // First, the constructor:
            ListNodeClass(c onst ItemType & Item, ListNodeClass * NextPtr = NULL):
            Info(Item), Next(NextPtr)
            {
            };
            void GetInfo(ItemTyp e & TheInfo) const;
            friend class ListClass; // very convenient to allow this
            };

            struct headernode
            {
            char x;
            ListClass *next;
            };

            class ListClass
            {
            private:
            ListNodePtr GetNode(const ItemType & Item,
            ListNodePtr NextPtr = NULL);
            void FreeNode(ListNo dePtr NodePtr);
            void ClearList();
            // Next 3 are sometimes made into protected fields:
            ListNodePtr Front, Rear;
            int Count;
            public:
            // constructor:
            ListClass();
            // destructor:
            ~ListClass();
            int NumItems() const;
            bool Empty() const;
            void InsertRear(cons t ItemType & Item);
            void printing();
            ItemType RemoveFront();
            ListNodePtr Find(const ItemType & Item) const;

            };
            int main()
            {
            int i,m;
            char postfix[80];
            char listname = 'a';

            cin>>arraysize;
            ListClass ListA[arraysize],*L;
            L=ListA;
            for( i = 0; i <arraysize; i++ )
            {
            while(cin>>numb er && number!=-1)
            {
            ListA[i].InsertRear(num ber);
            }
            cout<<ListA[i].NumItems()<<" ";
            ListA[i].printing();
            cout<<endl;
            }
            return 0;
            }

            this is almost what I have now and but cant make it work dont know why?and I have tried everyway you told me what I'm doing wrong

            Comment

            • arne
              Recognized Expert Contributor
              • Oct 2006
              • 315

              #66
              this is almost what I have now and but cant make it work dont know why?and I have tried everyway you told me what I'm doing wrong
              I have now used your code for a slim version. It's implementing a singly-linked, non-circular(!) list. All you can currently do is adding elements and print the content of the Info member.

              Use this as a starting point. Extend the list with all features you need (circularity, removing nodes, ...).

              Hope this is helpful :)


              Code:
              #include <iostream>
              #include <string>
              
              using namespace std;
              
              #define ItemType int
              
              // the definition of a list node
              class ListNodeClass
              {
              public:
              	// the data of a node
              	ItemType Info;
              
              	// the pointer to the next node
              	ListNodeClass *Next;
              };
              
              
              // the definition of a list (header!)
              class ListClass
              {
              private:
                      // entry to the list
              	ListNodeClass *Front;
              
              	// number of elements
              	int Count;           
              
              	// the ID to identify the set
                      char ID;  
              
              	void FreeNode( ListNodeClass* );
              
              public:
              	ListClass( char id ) : Front(0), Count(0), ID(id) {}
              
              	void InsertFront( ListNodeClass& );
              	void InsertRear( ListNodeClass& );
              
              	void RemoveFront();
              
              	void ClearList();
              	int NumItems() const { return Count; }
              	bool Empty() const;
              	void Printing();
              
              	ListNodeClass* Find(const ItemType & Item) const;
              
              };
              
              void ListClass::InsertFront( ListNodeClass& node ) {
              
              	node.Next = Front;
              	Front = &node;	
              	Count++;
              }
              
              void ListClass::Printing() {
              
              	ListNodeClass *ptr = Front;
              
              	cout << "This is set " << ID << " (" << Count << " elements): ";
              
              	while( ptr!=NULL ) {
              
              		cout << ptr->Info << " ";
              		ptr = ptr->Next;
              	}
              
              	cout << endl;
              }
              
              int main( void )
              {
              	ListNodeClass *node;
              
              	// list a
              	ListClass list_a( 'a' );
              	
              	node = new ListNodeClass;
              	node->Info = 21;
              	list_a.InsertFront( *node );
              
              	node = new ListNodeClass;
              	node->Info = 33;
              	list_a.InsertFront( *node );
              
              	list_a.Printing();
              
              	// list b
              	ListClass list_b( 'b' );
              	
              	node = new ListNodeClass;
              	node->Info = 3;
              	list_b.InsertFront( *node );
              
              	node = new ListNodeClass;
              	node->Info = 2;
              	list_b.InsertFront( *node );
              
              	node = new ListNodeClass;
              	node->Info = 1;
              	list_b.InsertFront( *node );
              
              	list_b.Printing();
              
              	return 0;
              }

              Comment

              • saraSS
                New Member
                • Oct 2006
                • 69

                #67
                thanks gonna try that

                Comment

                • saraSS
                  New Member
                  • Oct 2006
                  • 69

                  #68
                  looks like we were trying the same thing
                  I add 1 to the arraysize and try to put the letter in the first node but getting like 9 10 11 12 13 14 instead of a b c d e f how can I fix that? this is what I did

                  char listname = 97;

                  ListClass ListA[arraysize+1];

                  for( i = 1; i <arraysize+1; i++ )
                  {
                  while(cin>>numb er && number!=-1)
                  {
                  ListA[0].InsertRear((in t)listname++);
                  ListA[i].InsertRear(num ber);
                  }
                  cout<<ListA[i].NumItems()<<" ";
                  ListA[i].printing();
                  cout<<endl;
                  }
                  but getting
                  1st column the number of elemets of every cirlist
                  2nd is supose to be the a b c d e f
                  after that is every list
                  10 9 1 3 5 7 9 11 13 15 17 19
                  10 10 2 4 6 8 10 12 14 16 18 20
                  20 11 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
                  7 12 0 3 6 9 12 15 18
                  7 13 1 4 7 10 13 16 19
                  7 14 2 5 8 11 14 17 20

                  Comment

                  • saraSS
                    New Member
                    • Oct 2006
                    • 69

                    #69
                    I try 'a' instead of 97 and got the same also

                    Comment

                    • arne
                      Recognized Expert Contributor
                      • Oct 2006
                      • 315

                      #70
                      Originally posted by saraSS
                      I try 'a' instead of 97 and got the same also
                      You try to store the list's identifier in a list element? Why? That's what the header struct (ListClass) is for. It stores the number of elements and the ID. The list nodes should only store the 'data' (Info member) and the 'Next' node pointer. When printing the list, you get the ID and the number of elements from the ListClass and the data from the ListNodeClass. Please have a look on the code I posted.

                      Or did I get you wrong?

                      Comment

                      • saraSS
                        New Member
                        • Oct 2006
                        • 69

                        #71
                        It looks like I get that wrong also I need an x where x is a lower-case symbol in the range ‘a’ . . . ‘z’ to indicate that a copy should be made of an input set. This copy will be an ordered singly-linked circular list with a header. A pointer to the header of the cloned list is to be pushed to the stack. ‘a’ indicates that the first
                        input set should be copied, ‘b’ indicates that the second input set should be copied, and so on.

                        Comment

                        • arne
                          Recognized Expert Contributor
                          • Oct 2006
                          • 315

                          #72
                          Originally posted by saraSS
                          It looks like I get that wrong also I need an x where x is a lower-case symbol in the range ‘a’ . . . ‘z’ to indicate that a copy should be made of an input set. This copy will be an ordered singly-linked circular list with a header. A pointer to the header of the cloned list is to be pushed to the stack. ‘a’ indicates that the first
                          input set should be copied, ‘b’ indicates that the second input set should be copied, and so on.
                          You mean you don't understand this or your implementation doesn't work?

                          Comment

                          • saraSS
                            New Member
                            • Oct 2006
                            • 69

                            #73
                            I did not understand this part before and was trying something harder that what is suppose to be but the pointer thing still messing me up

                            Comment

                            • saraSS
                              New Member
                              • Oct 2006
                              • 69

                              #74
                              what I'm trying to do now is a stackclass where i'm gonna have all the fuction to use with the stacklike this
                              class stackClass
                              {
                              public:
                              stackClass();// constructor:
                              ~stackClass();// destructor:
                              void Initialize();
                              void SetExpression(c har *str);
                              void Push(char c);
                              char Pop();
                              void Evaluate();
                              };

                              and when I'm evalueting the expression I check for the letter and make a copy
                              'maybe'
                              l=strlen(eval);
                              ListNodePtr tmp;
                              for(i=0;i<l;i++ )
                              {
                              if(eval[i]>=97 && eval[i]<=122)
                              {
                              //tmp=CopyList(ev al[i]);
                              Push(eval[i]);
                              cout<<eval[i];
                              }
                              else
                              {
                              A=Pop( );
                              B=Pop( );
                              switch(eval[i])
                              {
                              case 'U': break;
                              case 'I': break;
                              case '-': break;
                              }
                              what do you thing?

                              Comment

                              • arne
                                Recognized Expert Contributor
                                • Oct 2006
                                • 315

                                #75
                                Originally posted by saraSS
                                what I'm trying to do now is a stackclass where i'm gonna have all the fuction to use with the stacklike this
                                I think you're trying to write too much code on your own. As far as I can see the assignment does not prevent you from using the std::stack from STL, does it? So, you don't have to implement a stack class on your own, it's already there!

                                Comment

                                Working...