Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. struct node* copyList()
  2. {
  3. //check if empty
  4. if(head==NULL){
  5. return NULL;}
  6. //create head
  7. struct node* p_head=head;//pointer to original head
  8. struct node* newHead = NULL; //head of new list
  9. struct node* p_newHead=newHead;//pointer to new head
  10. while(p_head!=NULL)
  11. int data=p_head->data;
  12. m_appendNode(data, p_newHead);
  13. p_head=p_head->next;
  14. }
  15. return newHead;
  16. }
  17.  
  18. void m_appendNode(int data, struct node *newHead)
  19. {
  20. struct node* current=newHead;
  21. if(newHead!=NULL){
  22. while((current->next)!=NULL){
  23. current = current->next;
  24. }
  25. current->next=malloc(sizeof(struct node));
  26. current->next->data=data;
  27. current->next->next=NULL;
  28. }
  29. else{
  30. struct node* newnode=malloc(sizeof(struct node));
  31. newnode->data=data;
  32. newHead=newnode;
  33. newnode->next=NULL;
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement