link list program.

 #include<iostream>

#include<stdlib.h>

using namespace std;

void infront();

void inend();

void delfront();

void delend();

void inposition();

void delposition();

void display();

int count();

struct student

{

int s1,s2,sum;

struct student *next;

}*head,*last,*node,*temp,*prev;

int main()

{

int ch,ans;

while(1)

{

cout<<"\n PRESS 1 FOR INSERT FRONT : ";

cout<<"\n PRESS 2 FOR INSERT LAST : ";

cout<<"\n PRESS 3 FOR DELETE FRONT : ";

cout<<"\n PRESS 4 FOR DELETE LAST : ";

cout<<"\n PRESS 5 FOR INSERT BASED ON POSITION : ";

cout<<"\n PRESS 6 FOR DELETE BASED ON POSITION : ";

        cout<<"\n PRESS 7 FOR DISPLAY : ";

        cout<<"\n PRESS 8 FOR COUNT : ";

        cout<<"\n PRESS 9 FOR EXIT : ";

        cout<<"\n ENTER YOUR CHOICE : ";

        cin>>ch;

        switch(ch)

        {

        case 1:

        infront();

        break;

       

        case 2:

    inend();

break;

case 3:

    delfront();

break;

case 4:

    delend();

break;

case 5:

    inposition();

break;

case 6:

    delposition();

break;

case 7:

    display();

break;

case 8:

    ans=count();

    cout<<"\n COUNT IS : "<<ans;

break;

case 9:

exit(0);

default:

cout<<"\n INVALID INPUT : ";

}

}

return 0;

}

void infront()

{

node=(struct student*)malloc(sizeof(struct student));

cout<<"\n ENTER THE N1 : ";

cin>>node->s1;

cout<<"\n ENTER THE N2 : ";

cin>>node->s2;

node->sum=node->s1+node->s2;

if(head=='\0')

{

head=node;

node->next='\0';

last=node;

}

else

{

node->next=head;

head=node;

}

}

void inend()

{

node=(struct student*)malloc(sizeof(struct student));

cout<<"\n ENTER THE N1 : ";

cin>>node->s1;

cout<<"\n ENTER THE N2 : ";

cin>>node->s2;

node->sum=node->s1+node->s2;

if(head=='\0')

{

head=node;

last=node;

}

else

{

last->next=node;

last=node;

}

}

void delfront()

{

if(head=='\0')

{

cout<<"\n LIST IS EMPTY : ";

}

else

{

if(head->next=='\0')

{

temp=head;

head=head->next;

free(temp);

}

}

}

void delend()

{

if(head=='\0')

{

cout<<"\n LIST IS EMPTY : ";

}

else

{

temp=head;

while(temp->next!=last)

{

temp=temp->next;

}

struct student *s;

s=last;

last=temp;

last->next='\0';

free(temp);

}

}

void inposition()

{

int pos;

cout<<"\nENTER THE POSITION: ";

cin>>pos;

int ans=count();

if(pos<1||pos>ans+1)

{

cout<<"\nINVALID POSITION!";

}

else

{

if(pos==1)

infront();

else

{

if(pos==ans+1)

infront();

else

{

node=(struct student*)malloc(sizeof(struct student));

cout<<"\n ENTER THE N1 : ";

cin>>node->s1;

cout<<"\n ENTER THE N2 : ";

cin>>node->s2;

node->sum=node->s1+node->s2;

temp=head;

int i=1;

while(i!=pos)

{

prev=temp;

temp=temp->next;

i=i+1;

}

node->next=prev->next;

prev->next=node;

}

}

}

}


void delposition()

{

{

int pos;

cout<<"\nENTER THE POSITION: ";

cin>>pos;

if(head=='\0')

{

cout<<"\nLIST IS EMPTY";

}

else

{

int ans=count();

if(pos<1||pos>ans)

{

cout<<"\nINVALID POSITION";

}

else

{

if(pos==1)

delfront();

else

if(pos==ans)

delend();

else

{

int i=1;

temp=head;

while(i!=pos)

{

prev=temp;

temp=temp->next;

i=i+1;

}

cout<<"\n"<<temp->s1<<temp->s2<<temp->sum<<" IS DELETED";

prev->next=temp->next;

temp->next='\0';

free(temp);

}

}

}

}

}

void display()

{

temp=head;

while(temp!='\0')

{

cout<<temp->s1<<"+";

cout<<temp->s2<<"=";

cout<<temp->sum<<" -> ";

temp=temp->next;

}

cout<<"\n ";

}

int count()

{

int c=0;

if(head=='\0')

{

cout<<"\nLIST IS EMPTY";

}

else

{

temp=head;

while(temp!='\0')

{

c=c+1;

temp=temp->next;

}

}

return c;

}