Thread: linked list problems

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    linked list problems

    I have also been working on this for a week. I had it to where it would display display whatever linked list was entered, but I needed to change it to accept another value and add it to the list and redisplay it. I've attempted to use the insert function but I am very unfamiliar with it and now the program does not even run. the only error I get is: error C2664: 'insert' : cannot convert parameter 1 from 'int' to 'int *' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast. And I have no idea how to fix that. Please help
    insert
    Code:
    #include "stdafx.h"
    #define maxnum 10
    struct value
    {
    	int num[maxnum]; 
    	struct value*nextaddr;
    };
    
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int a,b,c,d,e,f;
    	void readInsert();
    	printf("please enter five numbers:");
    		scanf("%d,%d,%d,%d,%d", &a,&b,&c,&d,&e,&f);
    		struct value t1={a};
    		struct value t2={b};
    		struct value t3={c};
    		struct value t4={d};
    		struct value t5={e};
    		struct value *first;
    		void display (struct value*);
    
    		first=&t1;
            t1.nextaddr=&t2;
    		t2.nextaddr=&t3;
    		t3.nextaddr=&t4;
    		t4.nextaddr=&t5;
    		t5.nextaddr=NULL;
    		display (first);
    		readInsert();
    		
    
    	return 0;
    }
    void display(struct value*contents)
    {
    	while (contents!=NULL)
    	{
    		printf("%d", *contents->num);
    		contents=contents->nextaddr;
    	}
    }
    	void readInsert()
    	{
    	int num[maxnum];
    	int newval;
    	void insert(int*);
    	printf("enter another number:");
    	scanf("%d",&newval);
    	insert(newval);
    	}

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    		struct value t1={a};
    		struct value t2={b};
    		struct value t3={c};
    		struct value t4={d};
    		struct value t5={e};
    I'm pretty sure that initializing variables with variable initializers in that way is C99-only.

    readInsert() cannot insert anything into the linked list in main(), because it does not have access to the said linked list. I'm not sure how insert() is supposed to work, but perhaps you could try
    Code:
    insert(&newval);
    I think that readInsert() (and insert()) will still need to be passed a struct value *first, however.

    Can you post the code or description of insert()?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    18
    From the book it says that insert() is used to add a new structure to a dynamically linked list. the insert function I used is pretty much from the book. The difference is that the program they use it in does not already have an existing list. Do you know how the pop() funtion works-is that more applicable? adding a value to the list and redisplaying it seems like it should be easy...

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    From the book it says that insert() is used to add a new structure to a dynamically linked list.
    If this is the case, then it must use a global linked list, because you're not passing it anything of the sort. The rest of your code is not using a global linked list, so this function likely isn't going to work for you. Post its code if you can.

    Do you know how the pop() funtion works-is that more applicable?
    pop(), insert(), and other similar functions are just user-defined functions. I can guess what they will do from their names, but I don't know for sure like I do for, say, printf(). You'll have to post the code for these functions if you want us to help you with their use.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    18

    insert() function

    Here is the code with my attempt at the insert function. Before the insert function the linked list did work properly. Now it has 28 errors-I am in over my head.
    insert
    Code:
    #include "stdafx.h"
    #include "stdlib.h"
    #include "string.h"
    #define maxnum 10
    struct value
    {
    	int num[maxnum]; 
    	struct value*nextaddr;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	int a,b,c,d,e,f;
    	firstRec=Null;
    	void readInsert();
    	printf("please enter five numbers:");
    		scanf("%d,%d,%d,%d,%d", &a,&b,&c,&d,&e,&f);
    		struct value t1={a};
    		struct value t2={b};
    		struct value t3={c};
    		struct value t4={d};
    		struct value t5={e};
    		struct value *first;
    		void display (struct value*);
    
    		first=&t1;
                                    t1.nextaddr=&t2;
    		t2.nextaddr=&t3;
    		t3.nextaddr=&t4;
    		t4.nextaddr=&t5;
    		t5.nextaddr=NULL;
    		display (first);
    		readInsert();
    		
    
    	return 0;
    }
    void display(struct value*contents)
    {
    	while (contents!=NULL)
    	{
    		printf("%d", *contents->num);
    		contents=contents->nextaddr;
    	}
    }
    	void readInsert()
    	{
    	int num[maxnum];
    	int newval;
    	void insert(int*);
    	printf("enter another number:");
    	scanf("%d",&newval);
    	insert(newval);
    	}
    	
    		void insert(int*newval)
    		{
    			srtuct value *linear Locate(int*);
    			srtuct value *newaddr, *here;
    		newaddr=(struct value*) malloc (sizeof(struct value));
    			if (newaddr==(struct value*) NULL)
    			{
    		printf("\n Could not allocate the requested space\n");
    				exit(1);
    			}
    			if (&t1==NULL)
    			{
    				newaddr->nextaddr=NULL;
    				&t1=newaddr;
    			}
    			else if (strcmp(newval, &t1->newval)<0)
    			{
    				newaddr->nextaddr=&t1;
    				=newaddr;
    			}
    			else
    			{
    				here=linear Locate(newval);
    			newaddr->nextaddr=here->nextaddr;
    			here->nextaddr=newaddr;
    		}
    			strcpy(nexaddr->newval, newval);
    		}
    my attempt at the function is my adaptation of how the book used it, for I don't know how to use it myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Help!
    By mbk in forum C Programming
    Replies: 3
    Last Post: 01-31-2008, 03:54 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM