Thread: stack error occured in link list

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  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,218
    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

  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, 12:30 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM