Thread: linked list

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    8

    linked list

    i am having a problem with a program of self referential structures.here is the code
    Code:
    # include<stdio.h>
    # include<conio.h>
    # include<string.h>
    typedef struct stud {
    			char*name;
    			struct stud*next;
    		    }gos;
    gos*temp;
    gos*cur;
    gos*prev;
    gos*root;
    gos*flag;
    char shame[20];
    void main()
    { 	int i,sz;
    	clrscr();
    	temp=(gos*)malloc(sizeof(gos));
    	root=temp;
    	cur=temp;
    	for(i=0;i<3;i++)
    	{
    		printf("enter the size of the name");
    		scanf("%d",&sz);
    		temp->name=(char*)malloc(sz*1);
    		printf("enter the name");
    		scanf("%s",temp->name);
    		cur=cur->next;
    		temp=cur;
    	}
    	temp=root;
    	cur=root;
    	printf("entered names are");
    	for(i=0;i<3;i++)
    	{
    		printf("\n %s \t %u \t %u",temp->name,temp,temp->next);
    		cur=cur->next;
    		temp=cur;
    	}
    	
                    /*insertion*/
    	flag=(gos*)malloc(sizeof(gos));
    	printf("enter the size of the name");
    	scanf("%d",&sz);
    	flag->name=(char*)malloc(sz*1);
    	printf("enter the name");
    	scanf("%s",flag->name);
    	printf("enter the name before which to enter");
    	gets(shame);
    	cur=root;prev=root;
    	while((strcmp(shame,cur->name)!=0)&&(cur!=NULL))
    	{
    		prev=cur;
    		cur=cur->next;
    	}
    	if(!cur)
    	{
    		printf("cannot insert");
    		exit();
    	}
    	if(cur==root)
    	{
    		root=temp;
    		temp->next=cur;
    	}
    	else
    	{
    	temp->next=cur;
    	prev->next=temp;
    	}
    	getch();
    }/*end of main*/
    suppose i am giving the input for three passes
    i,p,q;
    the output coming is:
    the entered names are
    i 2840 0
    q 0 0
    q 0 0
    i don't know why 0 has been assigned to temp->name,it also not diplaying p.I have done the program in two parts firstly i have only diplayed the three linked lists,then it worked fine.When i am trying to insert a new linked list,the problem arises, i am very much confused,plz help.
    code tags added by Salem - use them in future

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>>void main()
    Incorrect, use int main(void)

    >>>temp=(gos*)malloc(sizeof(gos));
    The cast isn't necessary, just make sure you include stdlib.h

    >>>temp->name=(char*)malloc(sz*1);
    If the size of the name if sz, does it include the null terminator? In other words do you need to add 1 to sz. also, whats the point in multipling it by 1??

    >>>mallocing a struct
    In your first for loop, you aren't mallocing any more structs, so how can you be storing 3 lots of data (the number of iterations in the loop)?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM