Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Is my merge list right?

Is my merge list right?

Scheduled Pinned Locked Moved C / C++ / MFC
questionlearning
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    wbgxx
    wrote on last edited by
    #1

    struct Node { int data; Node *next; }; typedef struct Node Node; Node *Reverse(Node *head); Node *Merge(Node *head1, Node *head2) { if (head1 == NULL) return head2; if (head2 == NULL) return head1; Node *head = NULL; Node *p1 == NULL; Node *p2 == NULL; if (head1->data > head2->data) { head->next = head1; p1 = head1->next; p2 = head2; } else { head->next = head2; p1 = head1; p2 = head2 -> data; } // I want to see is that why some book in this step using " Node *pcurrent =head" //and I dont not know why,coould someone give me a detail, thanks in advantage! while (p1!=NULL && p2!=NULL) { if (p1->data > p2->data) { head->next =p1; head = p1; p1 = p1->next; } else { head -next = p2; head = p2; p2 = p2->next; } } while (p1 != NULL) { head->next = p1; head = p1; p1 = p2->next; } while (p2 != NULL) { head -next = p2; head = p2; p2 = p2->next; } return head; }

    L 1 Reply Last reply
    0
    • W wbgxx

      struct Node { int data; Node *next; }; typedef struct Node Node; Node *Reverse(Node *head); Node *Merge(Node *head1, Node *head2) { if (head1 == NULL) return head2; if (head2 == NULL) return head1; Node *head = NULL; Node *p1 == NULL; Node *p2 == NULL; if (head1->data > head2->data) { head->next = head1; p1 = head1->next; p2 = head2; } else { head->next = head2; p1 = head1; p2 = head2 -> data; } // I want to see is that why some book in this step using " Node *pcurrent =head" //and I dont not know why,coould someone give me a detail, thanks in advantage! while (p1!=NULL && p2!=NULL) { if (p1->data > p2->data) { head->next =p1; head = p1; p1 = p1->next; } else { head -next = p2; head = p2; p2 = p2->next; } } while (p1 != NULL) { head->next = p1; head = p1; p1 = p2->next; } while (p2 != NULL) { head -next = p2; head = p2; p2 = p2->next; } return head; }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, please edit your post and add: 1. PRE tags instead of CODE tags, for better readability; 2. a textual description of your linked lists; e.g. is head supposed to contain any data, or is it only pointing to the first real node? 3. a textual description of what Merge is supposed to do (which must include preconditions: what is assumed about its parameters, and what is promissed about its return value). Without it, the only thing anyone can say is "if it compiles, it will do what it will do". :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Getting an article published on CodeProject should be easier and faster for Bronze and Silver authors.


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups