simple problem in a linked list !

This is a discussion on simple problem in a linked list ! within the C Programming forums, part of the General Programming Boards category; i try hard but i couldn`t fix the problem?! This is the source code Code: #include <stdio.h> #include <stdlib.h> #include ...

  1. #1
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70

    simple problem in a linked list !

    i try hard but i couldn`t fix the problem?!

    This is the source code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    struct node
    {
    
    	char *sn;
    	char *cn;
    	struct node* next;
    };
    
    typedef struct node N;
    typedef N* NP;
    
    
    int main()
    {
    	
    	char s[100];
    
    	char *r=NULL;
    	char d[1];
    	d[0]=':';
    	int i,x,y,j;
    	j=0;
    	NP root;
    	NP t;
    	int c=0;
    
    	FILE *fp;
    
        x=0;
    	y=0;
    
    
    	//File Checking !
    	fp = fopen("c:\\textfiles/test.txt","r");
    	if(fp == NULL)
    	{
    		printf("file couldn`t be opened!\n");
    		exit(0);
    	}
    
    	fgets(s,100,fp);
    
    	j=0;
    	d[0] = ':';
    
    	for ( i = 0; i < 100; i++ )
    	{
    		if ( s[i] == '\n' )
    		{
    			s[i] = '\0';
    			break;
    		}
    	}
    
    	r = strtok(s,d);
    	r = strtok(NULL,d);
    	d[0] = ',';
    
    
    
    	if(s[9] == '1')
    	{
    		while(j<3)
    		{
    			if(j==0)
    			{
    				r = strtok(r,d);
    			}
    			t = (N*) malloc(sizeof(N));
    			if(j==0)
    				root=t;
    			t->sn = "s1";
    			t->cn = r;
    			t->next =0;
    			r = strtok(NULL,d);
    			++j;
    			if(j<3)
    				t=t->next;
    		}
    	}
    
    	t->next = 0;
    
    
    	while(c<3)
    	{
    		printf("%s,",root->cn);
    		root = root->next;
    	}
    	printf("\n")
    fclose(fp);
    return 0;
    }
    and this is my input file :

    Code:
    student s1 takes exams:csc212,csc281,csc361
    after compiling the program it will output
    csc212, [the program crash]

    my compiler is : Microsoft Visual C++

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    What it is to have meaningful variable names, and good indentation....

    Oh, and first guess is the d array you're trying to strtok() with has no \0
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    your post dosn`t fix my problems !!

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    Well that's a shocker...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    thanks for help

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Did you even bother to try to fix it?

    Barely looked at your code, but as was pointed out to you, you could do this:

    Code:
    d[1] = '\0';

  7. #7
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    yes i do that ,but the code still not fixed

  8. #8
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    the problem :

    the t is a pointer to a struct and the root is another pointer to the struct

    if i malloc the t in the second time i will lose the first node and if i make this
    root=t;
    to keep the root to point to first node !!
    the root will lose the second and the third node, why that happend?!!

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    If root is the first node then node t could be inserted at the tail like
    root->next = t;
    t->next = NULL;


    An incorrect statement like
    root = t;
    will reassign the first node, leaking whatever root pointed to before.


    If you wanted to properly replace the first node I believe it's
    t->next = root->next;
    root->next = t;

    Thinking carefully, you should realise that by updating what node we had previously, we avoid leaking memory and place t where it should be. Of course, before doing any of this in the middle of a list you have to make sure that all assignments are valid: don't leak a node's next pointer, check it for NULL before assigning.

  10. #10
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    so you mean i cann`t make the root point to the t ?!!

    and i must malloc the root before make it point to t .. is this true!

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,660
    > so you mean i cann`t make the root point to the t ?!!
    > and i must malloc the root before make it point to t .. is this true!
    I don't mean that. root, being a node pointer, can point to other structs or be assigned to another node pointer.


    I gave the code a more thorough lookover.

    Code:
    t->next =0;
    r = strtok(NULL,d);
    ++j;
    if(j<3)
       t=t->next;
    Besides being rediculously hard to follow because the variable names aren't too expressive, (I have come to loathe single letter variable names in most places, expecially if the indentation doesn't help the eye follow the logic.) let's assume j is small. t, and t->next are basically handled as they should be: t->next is NULLed out for allocation and t = t->next will walk down the list. The rather obvious flaw is that contrary to what I posted, in your code, root->next is never updated. You end up with a disjointed list where root only points to the latest t.

    For your singly linked list you must consider that if you move the root node, you will have leaked memory, because there is no way to access the first node anymore.

    Attempt to walk down such a list later like you have...

    walk = root;
    walk = walk->next;

    walk is destined to fault somewhere, because you didn't connect your nodes.

    Correct insertion at the tail of a list:
    root->next = t;
    t->next = NULL;

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    > char d[1];
    How many characters and how many \0 can you store here?

    Post your latest code, with correct indentation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    thanks so much citizen your post help me alot.. @

    and what i really want is that i have 2 pointers

    first one name root
    and the second one name t

    the t is a list where i insert something to it

    and root because i want it to point to the first node in t , why i am doing that is because i want to print out the list from the head to the tail .

  14. #14
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    i am very thanks full for all of you guys , my problems in linked lists now fixed, after i try some tests ..

    i am so happy right now ..

    and i am sorry for bothering you dudes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 12:21 PM
  3. doubly linked list problem
    By YevGenius in forum C Programming
    Replies: 4
    Last Post: 05-02-2004, 08:54 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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