Thread: linked lists

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    linked lists

    I have this prototype:
    struct node{
    struct lnode *next;
    int id;
    char *name,*address;
    char **phones;
    }
    the exercise says that the user must be able to create
    categories of linked lists
    i.e. a category that manages information with a symbolic name family,
    another with name friends and so on...
    How can i write source code for creating various of linked lists and also give them symbolic names for later access?

    Sorry guys i didnt write any source code because i dont know how to start ....Any help plz

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    58
    Quote Originally Posted by dionys
    I have this prototype:
    struct node{
    struct lnode *next;//that has to be node too it should be like struct node *next
    int id;
    char *name,*address;
    char **phones;
    }
    why dont u try ur hand at c++. that way u could write this above code and inherit it to all the kind of names of classes u want. (this is only if u want separate features for each of the categories like for friends u would like to keep a separate datatype for nicknames or something like that).

    if that is not ur intention then the above will do. just declare objects of the above struct by whatever names u want like
    Code:
    struct node friend
    etc and use it.
    good luck coding.
    sorry i wont write the code for that.
    even a fish would not have been in danger had it kept its mouth shut.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    35

    Re:

    I cant use this tip because the user must be able to create categories and give symbolic names not me.....

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    For it to be a linked list you have to have
    Code:
    struct somename {
      struct *somename next;
      /* Other info */
    }
    Thats how you move from one node to the next. If there is other information they need then it can be put in there also. But you have to have a pointer to the next item in the list.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    35
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    
    struct lnode{
    struct lnode *next;
    int id; 
    char *name,*address;
    char **phone;
    };
    
    /* twith pointer nextCategory we can browse the categories
    i.e. family,freinds..
    with pointer categSearch we can search the elements of
    each category(basicaly with this pointer we can search
    the linked list of each category)
    */
    
    
    struct LinkLists{
    struct LinkLists *nextCategory;
    struct LinkLists *categSearch;
    char *categName;
    }
    
    
    void createCategory(struct LinkLists **headRef,char *catgName);
    
    int main (void){
    char *category;
    char buffer[81];
    struct LinkLists *head=NULL;
    	
    	
    	printf("Give a category name to continue:\n");
    	printf("If you want to stop give an empty line.\n");
    	
    	do{
    	scanf("%s",buffer);
    	category=(char *)malloc((strlen(buffer)+1)*sizeof(char));
    	if(!category){
    		fprintf(stderr,"Unable to allocate memory");
    		exit(1);
    		}
                          createCategory(&head,category);
    	 }while(buffer[0]!='\n');	
    		
    	
    return 0;
    }
    
    
    void createCategory(struct LinkLists **headRef,char *catgName)
    {
    	struct LinkLists *new;
    	struct LinkLists *current=*headRef;
    	struct LinkLists *prev;
    	new=(struct LinkLists *)malloc(sizeof(struct LinkLists ));
    	if(!new){
    		fprintf(stderr,"Unable to allocate memory");
    		exit(1);
    		}
    		
    	if(*headRef==NULL){
    	new->nextCategory=*headRef;
    	*headRef=new;
    	new->categName=catgName;
    	}
    	else{
    	
    	while(current!=NULL){
    	prev=current;
    	current=current->nextCategory;
    	}
    	prev->nextCategory=new;
    	new=current;
    	new->categName=catgName;
    	}
    }
    where is the problem? i take errors: two or more data types in declaration of `createCategory',
    conflicting types for `createCategory.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    struct LinkLists{
    struct LinkLists *nextCategory;
    struct LinkLists *categSearch;
    char *categName;
    }
    Should be
    Code:
    struct LinkLists{
    struct LinkLists *nextCategory;
    struct LinkLists *categSearch;
    char *categName;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM