Skip to content

Instantly share code, notes, and snippets.

@abhijangda
Created August 21, 2013 06:17
Show Gist options
  • Select an option

  • Save abhijangda/6290882 to your computer and use it in GitHub Desktop.

Select an option

Save abhijangda/6290882 to your computer and use it in GitHub Desktop.
//INSERTING ELEMENTS IN THE LIST IN ASCENDING ORDER
#include<iostream>
#include<malloc.h>
using namespace std;
struct Node{
int DATA;
struct Node *link;
}*START;
void insert(int item)
{
struct Node *newNode;
newNode=(struct Node *)malloc(sizeof(struct Node));
newNode->DATA=item;
newNode->link=NULL;
if(START==NULL)
{
START=newNode;
return;
}
struct Node *TEMP,*SAVE;
TEMP=START;
while(TEMP!=NULL)
{
if(TEMP->DATA > item)
{
if(TEMP==START)
{
newNode->link=START;
START=newNode;
}
else
{
SAVE->link=newNode;
newNode->link=TEMP;
}
return;
}
SAVE=TEMP;
TEMP=TEMP->link;
}
SAVE->link=newNode;
}
void display()
{
if(START==NULL)
{
cout<<"Empty List\n";
return;
}
struct Node *TEMP;
TEMP=START;
while(TEMP!=NULL)
{
cout<<TEMP->DATA<<" ";
TEMP=TEMP->link;
}
cout<<endl;
}
int main()
{
START=NULL;
cout<<"Enter the number of nodes in the list: ";
int n;
cin>>n;
while(n)
{
cout<<"Enter the data: ";
int data;
cin>>data;
insert(data);
n--;
}
display();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment