Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node
- {
- int data;
- Node *point;
- };
- Node *head;
- Node* getnewnode(int value)
- {
- Node *newnode = new Node();
- newnode -> data = value;
- newnode -> point = NULL;
- return newnode;
- }
- void Insert_any_index(int n, int value)
- {
- Node *newnode= getnewnode(value);
- if(n==1)
- {
- newnode->point = head;
- head = newnode;
- return;
- }
- Node *temp = head;
- for(int i=1; i<n-1; i++)
- {
- temp = temp->point;
- }
- newnode->point = temp->point;
- temp->point = newnode;
- }
- void Display()
- {
- Node *temp = head;
- cout << "\nCurrent Link list data....."<<endl;
- while(temp != NULL )
- {
- cout << temp->data << " ";
- temp = temp->point;
- }
- cout <<endl;
- }
- void Display_Sorted_linklist()
- {
- Node *temp = head;
- int arr[100],i=0,j=0,temp2,n;
- while(temp != NULL )
- {
- arr[i]=temp->data;
- temp = temp->point;
- i++;
- }
- n = i;
- for(j=0; j<n-1; j++)
- {
- for(i=j+1; i<n; i++)
- {
- if(arr[j]>arr[i])
- {
- temp2 = arr[j];
- arr[j] = arr[i];
- arr[i] = temp2;
- }
- }
- }
- cout << "\nSorted Link list data...."<<endl;
- cout << "Start";
- for(i=0; i<n; i++)
- {
- cout << "->" << arr[i];
- }
- cout << "->End"<<endl;;
- }
- int main()
- {
- int data,limit,i,Del_index,data2,index;
- cout << "Input size of link list: ";
- cin >> limit;
- for(i=1; i<=limit; i++)
- {
- cout << "Input data: ";
- cin >> data;
- Insert_any_index(i,data);
- }
- Display();
- Display_Sorted_linklist();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment