Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created September 28, 2017 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/5bce2d4e4aa9a6aaf8532354ab8dcf2f to your computer and use it in GitHub Desktop.
Save thinkphp/5bce2d4e4aa9a6aaf8532354ab8dcf2f to your computer and use it in GitHub Desktop.
Insert Sort using Linked List in c++
#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