THe foll is a part of a prg i have to append one dummy list to a original list at destination in the original list .So plz help me
i am a new user so sorry for any mistalke
Here is the code :

#include<stdio.h>
#include<conio.h>
struct node
{
struct node *prev,*next;
char line[20];
};
void copy(struct node **,int start,int end,int desti);
void Append(struct node **, char *);
void Appendll(struct node **, char *);
void Display(struct node **);
void main()
{
struct node *head=NULL;
int ch,start,end,desti;
char line[20],temp[3];
clrscr();
while(1)
{      printf("Enter your choice: ");
scanf("%d",&ch);
 if(ch==1)
 {
    printf("Enter the data in the first ll ");
    scanf("%s",line);
    Append(&head,line);
 }
 else
  if(ch==2)
 {
      Display(&head);
 }
 else
 if(ch==3)
 {
   printf("Enter the start end and destination  ");
   scanf("%d %d %d",&start,&end,&desti);
   copy(&head,start,end,desti);
 }
 else
  if(ch==0)
   {
     exit(0);
   }
 }
}
void Append(struct node **list, char line[])
{
 struct node *nn=NULL;
 char data[20];
 nn=(struct node *)malloc (sizeof(struct node));
 strcpy(nn->line,line);
 nn->next=NULL;
 if(*list==NULL)
 {
  nn->prev=NULL;
  *list=nn;
 }
 else
 {
  struct node *t=*list;
  while(t->next!=NULL)
    t=t->next;
    nn->prev=t;
  t->next=nn;
 }
}
void Appendll(struct node **dummy, char line[])
{
 struct node *newnode=NULL;
 newnode=(struct node *)malloc (sizeof(struct node));
 strcpy(newnode->line,line);
 newnode->next=newnode->prev=NULL;
 if(*dummy==NULL)
 {
  newnode->prev=NULL;
  *dummy=newnode;
 }
 else
 {
  struct node *temp=*dummy;
  while(temp->next!=NULL)
  temp=temp->next;
  newnode->prev=temp;
  temp->next=newnode;
 }
}
void Display(struct node **head)
{
 struct node *temp=*head;
 int i;
 if(temp==NULL)
  {
  printf("Empty");
   return;
   }
  else
  {
  printf("\n\t\t");
  do
  {
  printf("\n%s ",temp->line);
  temp = temp->next;
  }
 while (temp!=NULL);
  printf("NULL\n\n");
 }
}
void copy(struct node **head,int start,int end,int desti)
{
  struct node *s=NULL,*dummy=NULL,*t=NULL;
  int i;
  char data[20];
  for(i=1,s=*head;i<=start;i++)
  s=s->next;
  //copying the data into dummy link list
  for(i=start;i<=end;i++)
  {
    strcpy(data,s->line);
    Appendll(dummy,data);
  }
  printf("Dummmy");
  Display(dummy);
  //appending that dummy list to original one at destination
  for(i=1,s=*head;i<=desti;i++)
  s=s->next;
        //  desti
  //head-> 1 2 3 4 5  s->=6=  7
  // dummy ->=1 2 3 4<-t NULL
  if(s->next!=NULL)
  {
    for(t=dummy;t!=NULL;t=t->next);
    s->next->prev=t;
    t->next=s->next;
    s->next=dummy;
    dummy->prev=s;
  }
  Display(head);
}

Recommended Answers

All 4 Replies

What is problem you are facing??

that code doesnot work . I knw the logic but its not working. THere may be some errors may be u spot them out
Thanks:cheesy:

First I'd recommend learning how to indent your code, and add some whitespace, and use full bracing, and better variable names. Then throw out non-standard junk, and add in the necessary headers. And put in some error checking. And discard the malloc casts (if you are compiling C with a C++ compiler, look into compiling C as C). Avoid input using scanf. Watch out for empty loops cause by putting a semicolon at the end of a for statement. Properly declare main, etc.

Then it might look more like this (except this still uses your scanf calls) and someone else may bother to take a pass at it.

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

struct node
{
   struct node *prev,*next;
   char line[20];
};

void copy(struct node **,int start,int end,int desti);
void Append(struct node **, char *);
void Appendll(struct node **, char *);
void Display(struct node **);

int main()
{
   struct node *head = NULL;
   int ch, start, end, desti;
   char line[20];
   while ( 1 )
   {
      printf("Enter your choice: ");
      scanf("%d",&ch);
      if ( ch == 1 )
      {
         printf("Enter the data in the first ll ");
         scanf("%s",line);
         Append(&head,line);
      }
      else if ( ch == 2 )
      {
         Display(&head);
      }
      else if ( ch == 3 )
      {
         printf("Enter the start end and destination  ");
         scanf("%d %d %d",&start,&end,&desti);
         copy(&head,start,end,desti);
      }
      else if ( ch == 0 )
      {
         break;
      }
   }
   return 0;
}

void Append(struct node **list, char line[])
{
   struct node *newnode = malloc (sizeof *newnode);
   if ( newnode )
   {
      strcpy(newnode->line, line);
      newnode->next = NULL;
      if ( *list == NULL )
      {
         newnode->prev = NULL;
         *list=newnode;
      }
      else
      {
         struct node *t = *list;
         while ( t->next != NULL )
         {
            t = t->next;
         }
         newnode->prev = t;
         t->next       = newnode;
      }
   }
}

void Appendll(struct node **dummy, char line[])
{
   struct node *newnode = malloc(sizeof *newnode);
   if ( newnode )
   {
      strcpy(newnode->line,line);
      newnode->next=newnode->prev=NULL;
      if ( *dummy==NULL )
      {
         newnode->prev=NULL;
         *dummy=newnode;
      }
      else
      {
         struct node *temp=*dummy;
         while ( temp->next!=NULL )
            temp=temp->next;
         newnode->prev=temp;
         temp->next=newnode;
      }
   }
}

void Display(struct node **head)
{
   struct node *temp = *head;
   if ( temp==NULL )
   {
      printf("Empty");
      return;
   }
   printf("\n\t\t");
   do
   {
      printf("\n%s ", temp->line);
      temp = temp->next;
   }
   while ( temp != NULL );
   printf("NULL\n\n");
}

void copy(struct node **head, int start, int end, int desti)
{
   struct node *s = NULL, *dummy = NULL, *t = NULL;
   int i;
   char data[20];
   for ( i = 1, s = *head; i <= start; i++ )
   {
      s = s->next;
   }
   //copying the data into dummy link list
   for ( i = start; i <= end; i++ )
   {
      strcpy(data, s->line);
      Appendll(&dummy, data);
   }
   printf("Dummmy");
   Display(&dummy);
   //appending that dummy list to original one at destination
   for ( i = 1, s = *head; i<= desti; i++ )
   {
      s = s->next;
   }

   //  desti
   //head-> 1 2 3 4 5  s->=6=  7
   // dummy ->=1 2 3 4<-t NULL
   if ( s->next != NULL )
   {
      for ( t = dummy; t != NULL; t = t->next )
      {
         s->next->prev=t;
         t->next=s->next;
         s->next=dummy;
         dummy->prev=s;
      }
   }
   Display(head);
}

Instead of

scanf("%s",line);

Do something like

fgets(line,20,stdin);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.