• C

Making a doubly linked list

Hello I am having trouble creating a DLL that can add and delete a node.Traverse the list. Find an item on the list and read and write the information from a file. here is the code I have so far any help is appreciated.



/*
Data Structure program to implement a doubly-linked list using dynamic memory allocation*/

#include<stdio.h>
#include<stdlib.h>

typedef struct DLPkg *ptr_List;

typedef struct DLPkg{
      int iENum;
      char cEName[20];
      struct ptr_List *ptr_next;
      struct ptr_List *ptr_prev;
}List;



void *create_node ();
void *add_node ();
/*void delete_node();
void print_node(void);
/*void find_node)void);
/*void traverse_node(void);
void load_data(void);*/

void main ( )

{

   



}

/*************************************************************************/
void *create_node(ptr_List *ptr_cur )
{          
      *ptr_cur = NULL;

                 if ((*ptr_cur=(NODE *)malloc(sizeof(NODE)))==NULL)
                  error("malloc() fail\n");
}      
/***********************************************************************/
void add_node(List *ptr_cur,*List )/*function to insert a node*/
{

      NODE *ptr_new;
      int prev, new,curr,next;

      *ptr_new = create_node();
      printf("Enter the id:");
      scanf("%i", &ptr_new->iENum);

      if (*ptr_first == NULL){/*add to empty list*/
             prev=*ptr_new;
             next=*ptr_new;
             *ptr_prev=NULL;
             *ptr_next=NULL;

      }
      else{/*add to existing list*/
            if(curr==prev)
                  *ptr_new=NULL;
                   prev=new;
                  *ptr_next=curr;
                  *ptr_cur=new;
            }

      }
/*******************************************************************
void delete_node (*DPLkg,*ptr_cur,*List)/*function to delete a Node
{
      NODE *ptr,*ptr_cur;
      int iENum;
      int prev, new,curr,next;
      
      
      if (*ptr_first==NULL){
         printf("List is empty. Nothing to delete.");
        
      }else {
            printf("Enter the ID to delete");
            scanf(" %i", &ptr_cur->iENum);                  
                        
            if (next==prev==curr){ Only if there is one node in list*
                  prev=NULL;
                  next=NULL;
                  free(*ptr_first);
      }else {
            if(curr==prev)
               prev=*ptr_prev;
               *ptr_pev=NULL;
         } else {
            if (curr==next)
                  next=*ptr_next;
                  *ptr_next=NULL;
      }
            
/************************************************************************
void print_node(*DLPkg, *ptr_cur,*List)
{*/


      
                  
                      










Souldier98Asked:
Who is Participating?
 
cryptosidConnect With a Mentor Commented:
Hi,

You shouldn't be asking for source code here on EE, this forum is for helping out ppl who have specific problems with their code.

Anyways, i ran a search on Google. Check the below link

http://www.gidforums.com/t-7086.html

Use google next time :-)

Regards,
Siddhesh
0
 
Souldier98Author Commented:
I'm just trying to make sure I'm on the right track so far. Not to have the answer given to me.
0
 
rajeev_devinCommented:
>> Hello I am having trouble creating a DLL that can add and delete a node
Are you really making a DLL ?
0
The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

 
rajeev_devinCommented:
Your structure definition should be something like this

typedef struct DLPkg *ptr_List;

typedef struct DLPkg{
     int iENum;
     char cEName[20];
     ptr_List ptr_next;  // No need of pointer, because ptr_List is defined as a pointer.
     ptr_List ptr_prev; // Same here
}List;
0
 
rajeev_devinCommented:
>> if ((*ptr_cur=(NODE *)malloc(sizeof(NODE)))==NULL)
What is this NODE ?
Fo you mean List.
0
 
rajeev_devinCommented:
>> Fo you mean List.
Do you mean List?
0
 
rajeev_devinCommented:
Try to indent your code. It's not that difficult :)
0
 
Souldier98Author Commented:
Yes I'm trying to create a Doubly linked list
0
 
fridomConnect With a Mentor CEO/ProgrammerCommented:
create_node is wrong.

You want to have something like

