urgent help required plz help

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2006
Posts: 3
Reputation: sania kohli is an unknown quantity at this point 
Solved Threads: 0
sania kohli sania kohli is offline Offline
Newbie Poster

urgent help required plz help

 
0
  #1
May 23rd, 2006
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 :
  1.  
  2. #include<stdio.h>
  3. #include<conio.h>
  4. struct node
  5. {
  6. struct node *prev,*next;
  7. char line[20];
  8. };
  9. void copy(struct node **,int start,int end,int desti);
  10. void Append(struct node **, char *);
  11. void Appendll(struct node **, char *);
  12. void Display(struct node **);
  13. void main()
  14. {
  15. struct node *head=NULL;
  16. int ch,start,end,desti;
  17. char line[20],temp[3];
  18. clrscr();
  19. while(1)
  20. { printf("Enter your choice: ");
  21. scanf("%d",&ch);
  22. if(ch==1)
  23. {
  24. printf("Enter the data in the first ll ");
  25. scanf("%s",line);
  26. Append(&head,line);
  27. }
  28. else
  29. if(ch==2)
  30. {
  31. Display(&head);
  32. }
  33. else
  34. if(ch==3)
  35. {
  36. printf("Enter the start end and destination ");
  37. scanf("%d %d %d",&start,&end,&desti);
  38. copy(&head,start,end,desti);
  39. }
  40. else
  41. if(ch==0)
  42. {
  43. exit(0);
  44. }
  45. }
  46. }
  47. void Append(struct node **list, char line[])
  48. {
  49. struct node *nn=NULL;
  50. char data[20];
  51. nn=(struct node *)malloc (sizeof(struct node));
  52. strcpy(nn->line,line);
  53. nn->next=NULL;
  54. if(*list==NULL)
  55. {
  56. nn->prev=NULL;
  57. *list=nn;
  58. }
  59. else
  60. {
  61. struct node *t=*list;
  62. while(t->next!=NULL)
  63. t=t->next;
  64. nn->prev=t;
  65. t->next=nn;
  66. }
  67. }
  68. void Appendll(struct node **dummy, char line[])
  69. {
  70. struct node *newnode=NULL;
  71. newnode=(struct node *)malloc (sizeof(struct node));
  72. strcpy(newnode->line,line);
  73. newnode->next=newnode->prev=NULL;
  74. if(*dummy==NULL)
  75. {
  76. newnode->prev=NULL;
  77. *dummy=newnode;
  78. }
  79. else
  80. {
  81. struct node *temp=*dummy;
  82. while(temp->next!=NULL)
  83. temp=temp->next;
  84. newnode->prev=temp;
  85. temp->next=newnode;
  86. }
  87. }
  88. void Display(struct node **head)
  89. {
  90. struct node *temp=*head;
  91. int i;
  92. if(temp==NULL)
  93. {
  94. printf("Empty");
  95. return;
  96. }
  97. else
  98. {
  99. printf("\n\t\t");
  100. do
  101. {
  102. printf("\n%s ",temp->line);
  103. temp = temp->next;
  104. }
  105. while (temp!=NULL);
  106. printf("NULL\n\n");
  107. }
  108. }
  109. void copy(struct node **head,int start,int end,int desti)
  110. {
  111. struct node *s=NULL,*dummy=NULL,*t=NULL;
  112. int i;
  113. char data[20];
  114. for(i=1,s=*head;i<=start;i++)
  115. s=s->next;
  116. //copying the data into dummy link list
  117. for(i=start;i<=end;i++)
  118. {
  119. strcpy(data,s->line);
  120. Appendll(dummy,data);
  121. }
  122. printf("Dummmy");
  123. Display(dummy);
  124. //appending that dummy list to original one at destination
  125. for(i=1,s=*head;i<=desti;i++)
  126. s=s->next;
  127. // desti
  128. //head-> 1 2 3 4 5 s->=6= 7
  129. // dummy ->=1 2 3 4<-t NULL
  130. if(s->next!=NULL)
  131. {
  132. for(t=dummy;t!=NULL;t=t->next);
  133. s->next->prev=t;
  134. t->next=s->next;
  135. s->next=dummy;
  136. dummy->prev=s;
  137. }
  138. Display(head);
  139. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: urgent help required plz help

 
0
  #2
May 23rd, 2006
What is problem you are facing??
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3
Reputation: sania kohli is an unknown quantity at this point 
Solved Threads: 0
sania kohli sania kohli is offline Offline
Newbie Poster

Re: Errors to be found out

 
0
  #3
May 23rd, 2006
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:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Errors to be found out

 
0
  #4
May 23rd, 2006
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.
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct node
  6. {
  7. struct node *prev,*next;
  8. char line[20];
  9. };
  10.  
  11. void copy(struct node **,int start,int end,int desti);
  12. void Append(struct node **, char *);
  13. void Appendll(struct node **, char *);
  14. void Display(struct node **);
  15.  
  16. int main()
  17. {
  18. struct node *head = NULL;
  19. int ch, start, end, desti;
  20. char line[20];
  21. while ( 1 )
  22. {
  23. printf("Enter your choice: ");
  24. scanf("%d",&ch);
  25. if ( ch == 1 )
  26. {
  27. printf("Enter the data in the first ll ");
  28. scanf("%s",line);
  29. Append(&head,line);
  30. }
  31. else if ( ch == 2 )
  32. {
  33. Display(&head);
  34. }
  35. else if ( ch == 3 )
  36. {
  37. printf("Enter the start end and destination ");
  38. scanf("%d %d %d",&start,&end,&desti);
  39. copy(&head,start,end,desti);
  40. }
  41. else if ( ch == 0 )
  42. {
  43. break;
  44. }
  45. }
  46. return 0;
  47. }
  48.  
  49. void Append(struct node **list, char line[])
  50. {
  51. struct node *newnode = malloc (sizeof *newnode);
  52. if ( newnode )
  53. {
  54. strcpy(newnode->line, line);
  55. newnode->next = NULL;
  56. if ( *list == NULL )
  57. {
  58. newnode->prev = NULL;
  59. *list=newnode;
  60. }
  61. else
  62. {
  63. struct node *t = *list;
  64. while ( t->next != NULL )
  65. {
  66. t = t->next;
  67. }
  68. newnode->prev = t;
  69. t->next = newnode;
  70. }
  71. }
  72. }
  73.  
  74. void Appendll(struct node **dummy, char line[])
  75. {
  76. struct node *newnode = malloc(sizeof *newnode);
  77. if ( newnode )
  78. {
  79. strcpy(newnode->line,line);
  80. newnode->next=newnode->prev=NULL;
  81. if ( *dummy==NULL )
  82. {
  83. newnode->prev=NULL;
  84. *dummy=newnode;
  85. }
  86. else
  87. {
  88. struct node *temp=*dummy;
  89. while ( temp->next!=NULL )
  90. temp=temp->next;
  91. newnode->prev=temp;
  92. temp->next=newnode;
  93. }
  94. }
  95. }
  96.  
  97. void Display(struct node **head)
  98. {
  99. struct node *temp = *head;
  100. if ( temp==NULL )
  101. {
  102. printf("Empty");
  103. return;
  104. }
  105. printf("\n\t\t");
  106. do
  107. {
  108. printf("\n%s ", temp->line);
  109. temp = temp->next;
  110. }
  111. while ( temp != NULL );
  112. printf("NULL\n\n");
  113. }
  114.  
  115. void copy(struct node **head, int start, int end, int desti)
  116. {
  117. struct node *s = NULL, *dummy = NULL, *t = NULL;
  118. int i;
  119. char data[20];
  120. for ( i = 1, s = *head; i <= start; i++ )
  121. {
  122. s = s->next;
  123. }
  124. //copying the data into dummy link list
  125. for ( i = start; i <= end; i++ )
  126. {
  127. strcpy(data, s->line);
  128. Appendll(&dummy, data);
  129. }
  130. printf("Dummmy");
  131. Display(&dummy);
  132. //appending that dummy list to original one at destination
  133. for ( i = 1, s = *head; i<= desti; i++ )
  134. {
  135. s = s->next;
  136. }
  137.  
  138. // desti
  139. //head-> 1 2 3 4 5 s->=6= 7
  140. // dummy ->=1 2 3 4<-t NULL
  141. if ( s->next != NULL )
  142. {
  143. for ( t = dummy; t != NULL; t = t->next )
  144. {
  145. s->next->prev=t;
  146. t->next=s->next;
  147. s->next=dummy;
  148. dummy->prev=s;
  149. }
  150. }
  151. Display(head);
  152. }
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: Errors to be found out

 
0
  #5
May 23rd, 2006
Instead of
  1. scanf("%s",line);
Do something like
  1. fgets(line,20,stdin);
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1004 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC