Social Icons

banner image

DATA STRUCTURE PROGRAMMS

                                              SORTING LINKLIST IN AN ASCENDING ORDER

#include <stdio.h>
#include <conio.h>
#include <malloc.h>
struct node
{
                int data;
                struct node *next;
};

void main()
{
               
                inti;
                intnum ;
                struct  node *first, *nw, *pre, *new1, *count;
                clrscr();
                printf("\n Enter the number of node you want to create: ");
                scanf("%d", &num );
                first->next = NULL;
                nw = first;
                for (i = 0; i<num ; i++)
                {
                                nw->next = (struct node* ) malloc(sizeof(struct node));
                                nw = nw->next;
                                printf("\n Enter the node: %d: ", i+1);
                                scanf("%d", &nw->data);
                                nw->next = NULL;
                }
                new1 = first;
                for( ; new1->next != NULL; new1 = new1->next)
                {
                                for(count = new1->next; count != NULL; count = count->next)
                                {
                                                if(new1->data > count->data)
                                                {
                                                                int temp = new1->data;
                                                                new1->data = count->data;
                                                                count->data = temp;
                                                }
                                }
                }
                nw = first->next;
                printf("\n After sorting the Linked list is as follows:\n");
                while (nw)
                {
                                printf("%d\t", nw->data);
                                nw = nw->next;
                }
                getch();

}

Search  link list


#include<iostream>
#include<conio.h>
using namespace std;
void display();
void create();
void insert(int);
struct node
{
        char data;
        node*link;
};
node*start=NULL;
int main()
{
        intposition,ch,item;
        do
        {
                cout<<"Enter 1 to create, 2 to display, 3 to search, 4 to insert, 5 to delete";
                cin>>ch;
                if(ch==1)
                {
                        create();
                }
                else if(ch==2)
                {
                        display();
                }
       
                else if(ch==3)
                {
                        cout<<"Enter the position";
                        cin>>position;
                        insert(position);
                }
        }
        while(ch==1||2||3);
        getch();
        return 0;
}
void create()
{
        node *p, *q;
        p=new node;
        cout<<"Enter data of node";
        cin>>p->data;
        p->link=NULL;
        if(start==NULL)
        {
                start=p;
        }
        else
        {
                q=start;
                while(q->link!=NULL)
                {
                        q=q->link;
                }
                q->link=p;
        }      
}
void display()
{
        node*k;
        if(start==NULL)
        {
                cout<<"Empty";
        }
        else
        {
                for(k=start;k!=NULL;k=k->link)
                {
                        cout<<k,k->data,k->link;
                }
        }
}

void insert(int position)
{
        node*p,*q;
        p=new node;
        cout<<"Enter data part";
        p->link=NULL;
        if((position==1)||(start==NULL))
        {
                p->link=start;
                start=p;
        }
        else
        {
                q=start;
                int k=1;
                while((q->link!=NULL)&&(k!=position-1))
                {
                        k++;
                        q=q->link;
                }
                if(q->link==NULL)
                {
                        q->link=p;
                }
                else
                {
                        p->link=q->link;
                        q->link=p;
                }
        }

}

Circular link list

#include<iostream>
using namespace std;
#include<conio.h>
void create();
void display();
void search(int);
void insert(int);
void del(int);
struct node
{
        int data;
        node *link;
};
node *start=NULL;
int main()
{
        intch,item,pos;
        do
        {
                cout<<"enter 1 to create\n"
                <<"enter 2 to display\n"
                <<"enter 3 to search\n"
                <<"enter 4 to insert";
                cin>>ch;
                if(ch==1)
                    {
                        create();
                        }
                        else if(ch==2)
                        {
                                display();
                        }
                        else if(ch==3)
                        {
                                cout<<"enter item to be searched";
                                cin>>item;
                                search(item);
                        }
                        else if(ch==4)
                        {
                                cout<<"enter the pos u want to enter";
                                cin>>pos;
                                insert(pos);
                        }
                        else if(ch==5)
                        {
                                cout<<"enter the item u want to delete";
                                cin>>item;
                                del(item);
                        }
                        else
                        {
                                cout<<"invalid choice";
                        }
                }
                        while(ch==1||ch==2||ch==3||ch==4||ch==5);
                        getch();
                        return 0;       
}
void create()
{
        node *p=new node;
        cout<<"enter value";
        cin>>p->data;
        p->link=NULL;
        if(start==NULL)
        {
                start=p;
                start->link=start;
        }
        else
        {
                node *q=start;
                while(q->link!=start)
                {
                        q=q->link;
                }
                q->link=p;
                p->link=start;
        }
}
void display()
{
        if(start==NULL)
        {
                cout<<"empty";
        }
        else
        {
                node *q=start;
                while(q->link!=start)
                {
                        cout<<q<<"\t"<<q->data<<"\t"<<q->link;
                        cout<<endl;
                    q=q->link;
                }
                        cout<<q<<"\t"<<q->data<<"\t"<<q->link;
        }
}
void search(int item)
{
        node *q=start;
        int k=1;
        while((q->data!=item)&&(q->link!=start))
        {
                k++;
                q=q->link;
        }
        if(q->data==item)
        {
                cout<<"item is present at"<<k;
        }
        else
        {
                cout<<"item is not present";
        }
}
void insert(intpos)
{
        node *p=new node;
        node *q;
        cout<<"enter data pos";
        cin>>p->data;
        p->link=NULL;
        if(pos==1)
        {
                q=start;
                while(q->link!=start)
                {
                        q=q->link;
                }
                p->link=start;
                start=p;
                q->link=start;
        }
        else
        {
                q=start;
                int k=1;
        while((q->link!=start)&&(k!=pos-1));
            {
                k++;
                q=q->link;
                }
                if(q->link=start)
                {
                        q->link=p;
                        p->link=start;
                }
                else
                {
                        p->link=q->link;
                        q->link=p;
                }
        }
}
void del(int item)
{
        node *q,*q1,*ptr;
        cout<<"enter the item u wnt to delete";
        if(start->data==item)
        {
                if(start->link=start)
                {
                        ptr=start;
                        start=NULL;
                }
                else
                {
                        q=start;
                        while(q->link!=start)
                        {
                                q=q->link;
                        }
                        ptr=start;
                        start=start->link;
                        q->link=start;
                }
        }
                else
                {
                        q1=start;
                        q=start->link;
                        while((q->data!=item)&&(q->link!=start))
                        {
                                q1=q;
                                q=q->link;
                        }
                        if(q->data==item)
                        {
                                ptr=q;
                                q1->link=q->link;
                        }
                        else
                        {
                                cout<<"item cant b deleted not found!!!";
                        }
                        deleteptr;
                }
       
}

Parenthesis check in an expression in link list


#include<iostream>
 #include <string>
using namespace std;

struct node
    {
char data;
node *link;
    };

int pop(node *&stack)  //removing an element
    {
        char result;
        node *top=new node;
top=stack;
result=top->data;
stack=top->link;  
        delete top;
return result;
    }

bool Pairs(char openP,charclosedP)
    {
if(openP == '(' &&closedP == ')') return true;
else if(openP == '{' &&closedP == '}') return true;
else if(openP == '[' &&closedP == ']') return true;
else return false;
    }

bool Check(string exp)
    {
inti=0;
node *stack=NULL;
        while(exp[i])
        {
        if(exp[i]=='(' || exp[i]=='[')
               {
                                node *neww=new stack;
                                neww->data=exp[i];
                                neww->link=stack; 
                                stack=neww;
                        }
        if(exp[i]==')' || exp[i]==']')
              {
        if(stack==NULL)
                return 0;
        else if (Pairs(pop(stack), exp[i])==0) 
                return 0;                                                       
              }
        i++;
        }
        if(stack==NULL)
                return 1;
        else
        return 0;
    }

int main()
    {
        stringexp;
        cout<<"Enter parentheses:\n";
        cin>>exp;
        if(Check(exp)!=0)
        cout<<"P. are  balanced";
        else
        cout<<"P. are not balanced"; 
        return 0;
    }   



DATA STRUCTURE PROGRAMMS DATA STRUCTURE PROGRAMMS Reviewed by Marketing Thrills on November 08, 2015 Rating: 5

No comments:

Airtel Hackaton 2017

Powered by Blogger.