Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old May 24th, 2006, 10:41 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 877
Rep Power: 4 The Dark is on a distinguished road
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.
The Dark is offline   Reply With Quote
Old May 24th, 2006, 10:57 PM   #3
Pacer
Newbie
 
Join Date: May 2006
Posts: 4
Rep Power: 0 Pacer is on a distinguished road
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.
Pacer is offline   Reply With Quote
Old May 25th, 2006, 12:06 AM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 877
Rep Power: 4 The Dark is on a distinguished road
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).
The Dark is offline   Reply With Quote
Old May 25th, 2006, 12:40 AM   #5
Pacer
Newbie
 
Join Date: May 2006
Posts: 4
Rep Power: 0 Pacer is on a distinguished road
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);

}
Pacer is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:50 AM.

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