Created
September 28, 2017 02:15
-
-
Save thinkphp/5bce2d4e4aa9a6aaf8532354ab8dcf2f to your computer and use it in GitHub Desktop.
Insert Sort using Linked List in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#define MAXINT 32000 | |
using namespace std; | |
struct Node { | |
int info; | |
struct Node *next; | |
}; | |
struct Node *base, | |
*curr, | |
*c, | |
*c1; | |
int main() { | |
int size, elem; | |
cout<<"The size of Array"; | |
cin>>size; | |
base = new struct Node; | |
base->info = MAXINT; | |
base->next = 0; | |
for(int i = 1; i <= size; ++i) { | |
cout<<"v["<<i<<"]="; | |
cin>>elem; | |
curr = new struct Node; | |
curr->info = elem; | |
if(curr->info < base->info) { | |
curr->next = base; | |
base = curr; | |
} else { | |
c = base; | |
c1 = c->next; | |
while(c1->info < elem) { | |
c = c->next; | |
c1 = c1->next; | |
} | |
c->next = curr; | |
curr->next = c1; | |
} | |
} | |
c = base; | |
while(c->info != MAXINT) { | |
cout<<c->info<<" "; | |
c = c->next; | |
} | |
return(0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment