View Single Post
Old May 24th, 2006, 10:33 PM   #1
Pacer
Newbie
 
Join Date: May 2006
Posts: 4
Rep Power: 0 Pacer is on a distinguished road
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);
}
Pacer is offline   Reply With Quote