Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   error of double linked list . (http://www.programmingforums.org/showthread.php?t=9968)

Pacer May 24th, 2006 9:33 PM

error of double linked list .
 
Hello,everybody.
I have no idea to rectify my error.
Could everyone helps me to solve it,thanks all.
I am a fresh man.Thanks again.

head file:
:

/* head file for double linked list */
/* Filename H1.h */

#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(linklist) /* Define the length of linked list */

typedef struct link
{
        char data;
        struct link * pre;
        struct link * next;
}linklist;            /* Defint structure of linked list */


main file:
:

/* double linked list main file */
/* Filename double.cpp */
#include "H1.h"

linklist * create(void)  /* Define the Function to create linked list*/
{
        linklist * head=NULL;
        linklist * p1,* p2;
        int i=2;              /* Define the sequence number */
        p1=(linklist *)malloc(LEN);
        head->next=p1;
        p1->pre=head;
        printf("Member[1] :");
        scanf("%c",&p1->data);
       
        if(p1!=NULL)
        {
                do
                {
                        p2=(linklist *)malloc(LEN);
                        printf("Member[%d] :",i);
                        scanf("%c",&p2->data);
                        p2->pre=p1;
                        p1->next=p2;
                        p1=p2;
                }while(p1->data!='$');
                p1->next=NULL;
                return head;
        }
}
/////////////////////////////////////////////////
void display(linklist * head) /* Define the function to display linked list */
{
        linklist * tem;
        int j=1;
        if((tem=head->next)!=NULL)
        {
                printf("\n");
                while(tem)
                {
                        printf("Member[%d] :",j++);
                        printf("%c",tem->data);
                }
        }
        else
        {
                printf("head node error, exit now!\n");
                exit(0);
        }
}

//////////////////////////////////////////////////
void main()
{
        linklist * test_head;
        test_head=create();
        display(test_head);
}


The Dark May 24th, 2006 9:41 PM

You might want to let us know what the error is, we are not all mind-readers.

One thing that will cause a problem is this line near the top of create
:

        head->next=p1;
You have assigned the value of NULL to head and are then trying to dereference it, this will probably cause your program to shut down.

Note: I didn't look any further, there may be more problems, you need to give more information.

Pacer May 24th, 2006 9:57 PM

thank you Dark.
This is what I want to know.
I really didn't know where the error occured.But I have no more info about it. :(

The Dark May 24th, 2006 11:06 PM

You might not know where the error occurred, but you must know what sort of error it was. For example:
- Did the program not compile? (show the error messages)
- Did the program not link? (show the error messages)
- Did the program run but not produce the expected output? (show the output)
- Did the program run for a bit and then stop? (show any output or error)
- Did the program stop straight away? (show any error messages)
- Did the program connect to the net and buy stuff on ebay using your credit card? (show me what it bought, I might want some too).

Pacer May 24th, 2006 11:40 PM

Dark,I have completed.
:

/* linked list main file */
#include "H1.h"

linklist * create(linklist * point[2])  /* Define the Function to create linked list*/
{
        linklist * head,* tail;
        linklist * p1,* p2;
        int flag=0;
        int count=2;
        //linklist * point[2]; /* Deposit head and tail */
        p1=p2=(linklist *)malloc(LEN);
        printf("\nMember[1] :");
        scanf("%c",&p1->data);
        head=NULL;
       
        while(p1->data!='0')
        {
                flag++;
                if(flag==1)          /* the first node*/
                {
                        head=p1;
                        p1->pre=p1->next=NULL;
                }
                else                  /* other nodes except the first one */
                {
                        p2->next=p1;
                        p1->pre=p2;
                }
                p2=p1;
                p1=(linklist *)malloc(LEN);
                printf("Member[%d] :\n",count++);
                scanf("%c",&p1->data);
        }                        /* create nodes */

        p2->next=NULL;
        tail=p2;
        point[0]=head;
        point[1]=tail;
        return * point;
}
/////////////////////////////////////////////////
void dispAhead(linklist * head) /* Define the function to display linked list */
{
        linklist * tem;
        int j=1;
        if((tem=head)!=NULL)
        {
                printf("\n");
                while(tem)
                {
                        printf("Member[%d] :",j++);
                        printf("%c",tem->data);
                        tem=tem->next;
                }
        }
        else
        {
                printf("head node error, exit now!\n");
                exit(0);
        }
}

//////////////////////////////////////////////////
void main()
{
        linklist * point[2];
        linklist * tem;
    tem=create(point);
        dispAhead(tem);

}



All times are GMT -5. The time now is 6:52 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC