Wednesday, June 20, 2018

Print Data Of Single Link List

#include <iostream>

using namespace std;
struct node
{
    int data;
    node *n;
};
struct node *h,*e,*t;

void checkempty()
{
    if(h==0)
    {
        cout<<"List  is empty..."<<endl;
    }
    else
    {
        cout<<"List is not empty"<<endl;
    }
}
void create1stnode()
{
  t=new node;
  cout<<"Enter Data:"<<endl;
  cin>>t->data;// if we enter 8
  t->n=0;
  e=t;//because here our first and last node is t....
  h=t;
}
void addmore()
{
    if(h==0)
    {
        cout<<"List doesn't exist..1st create the list"<<endl;

    }
    else
    {
        t=new node;
         cout<<"Enter Data:"<<endl;
  cin>>t->data;// if we enter 10...
  e->n=t;
  e=t;
  e->n=0;

    }
}
void printlist()
{
    if(h==0)
    {
        cout<<"List is Empty"<<endl;

    }//To check is list empty or not.....
    else if(h!=0)
    {
       for(t=h; t!=0; t=t->n)
       {
           cout<<t->data<<endl;
       }
    }
}
int main()
{
     int ch;
    label:
cout<<"======================================================================================================================="<<endl;
    cout<<"1. Create List"<<endl;
    cout<<"2. Add More Data:"<<endl;
        cout<<"3. Check List is Empty or Not!"<<endl;
cout<<"4. Print List"<<endl;

     cin>>ch;
    if(ch==1)
    {
        create1stnode();
        cout<<"Data inserted Successfully"<<endl;
    goto label;
    }
    else if(ch==2)
    {
        addmore();
        goto label;
        cout<<"\n\n\n";
    }
    else if(ch==3)
    {
        checkempty();
        goto label;
    }
    else if(ch==4)
    {
        printlist();
        goto label;
    }
    return 0;
}





Create Next Nodes Of Single Link List

#include <iostream>

using namespace std;

struct node
{
    int data;
    node *n;
};

struct node *h,*e,*t;

void checkempty()
{
    if(h==0)
    {
        cout<<"List is Empty!"<<endl;
    }
    else
    {
        cout<<"List is not Empty"<<endl;
    }
}
void createlist()
{
    t=new node;
    cout<<"Enter Data in first Node: "<<endl;
    cin>>t->data;
    t->n=0;
    e=t;
    h=t;

}

void addmore()
{
    if(h==0)
    {
        cout<<"List doesn't Exist.! 1st create the list......"<<endl;
    }
    else if(h!=0)
    {

    t=new node;
    e->n=t;
    e=t;
    cout<<"Enter Data: "<<endl;
    cin>>t->data;
    e->n=0;

}
}
int main()
{
 int ch;
    label:
cout<<"======================================================================================================================="<<endl;
    cout<<"1. Create List"<<endl;
    cout<<"2. Add More Data:"<<endl;
    cout<<"3. Check Empty"<<endl;
    cin>>ch;
    if(ch==1)
    {
        createlist();
        cout<<"Data inserted Successfully"<<endl;
    goto label;
    }
    else if(ch==2)
    {
        addmore();
        goto label;
        cout<<"\n\n\n";
    }


else if(ch==3)
    {
        checkempty();
         cout<<"\n\n";
        goto label;
    }

return 0;
}




Create first Node of Single Link List

#include <iostream>

using namespace std;

struct node
{
    int data;
    node *n;
};
struct node *h,*e,*t;
void create1stNode()
{
    t=new node;
    cout<<"Enter Data in first Node: "<<endl;
    cin>>t->data;
    t->n=0;
    e=t;
    h=t;

}
int main()
{
create1stNode();

 return 0;
}