Struck inside a struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ikkyizzy
    New Member
    • Nov 2016
    • 16

    Struck inside a struct

    Pls. give a hand. I have a simple mistake.

    Code:
    #include<iostream>
    using namespace std;
    
    struct User{
    	 string read;
    	 string write;
    	 string execute;
    };
    
    struct File
    {
    	string filename;
    	User accessrights;
    	File*next;
    };
    
    bool isEmpty(File *head);
    char menu();
    void insertAsFirstElement(File *&head, File *&last, string filename, User accessrights);
    void insert(File *&head, File *&last, string filename, User accessrights);
    void remove(File *&head, File *&last);
    void showList(File *current);
    
    
    bool isEmpty(File *head)
    {
    	if(head == NULL)
    	return true;
    	else 
    	return false;
    }
    char menu()
    {
    	
    	char choice;
    
    	cout<<"Menu\n";
    	cout<<"1.Add an item \n";
    	cout<<"2.Remove an item \n";
    	cout<<"3.Show the list \n";
    	cout<<"4.Exit \n";
    	
    	cin>>choice;
    	
    	return choice;
    }
    void insertAsFirstElement(File *&head, File *&last, string filename1, User accessrights1)
    {
    	File *temp = new File;
    	temp->filename = filename1;
    	temp->accessrights=accessrights1;
    	temp -> next = NULL;
    	head = temp;
    	last = temp;	
    }
    void insert(File *&head, File *&last, string filename2, User accessrights2)
    {
    	if(isEmpty(head))
    	{
    		insertAsFirstElement(head, last, filename2, accessrights2);
    
    	}
    	else
    	{
    		File* temp = new File;
    		temp -> filename = filename2;
    		temp -> accessrights=accessrights2;
    		temp -> next = NULL;
    		last->next =temp;
    		last = temp;
    	}
    }
    void remove(File *&head, File *&last)
    {
    	if(isEmpty(head))
    	cout<<"The list is alreay empty.\n";
    	else if(head==last)
    	{
    		delete head;
    		head==NULL;
    		last = NULL;
    		
    	} else
    	{
    		File*temp = head;
    		head = head->next;
    		delete temp;
    	}
    }
    
    void showList(File *current)
    {
     
      if(isEmpty(current))
      {
      	cout<<"The list is Empty\n";
      }
      else
      {
      	cout<<"The list contains : \n";
      	while(current !=NULL)
    	  {
      		cout<<current->filename<<endl;
      		cout<<current->accessrights.read<<current->accessrights.write<<current->accessrights.execute;
      		cout<<"--------------------"<<endl;
      		current = current ->next;
    	  }
      }
     
    }
    
    
    int main()
    {
    	File *head = NULL;
    	File*last = NULL;
    	char choice;
    	string filenamE;
    	User accessrightS;
    		
    	
    	do{
    	
    
    		choice = menu();
            
    		switch(choice)
    		{
    		
    	     cout<<"Enter your choice :";
           	cin>>choice;
    		
    	     case '1' : cout<<"Enter file name :";
    				     cin>>filenamE;
    				     cout<<"Grant Access rights :";
    				     cin>>User.read>>User.write>>User.execute;
    				     insert(head, last, filenamE, accessrightS);
    				     break;
    				     
    		 case '2' : remove(head,last);
    		 			break;
    		 			
    		 case '3' : showList(head);
    		 			break;
    		 default: cout <<cout<<"System exit\n";
    		}
    	
    	}while(choice!='4');
    	
    }

    I have mistake in the line in Case 1 where I need to get three inputs from the user.
    cout<<"Grant Access rights :";
    cin>>User.read> >User.write>>Us er.execute;
    insert(head, last, filenamE, accessrightS);

    I get the error
    ------------------
    D:\SEMESTER_F\F avQuotes\Source & Exe\FMSFinal.cp p In function 'int main()':
    137 19 D:\SEMESTER_F\F avQuotes\Source & Exe\FMSFinal.cp p [Error] expected primary-expression before '.' token
    137 30 D:\SEMESTER_F\F avQuotes\Source & Exe\FMSFinal.cp p [Error] expected primary-expression before '.' token
    137 42 D:\SEMESTER_F\F avQuotes\Source & Exe\FMSFinal.cp p [Error] expected primary-expression before '.' token
  • ikkyizzy
    New Member
    • Nov 2016
    • 16

    #2
    hey guys I 'm now able to do the prog. by myself
    :) :)

    Comment

    • ikkyizzy
      New Member
      • Nov 2016
      • 16

      #3
      Thanks for viewing my problem although!!

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Too many errors for me to even begin advising you.

        It also looks like you are trying to do a linked list without using nodes.

        Would you please explain more fully your code?

        Comment

        • ikkyizzy
          New Member
          • Nov 2016
          • 16

          #5
          Hi weaknessforcats ,
          Thank you for the reply. I am trying to get what is show in the attached linkedlist structure

          Comment

          • ikkyizzy
            New Member
            • Nov 2016
            • 16

            #6
            Hi guys don't just view give a help!

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              OK. I understand.

              What you have here is a linked list of files where each file is a linked list of users. Sometimes this is called a two dimensional linked list.

              What you try to do this write a linked list that can be used for both files and users. That means writing the linked list when you don't know the data that's contained within the list.

              The common design is a cursor-based linked list. In this design there is a list of nodes where each node is one of your data elements. Here you have two structs. The struct List for the list itself and a struct Node for the nodes in the list:

              Code:
              struct LinkedList
              {
                              int   type; //1 = file 2 = user
              		Node* start;
              		Node* end;
                              Node* current;
              };
              This LinkedList struct s called a cursor. All it knows is where the list starts, where it ends, and where you are currently positioned, and the type of data in the list.

              The Node pointers (start, end, current) are 0 when the list is empty.

              The int type is called a discriminator. If it is a 1 the linked list is for a file. If it is 2 the linked list contains users. type


              The Node struct looks like:

              Code:
              struct Node
              {
              	void* data;
               
              	Node* prev;
              	Node* next;
              };

              You try to write the code so that the user in main() calls only functions with LinkedList pointers as arguments. These functions should access only the members of the LinkedList struct. If they are to insert data in the list then they call a function with a Node* argument to work with the actua nodes.

              So here goes:

              Code:
              int main();
              
              {
              LinkedList* myfiles = CreateLinkedList(1);     //create the list for files
              }
              where

              Code:
              LinkedList* CreateLinkedList(int arg)
              {
                 LinkedList* thelist = new LinkedList;
              
                 thelist->start = 0;
                 theList->end = 0;
                 theList->current = 0;
                 theList->type = arg;
              
              
                 return theklist;
              }
              Does this make sense to you so far?

              Comment

              • ikkyizzy
                New Member
                • Nov 2016
                • 16

                #8
                Thanks for the reply :) Ok . will try like the way you explained

                Comment

                • weaknessforcats
                  Recognized Expert Expert
                  • Mar 2007
                  • 9214

                  #9
                  Ok. But let me go a little further. Let's add to the end of a linked list:

                  Code:
                  int main();
                  
                  {
                  	LinkedList* myfiles = CreateLinkedList(1);     //create the list for files
                  
                          //Create the data
                          File* ptr  = new File;
                          
                          AddToEnd(myfiles, ptr);
                  
                  
                  }
                  where:

                  Code:
                  void  AddToEnd(LinkedList* thelist, File* thedata)
                  {
                          //Create the node
                         Node* n = CreateNode(thedata);
                  
                        //See if the list is empty
                         if (thelist->start == 0)
                         {
                            thelist->start = n;
                            thelist->end = n;
                            return;
                         {
                         //Otherwise insert the second argument after the first argument
                  
                          InsertAfter(thelist->end, n); 
                  {
                  This requires a function to insert one node after another:

                  Code:
                  void* InsertAfter(Node* first, Node* second)
                  {
                  
                         //Is first the last node?
                         if (first->next == 0)
                         {
                         first->next = second;
                         second->prev = first; 
                         second->next = 0;
                          return;
                         }
                         //Otherwise it's in the middle somewhere 
                         second->next = first->next;
                         second->prev = first;
                         first->next->prev  = second;
                         first->next = second;
                  }
                  Notice that all the pointer work with the nodes is inside InsertAfter. This is an example of encapsulation.
                  You do not want node pointers scattered all over the code.

                  I didn't compile this so let me know how it works.

                  Comment

                  • ikkyizzy
                    New Member
                    • Nov 2016
                    • 16

                    #10
                    Code:
                    #include<iostream>
                    using namespace std;
                    
                        struct Node
                        {
                            void* data;
                         
                            Node* prev;
                            Node* next;
                        };
                        
                        struct LinkedList
                        {
                                        int   type; //1 = file 2 = user
                                Node* start;
                                Node* end;
                                        Node* current;
                        };
                        LinkedList* CreateLinkedList(int arg)
                        {
                           LinkedList* thelist = new LinkedList;
                         
                           thelist->start = 0;
                           thelist->end = 0;
                           thelist->current = 0;
                           thelist->type = arg;
                         
                         
                           return thelist;
                        }
                    
                    
                    
                    
                        void* InsertAfter(Node* first, Node* second)
                        {
                         
                               //Is first the last node?
                               if (first->next == 0)
                               {
                               first->next = second;
                               second->prev = first; 
                               second->next = 0;
                                return first;
                               }
                               //Otherwise it's in the middle somewhere 
                               second->next = first->next;
                               second->prev = first;
                               first->next->prev  = second;
                               first->next = second;
                        }
                    
                    
                        void  AddToEnd(LinkedList* thelist, Node* thedata)
                        {
                                //Create the node
                               Node* n = new Node;
                         
                              //See if the list is empty
                               if (thelist->start == 0)
                               {
                                  thelist->start = n;
                                  thelist->end = n;
                                  return;
                               {
                               //Otherwise insert the second argument after the first argument
                         
                                InsertAfter(thelist->end, n); 
                        {
                    
                    
                    int main()
                     
                    {
                    
                    LinkedList* myfiles = CreateLinkedList(1);     //create the list for files
                    //Create the data
                            Node* ptr  = new Node;
                     
                            AddToEnd(myfiles, ptr);      
                            
                            return 0;
                    }


                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      I made a typo in the if statement inside AddToEnd. It has two {. Change the second one to }.


                      I would not have a Node* in main(). The user in main does not know about nodes. The user knows about lists. So you create your data and pass the address of the data to a function that also has a pointer to the list and it is this function (because it is a list function) that knows about nodes. So it is ere you create your node and then call a node function that knows how to connect nodes.

                      By having the Node* in main() you have exposed how the list is constructed. Should you redesign the list main() will have to be changed along with the redesigned list code. This type of coding is called "spaghetti" because the logic threads go all over the place.

                      It takes a little practice to compartmentaliz e your thinking into functional blocks but it is well worth the effort.

                      Comment

                      Working...