Thread: Lookup in lists

  1. #1
    Registered User
    Join Date
    Dec 2007

    Lookup in lists

    I am just practicing my list writing skills. I wrote a few list funcs, such as addnew, lookup etc.
    However, when it comes to implementing lookup in main funcs i am a bit cunfused as to what parameters i should use. here is my code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    typedef struct List { char *name; int value; struct List *next;} List;
    List *newitem (char *name, int value)
    	List *newp;
    	newp = (List *)calloc(1,sizeof(List));
    	newp -> name = name;
    	newp -> value = value;
    	newp -> next = NULL;
    	return newp;
    List *addfront (List *listp, List *newp)
    	newp -> next = listp;
    	return newp;
    List *addtoend (List *listp, List *newp)
    	List *p;
    	if (listp == NULL)
    		return newp;
    	for (p = listp; p-> next != NULL; p = p -> next)
    	p -> next = newp;
    		return listp;
    List *lookup (List *listp, char *name)
    	for ( ;listp != NULL; listp = listp -> next)
    		if (strcmp(name, listp -> name) == 0)
    		return listp;
    	return NULL;
    int main (void)
    	char s;
    	char j[15];
    	List *K = NULL;
    	while (s != '.'){
    		s = getchar();
    	K = newitem (s,K);
    	printf("Type a name please:");
    	scanf("%s", j);
    what would i need to put into the lookup func after a name is read into the array?

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    You haven't properly constructed the list yet, so lookup wont work. You haven't got the right parameters to newItem, and you aren't calling addfront or addtoend to build up a list. You have to fix that before you can start trying to get lookup to work.

    You might want a char array in the List struct instead of just a char*.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  2. Help creating lists of pointers
    By The_Kingpin in forum C Programming
    Replies: 2
    Last Post: 12-11-2004, 08:10 PM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  5. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM