Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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