| | |
urgent help required plz help
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2006
Posts: 3
Reputation:
Solved Threads: 0
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 :
i am a new user so sorry for any mistalke
Here is the code :
C Syntax (Toggle Plain Text)
#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); }
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.
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.
C Syntax (Toggle Plain Text)
#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); }
"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
Instead of
Do something like
C Syntax (Toggle Plain Text)
scanf("%s",line);
C Syntax (Toggle Plain Text)
fgets(line,20,stdin);
![]() |
Similar Threads
- how to write code for update,delete,cancel buttons in datagrid using vb.net only..plz (IT Professionals' Lounge)
- final year project help required plz (Networking Hardware Configuration)
- :: help required for ftp server implementation:: (C)
Other Threads in the C Forum
- Previous Thread: How to compare two strings?
- Next Thread: file i/o irregularity
Views: 1004 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm command copyanyfile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework ide include initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf openwebfoundation overwrite pause pdf pointer posix power process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads turboc unix urboc user whythiscodecausesegmentationfault win32api windowsapi






