You are here:

C/doubly linked list

Advertisement


Question
i have already created this program but there are certain problems that i do not know how to debug... like the program crashes whenever i add an entry,etc. can you just check the problem? thanks.

#include<stdlib.h>
struct node{
  char info;
  struct node *right;
  struct node *left;
};

typedef struct node *nodeptr;
typedef int bool;
nodeptr SL;

nodeptr getnode()
{
  nodeptr p;
  p=(struct node *) malloc (sizeof(struct node));
  return p;
}

nodeptr initialize(nodeptr S)//initialize
{
  S=NULL;
  return S;
}

bool empty(nodeptr S)
{
  if(S==NULL)
     return 1;
  else
     return 0;
}

void insertright(nodeptr p , char entry) //insert right
{
        nodeptr q;
        nodeptr temp;
        q=getnode();
        temp = getnode();
        if(p=NULL){
        q->info = entry;
        q = p->right;
        q->left = p;
        }
        else{
             temp = p->right;
             p->right = q;
             q->left =p;
             q->right = temp;
             temp->left = q;
             }
}

void insertleft(nodeptr p , char entry) //insert left
{        
       nodeptr q;
       nodeptr temp;
       q=getnode();
        temp = getnode();
       if(p=NULL){
       p->info =entry;
       q->left = p;
       p->right= q;
       }
       else{
            p->info = entry;
            temp = q->right;
            q->right = p;
            q = p->left;
            p->right = temp;
            temp->left = p;
            }
}

void removenode (nodeptr p) //remove cuurent node
{
    nodeptr q;
    q=p;
    p=p->right;
    p->left = NULL;
    free(q);
}

void display(nodeptr S)
{
  nodeptr p1;
  p1=S;
  printf("\nLINKED LIST:\n");
  while(p1!=NULL)
  {
     printf("%c ", p1->info);
     p1=p1->right;
  }
  printf("\n");
  return;
}
--------------------------------------------------------

#include<stdio.h>
#include<conio.h>

#include "supplementary.c"

int main()
{
   nodeptr mainnode;
   
   int quit=0;
   char ch;
   char position;
   char item;

   printf("\t\t\tDOUBLY LINKED LIST\n\n");
   printf("Operations:\n\n");
   printf("---------------------------------------------------\n");
   printf("e       - checks if link list is empty.\n");
   printf("i       - initializes link list.\n");
   printf("+ L X   - inserts an item X at the left.\n");
   printf("+ R X   - inserts an item X at the right.\n");
   printf("-       - removes an item.\n");
   printf("d       - displays the linked list.\n");
   printf("q       - quits the program.\n");
   printf("---------------------------------------------------\n\n");
   
   
   while (quit==0)
   {
         printf("> ");
         ch=getche();
         
         switch(ch)
         {
                   case 'e': //checks if linked list is empty
                   {
                        if( empty(mainnode)) {printf("\n\t->List is empty!\n"); }
                        else if( !empty(mainnode) ) { printf("\n\t->List has contents.\n"); }
                        else { printf("\n\t->Create linklist first.\n"); }
                   }break;
                   case 'i':
                   {
                        mainnode = initialize(mainnode);
                        printf("\n\t->List initialized.\n");
                   }break;
                   case '+':
                   {
                        printf("\n\t->Position: ");
                        fflush(stdin);
                        scanf("%c", &position);
                        fflush(stdin);
                        
                        if (position!='R' &&  position!='L' && position!='l' && position!='r'){ printf("\n\t->Invalid position.\n"); break;}
                        
                        printf("\n\t->Item: ");
                        fflush(stdin);
                        scanf("%c", &item);
                        fflush(stdin);
                        
                        if (position== 'L' || position== 'l'){
                                    insertleft(mainnode, item);
                                    printf("\n\t->Node data added.\n");
                                    }
                        else if(position=='R' || position== 'r'){
                                    insertright(mainnode,item);
                                    printf("\n\t->Node data added.\n");
                                    }
                        
                        //display(mainnode);
                   }break;
                   case '-':
                   {
                        if( empty(mainnode)) {printf("\n\t->List is empty!\n"); break; }
                        
                        removenode(mainnode);
                        
                        printf("\n\t->Node data removed.\n");
                        
                        //display(mainnode);
                   } break;
                   case 'd':
                   {
                        if(empty(mainnode)) { printf("\n\t->Nothing to display!\n"); }
                        else { display(mainnode); }
                        break;
                   }
                   case 'q':
                   {
                        quit =-1;
                   }break;
                   default:
                   {
                           printf("\n\t->Invalid command.\n");
                   }break;
         }//end switch
   }//end while
   
   system("pause");
}
-----------------------------------------------------

thanks i appreciate your help for this.  

Answer
I am finding it difficult to understand your code.
Do you have the algorithm of this code?
That would help in understanding it.

A quick question: What are you trying to do in the function insertright()?
In that function, you are doing:
if(p=NULL){
Note that you are using '=' instead of '=='.
And in the same function, you are doing:
     if(p=NULL){
       q->info = entry;
       q = p->right;
       q->left = p;
     }
When p is NULL, trying to get p->right will crash the code.

And are you trying to implement a circular list or linear doubly linked list?

-Narendra

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.