Created
August 21, 2013 06:17
-
-
Save abhijangda/6290882 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| //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