DL_Node * create_node (void){
0
 
fridomCEO/ProgrammerCommented:
Oh, great sending the stuff without having written it all I hate this:
again
DL_Node * create_node (void){
    DL_Node *result = malloc(sizeof(*result));
    /* set the containing structure elements to proper values */


   return result;

You do not tell where you like to insert a new node so one can use the beginning
if you have someting like
DL_Node *header;

if (header  == NULL) {
    header = create_node();
} else {
   DL_Node *new_node = create_node();
   header->next = new_node;
   new_node->previous = header;
}
...

If you want to delete you have to decide which way you like to go:
- comparing ot a special DL_Node element and deleting it if found
- deleting at a special position.

Source for a Double linked list can be found e.g in the glib sources.

Regards
Friedrich
   


0
 
cryptosidCommented:
Hi,

Sorry for assuming that you wanted complete code.

Here is a ppt of the Algorithm.

http://www.cs.gsu.edu/~cscbecx/Chapter%2017.ppt

It should help you achieve the objective.

Regards,
Siddhesh
0
 
PaulCaswellCommented:
Hi Souldier98,

Dont worry about the fact that this is homework. Its irritating to us when students try to lie about it, thinking they will get more that way. We do help with homework here. All you need to do is concentrate on what the experts are saying, understand it and make it happen in your code. If you dont understand what they are saying, say so and they, or someone else, will explain for you.

As you hit each problem, post your code, describe the problem and we will help. Honestly :-)

Paul <Page Editor>
0
 
Souldier98Author Commented:
I have done some more work on the program and fixed some of the errors pointed out.
Know I am having problems with the add and delete functions. The errors state that I have incompatable oprands. Also I am started to write my save and load from file statements but am unsure if a for or while loop would work best to read in the data.



Data Structure program to implement a doubly-linked list using dynamic memory allocation*/

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>



 struct DLPkg{
      int iENum;
      char cEName[20];
      struct DLPkg *ptr_next;
      struct DLPkg *ptr_prev;
      struct DLpkg *ptr_cur;
}

 
 *create_List ();
void add_node ();
void delete_node();
void print_list();
void find_node ();
void traverse_list();
void load_data();
void save_data();

void main ( )

{

      char choice = 'J';
      while (choice != 'x')
      {
            printf("\n\n\n");
            puts("C: Create a List");
            puts("A: Add a item");
            puts("D: Delete a item");
            puts("P: Print the List");
            puts("T: Traverse the List");
            puts("F: Find a Record");
            puts("L: Load data");
            puts("S: Save data");  

            printf("\n Please enter your choice:  " );
            gets(&choice );

            choice = toupper( choice);


          switch (choice)
            {
                  case 'C': create_List();
                        break;
                  case 'A': add_node ();
                        break;
                  case 'D': delete_node();
                        break;
                  case 'P': print_list();
                        break;
                  case 'T': traverse_list();
                        break;
                  case 'F': find_node();
                        break;
                  case 'L' : Load_data();
                        break;
                  case 'S': Save_data();
                        break;
                  default : puts("Not an option.");
                  }
                  
       }

      return ;

}
 
/*************************************************************************/
void create_List(DLPkg)
{          
      struct DLPkg *ptr_cur,*ptr_next, *ptr_prev;

      ptr_cur = (struct DLPkg *)malloc(sizeof(struct DLPkg));
      
      ptr_cur->iENum=0;
      *ptr_next = NULL;
      *ptr_prev = NULL;

      return ;
      
}
/***********************************************************************/
void DLPkg *add_node(DLPkg *ptr_cur)/*function to insert a node*/
{

      struct DLPkg *ptr_new, *ptr_next, *ptr_prev;
      struct DLPkg prev, new,curr,next;

      *ptr_new = create_List();
      printf("Enter the id:");
      scanf("%i", &ptr_new->iENum);

      if (*ptr_new == NULL){/*add to empty list*/
             prev=*ptr_new;
             next=*ptr_new;
             *ptr_prev=NULL;
             *ptr_next=NULL;

      }
      else{/*add to existing list*/
            if(curr==prev)
                  *ptr_new=NULL;
                   prev=new;
                  *ptr_next=curr;
                  *ptr_cur=new;
            }
            return *ptr_cur;
}
/*******************************************************************/
void DLPkg *delete_node (void)/*function to delete a Node*/
{
      struct DLPkg *ptr_cur, *ptr_prev;
      int iENum;
      struct DLPkg prev,curr,next;
      
      
      if (*ptr_cur==NULL){
         printf("List is empty. Nothing to delete.");
        
      }  else {
            printf("Enter the ID to delete");
            scanf(" %i", &ptr_cur->iENum);                  
                        
            if (next==prev==curr){ /*Only if there is one node in list*/
                  prev=NULL;
                  next=NULL;
                  free(*ptr_cur);
      }  else {
            if(curr==prev)
               prev=*ptr_prev;
               *ptr_prev=NULL;
               }
      }      
/************************************************************************/
void save_data (void)
{

            FILE *datafile;

            int i;

            datafile= *fopen("dbllist.txt", "w+");

            if (datafile == NULL)
            {
                  printf("Error opening data file");
                  exit(0);
            }








/**********************************************************/
void load_data (dbllist, *DLPkg)
{

            FILE *datafile;
            char cEName;
            int iENum;
            int i;

            datafile = fopen("dbllist.txt", "r");

            while  (datafile != NULL)
            {
                   fscanf(datafile, "%i,  %s",&iNum, &sString);
            }

            

      


      
                  
                      







return 0;

}
0
 
fridomCEO/ProgrammerCommented:
bug 1:

*ptr_new = create_List();
     printf("Enter the id:");
     scanf("%i", &ptr_new->iENum);

     if (*ptr_new == NULL){/*add to empty list*/

ptr_new can not be null but in the case of  a memory allocation problem:


bug 2:
struct DLPkg *ptr_cur, *ptr_prev;
     int iENum;
     struct DLPkg prev,curr,next;
     
     
     if (*ptr_cur==NULL){
        printf("List is empty. Nothing to delete.");
       
     }  else {

ptr_cur does not get any values assingned so you access a non initialized pointer.

It's a  bad idea to ask stuff withint delete_node. you should ask before and hand it over to delete_node as a parameter.

Friedrich
0
 
cryptosidCommented:
void create_List(DLPkg)
{          
     struct DLPkg *ptr_cur,*ptr_next, *ptr_prev;

     ptr_cur = (struct DLPkg *)malloc(sizeof(struct DLPkg));
     
     ptr_cur->iENum=0;
     *ptr_next = NULL;
     *ptr_prev = NULL;

     return ;
     
}

The above function seems incorrect... you should have some global pointer which is the first node which this function could manipulate or else atleast pass that pointer to this function so that this function may allocate memory and initialize its members.

Also here..
ptr_cur->iENum=0;
     *ptr_next = NULL;
     *ptr_prev = NULL;
I think it should have been...

ptr_cur->iENum=0;
ptr_new->ptr_next = NULL;
ptr_new->ptr_prev = NULL;

Hope that helps.
Regards,
Siddhesh
0
 
Souldier98Author Commented:
Thank you everyone for your input I have fixed alot of things the only problem I am having know is in the createlist function and addnode function. In create list I get a extraneous return error at the end of the function and in addnode I can't figure out how to get the ptr_new to be compatiable with the createlist function.

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>



 struct DLPkg{
      int iENum;
      char cEName[20];
      struct DLPkg *ptr_next;
      struct DLPkg *ptr_prev;
      struct DLpkg *ptr_cur;
      struct DLPkg *ptr_new;
};

struct DLPkg *prev, *curr, *new, *next;


void create_List (void);
void add_node (void);
void delete_node(void);
void print_list(void);
void find_node (void);
void traverse_list(void);
void load_data(void);
void save_data(void);



void main ( )

{

      char choice = 'J';
      while (choice != 'x')
      {
            printf("\n\n\n");
            puts("C: Create a List");
            puts("A: Add a item");
            puts("D: Delete a item");
            puts("P: Print the List");
            puts("T: Traverse the List");
            puts("F: Find a Record");
            puts("L: Load data");
            puts("S: Save data");  

            printf("\n Please enter your choice:  " );
            gets(&choice );

            choice = toupper( choice);


          switch (choice)
            {
                  case 'C': create_List();
                        break;
                  case 'A': add_node ();
                        break;
                  case 'D': delete_node();
                        break;
                  case 'P': print_list();
                        break;
                  case 'T': traverse_list();
                        break;
                  case 'F': find_node();
                        break;
                  case 'S': save_data();
                         break;
                  case 'L': load_data();
                         break;                        
                  
                  default : puts("Not an option.");
                  }
                  
       }

      return ;

}
 
/**********************************************************/
void create_List (void)
{          
      struct DLPkg *ptr_cur;
      

      ptr_cur = ((struct DLPkg *)malloc(sizeof( struct DLPkg)));
            
      ptr_cur->iENum=0;
      ptr_cur->ptr_next = NULL;
      ptr_cur->ptr_prev = NULL ;

      
      return (ptr_cur);/*extraneous return value error*/
}
/**********************************************************/
void add_node(void)  /*function to insert a node*/
{
      
      struct DLPkg *ptr_cur,*ptr_next,*ptr_new;
      
      
      ptr_cur=malloc(sizeof(int));
      ptr_new=create_list();/*incompatable type error*/
      printf("Enter the id:");
      scanf("%i%s", ptr_new->iENum, &ptr_new->cEName);
      
      

      if (ptr_new == NULL){/*add to empty list*/
             prev=ptr_new;
             next=ptr_new;
             
             
      }
      else{/*add to existing list*/
            if(curr==prev)
                  ptr_new=NULL;
                  prev=new;
                  ptr_next=curr;
                  ptr_cur=new;
            }
            return ;
}
/*********************************************************/
void delete_node (void)/*function to delete a Node*/
{
      
      struct DLPkg *ptr_cur,*ptr_prev;

      
      
      ptr_cur=malloc(sizeof(int));

      if (ptr_cur==NULL){
         printf("List is empty. Nothing to delete.");
        
      }  else {
            printf("Enter the ID to delete");
            scanf(" %i", &ptr_cur->iENum);                  
                        
            if (next&&prev==curr){ /*Only if there is one node in list*/
                  prev=NULL;
                  next=NULL;
                  free(ptr_cur);
      }  else {/*only if there is more than one node in list*/
            if(curr==prev)
               prev=ptr_prev;
               ptr_prev=NULL;
               }             
      }      
      return;
}
/**********************************************************/
void save_data (void)/*saves data to file*/ {

            FILE *datafile;

            struct DLPkg *ptr_cur;

            datafile= fopen("dbllist.txt", "w+");

        if (datafile == NULL)
            {
                  printf("Error opening data file");
                  exit(0);
            }

      while (ptr_cur=NULL)
       {
            fwrite(ptr_cur, sizeof(struct DLPkg),1,datafile);
            ptr_cur=next;
      }

      fclose(datafile);

      return;


}



/**********************************************************/
void load_data (void)/*loads data from file*/
 {

            FILE *datafile;
                        
            struct DLPkg *ptr_cur;

            int iNum;
            char sString;

            datafile = fopen("dbllist.txt", "r");

            if(datafile)
              {
               ptr_cur=(struct DLPkg *)malloc(sizeof(struct DLPkg));
            }
            while  (datafile != NULL)
            {
                   fscanf(datafile, "%i,  %s",&iNum, &sString);
            }

            return;
      }
/*************************************************/
void print_list (void)
{
      struct DLPkg *ptr_cur,*ptr_next;
            

      if (ptr_cur==NULL)
      {
            printf("Nothing to display.\n");
      }else {
            ptr_cur=ptr_next;
            do{
            printf("%i %s",
                        ptr_cur->iENum,
                        ptr_cur->cEName);
                  }while((ptr_cur=ptr_cur->ptr_next)!=NULL);
      }




}
0
 
fridomCEO/ProgrammerCommented:
create list is wrong the prototyp should be:
struct DLPkg * create_List (void)
{          
     struct DLPkg *ptr_cur;


add_node  is wrong. you want to have
- one pointer to the top of the list or in the list
- one pointer which you like to add to the list  but you define:

*ptr_next,*ptr_new;
     
     


     ptr_cur=malloc(sizeof(int));
What do you think you are doing here?


     ptr_new=create_list();/*incompatable type error*/

well a void functoin can not return something.

     printf("Enter the id:");
     scanf("%i%s", ptr_new->iENum, &ptr_new->cEName);
     
     

     if (ptr_new == NULL){/*add to empty list*/
           prev=ptr_new;

print_list is wrong also you are accessing an uninitalized pointer

How about getting one thing done after the other?

Once again. You need some pointer to the current list. You have a doubly linked list therfor it can point anywhere into this list


the type of a list can be seen like this:

prev | content | next     <-> prev | content | next

You just keep one pointer hanging around to the top of the list.
then you walk the list like this

struct DLPkg *cursor = head,
cursor = cursor->next

But this head has to come from the outside you can not expect some arbitrary pointer to point to the list. So you either have to give you functions a parameter or keep some global hanging around.

I strongly suggest you step back and look throuig a decent C book about how this stuff has to be handled. Or you check out sources like for e.g from glib

Friedrich
0
 
fridomCEO/ProgrammerCommented:
Hm, we did not get any feedback but it that our problem? And don't we give at least good hints and even program excperts?
I do not understand why you feel a delete is "fine" here.

Regards
Friedrich
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.