943,936 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1105
  • C RSS
May 23rd, 2006
0

urgent help required plz help

Expand Post »
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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sania kohli is offline Offline
3 posts
since May 2006
May 23rd, 2006
0

Re: urgent help required plz help

What is problem you are facing??
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
May 23rd, 2006
0

Re: Errors to be found out

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:
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sania kohli is offline Offline
3 posts
since May 2006
May 23rd, 2006
0

Re: Errors to be found out

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. }
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 23rd, 2006
0

Re: Errors to be found out

Instead of
  1. scanf("%s",line);
Do something like
  1. fgets(line,20,stdin);
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to compare two strings?
Next Thread in C Forum Timeline: file i/o irregularity





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC