Friday 24 November 2017

C++ Program to Implement Sorted Singly Linked List


Code:

#include    stdio.h
#include    conio.h
#include    iostream
using namespace std;
struct node
{
    int data;
    node *next;
}*p = NULL, *head = NULL, *q = NULL, *np = NULL;
int c = 0;
void create(int x)
{
    np = new node;
    np->data = x;
    np->next = NULL;
    if (c == 0)
    {
        head = np;
        p = head;
        p->next = head;
        c++;
    }
    else if (c == 1)
    {
        p = head;
        q = p;
        if (np->data < p->data)
        {
            np->next = p;
            head = np;
            p->next = np;
        }
        else if (np->data > p->data)
        {
            p->next = np;
            np->next = head;
        }
        c++;
    }
    else
    {
        p = head;
        q = p;
        if (np->data < p->data)
        {
            np->next = p;
            head = np;
            do
            {
                p = p->next;
            }
            while (p->next != q);
            p->next=head;
        }
        else if (np->data > p->data)
        {
            while (p->next !=head && q->data < np->data)
            {
                q = p;
                p = p->next;
                if (p->next == head  && (p->data < np->data))
                {
                    p->next = np;
                    np->next = head;
                }
                else if (np->data < p->data)
                { 
                    q->next = np;
                    np->next = p;
                    break;
                }
            }
        }
    }
}
void traverse(int i)
{
    node *t = head;
    int c = 0;
    while (c <= i)
    {
        cout<data<<"\t";
        t = t->next;
        c++;
    }
}
int main()
{
    int i = 0, n, x;
    cout<<"enter the no of nodes\n";
    cin>>n;
    while (i < n)
    {
        cout<<"\nenter value of node\n";
        cin>>x;
        create(x);
        i++;
    }
    cout<<"\n\nlinear display of nodes currently present in circularly linked list....\n\n";
    traverse(n);
    getch();
}


Output:

enter the no of nodes
7

enter value of node
4

enter value of node
6

enter value of node
1

enter value of node
9

enter value of node
3

enter value of node
8

enter value of node
2


linear display of nodes currently present in circularly linked list....

1       2       3       4       6       8       9       1


More C++ Programs:















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...