Linked lists help!!!

This is a discussion on Linked lists help!!! within the C Programming forums, part of the General Programming Boards category; hey, I am somewhat a new programmer, so my code isn't the best, but I need to turn this into ...

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

    Linked lists help!!!

    hey,

    I am somewhat a new programmer, so my code isn't the best, but I need to turn this into linked list. Can anyone help?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    typedef struct army {
    
    	char section_name[81];
    	char sub_name[81];
    	int points;
    	char discription[500];
    };
    
    int i=0;
    
    struct army Army;
    
    char file_end[81];
    char temp_word[81];
    char temp_close[8]="</name>";
    char temp_close2[10]="</descrip>";
    
    
    void main()
    {
    	char name[81]="<name>", scan[81], descrip[10]="<descrip>";
    	
    	
    FILE *inputfile;
    
    inputfile = fopen("Rune2.txt","r");
    
    if (inputfile == NULL)
    	{
    		printf("error opening file");
    	}
    	else
    	{
    		
    	printf("File opened successfully. Loading data...\n\n");
    	}
    	
    fscanf(inputfile,"%s", scan); 
    
    for (fscanf(inputfile," %s ", temp_word); temp_word!=NULL; fscanf(inputfile," %s ", temp_word))
    
    {
    		
    	if (strcmp(temp_word, temp_close)==0) break;
    	strcat(temp_word, " ");
    	strcat(Army.sub_name, temp_word);
    	
    }
    
    
    	fscanf(inputfile,"%d", &Army.points); 
    	fscanf(inputfile,"%s", scan);
    
    for (fscanf(inputfile," %s ", temp_word); temp_word!=NULL; fscanf(inputfile," %s ", temp_word))
    
    {
    	i++;	
    	if (strcmp(temp_word, temp_close2)==0) break;
    	strcat(temp_word, " ");
    	strcat(Army.discription, temp_word);
    if (i==9)
    	{
    		i=0;
    		strcat(Army.discription, "\n");
    	}
    
    }
    printf("%s\n%d\n%s\n", Army.sub_name, Army.points, Army.discription);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    typedef struct army {
    
    	char section_name[81];
    	char sub_name[81];
    	int points;
    	char discription[500];
    } /*Missing something here?*/;
    When you use typedef, you're defining a new type. That means you need to provide some name for its new type.
    Code:
    void main()
    This is covered in the FAQ. main never returns void. It always returns an int. Anyone who says otherwise is wrong, or is using Java.

    Next, you should probably get rid of those global variables, as it's clutter, and makes it harder to read your code (especially once your program gets big).

    Finally, here's how you'd start a single linked list:
    Code:
    struct army {
    	struct army *next;
    	char section_name[81];
    	char sub_name[81];
    	int points;
    	char discription[500];
    };
    This is a pointer to the "next" item in the list. Search the forum for "linked list", and there will be ample examples provided for you, as it's a common question. Then, post your attempt at making a list, and any problems, etc, as per the Announcements at the top of the forum.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2004
    Posts
    14
    thanks quzah

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, 04: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, 10: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21