stack error occured in link list

This is a discussion on stack error occured in link list within the C Programming forums, part of the General Programming Boards category; hello all. I don't know why it output twice in "Node-data[%d]",so irritancy! header file Code: /* this a header file ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    22

    stack error occured in link list

    hello all.
    I don't know why it output twice in "Node-data[%d]",so irritancy!

    header file
    Code:
    /* this a header file for list.c */
    /* file name: head-list.h */
    /* author:mycount */
    /* time:12:03 */
    
    
    #define LEN sizeof(link)
    typedef struct list
    {
    	char data;
    	struct list * next;
    }link;
    my code:
    Code:
    /* this is a c programm */
    /* file name:list.c */
    
    #include "head-list.h"
    #include <stdio.h>
    #include <stdlib.h>
    /* above is include files which are concerned in use */
    
    link *create(void)
    {
    	link * head;
    	head=NULL;
    	head=(link *)malloc(LEN);
    	if(head!=NULL)
    	{
    		return head;
    	}
    	else
    	{
    		exit(0);
    	}
    }/* this function is create a list */
    
    link *Init(link *head,int num)
    {
    	int flag=1;
    	head->data=0;
    	link *p,*q;
    
    	while(num--)
    	{
    		if(flag==1)
    		{
    			p=(link *)malloc(LEN);
    			head->next=p;
    			printf("\nfollowing is initialize the link list\n\nplease input the node'data.\n");
    			printf("Node-data[%d]",flag);
    			p->data=getchar();
    		}
    		
    		else
    		{
    			printf("\n");
    			p=(link *)malloc(LEN);
    			printf("Node-data[%d]",flag);
    			p->data=getchar();
    			
    		}
    		q=p;
    		p=p->next;
    		q->next=p;
    		flag++;
    	
    	}
    	q->next=NULL;
    	return head;
    }/* this function is initialize a list */
    
    void disp(link *h)
    {
    	while(h!=NULL)
    	{
    		h=h->next;
    		printf("%c",h->data);
    	}
    }/* this function is display the list */
    
    
    /* following is main function */
    
    int main(void)
    {
    	link * head;
    	link * init_head;
    	head=create();
    	init_head=Init(head,5);
            disp(init_head);	
    
    }//end

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    > head=create();
    This dummy node has no data, yet you print it.
    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
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    please forgave
    i yet do not understand.is the node "head" must have data?
    somebody could you tell me in details.thanks very much.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    It does if you're trying to print it!
    If you understand what you're doing, you're not learning anything.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    > init_head=Init(head,5);
    Think about what actually happens if you have
    init_head=Init(head,0);

    Apart from the crashing.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2006
    Posts
    22
    thanks Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  2. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  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