Single linked List Lab program

 #include<stdio.h>

#include<stdlib.h>

struct node

{

    int data;

    struct node *link;

};

struct node *root=NULL;

void create()

{

 struct node *second,*third,*fourth,*fifth;

 clrscr();

 root=malloc(sizeof(struct node));

 second=malloc(sizeof(struct node));

 third=malloc(sizeof(struct node));

 fourth=malloc(sizeof(struct node));

 fifth=malloc(sizeof(struct node));

 root->data=10;

 second->data=20;

 third->data=30;

 fourth->data=40;

 fifth->data=50;

 root->link=second;

 second->link=third;

 third->link=fourth;

fourth->link=fifth;

fifth->link=NULL;

}

void addFirst(int val)

{

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

temp->data=val;

temp->link=root;

root=temp;

printf("new node is inserted at beginning");

}

void addEnd(int val)

{

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

temp->data=val;

temp->link=NULL;

if(root==NULL)

{

root=temp;

}

else

{

struct node *p;

p=root;

while(p->link!=NULL)

{

p=p->link;

}

p->link=temp;

printf("node is inserted at end");

}

}

void addposition(int val)

{

int loc,i=1;

struct node  *p,*temp;

temp=malloc(sizeof(struct node));

temp->data=val;

temp->link=NULL;

printf("enter the location");

scanf("%d",&loc);

p=root;

while(i<loc)

{

p=p->link;

i++;

}

temp->link=p->link;

p->link=temp;

}

void deleteBeginEndPos(int key)

{

struct node *temp;

temp=root;

if(temp->data==key)

{

root=root->link;

free(temp);

}

else

{

struct node *p=root;

while(p->link!=NULL)

{

if(p->link->data==key)

{

temp=p->link;

p->link=temp->link;

free(temp);

break;

}

else

{

p=p->link;

}

}

}

}

int search(int key)

{

struct node *temp;

temp=root;

while(temp!=NULL)

{

if(temp->data==key)

   return 1;

temp=temp->link;

}

return -1;

}

void display()

{

struct node *temp;

temp=root;

while(temp!=NULL)

{

printf("%d--",temp->data);

temp=temp->link;

}

}

void main()

{

int op,val,x;

    create();

    while(1)

    {

printf("\n linked list menu\n");

printf("1.insert at begin\n");

printf("2.insert at end\n");

printf("3.insert at pos\n");

printf("4.delete at beg,end,pos\n");

printf("5.search\n");

printf("6.display\n");

printf("7.exit\n");

printf("choose one option\n");

scanf("%d",&op);

switch(op)

{

case 1:printf("\n enter a new node data:");

       scanf("%d",&val);

       addFirst(val);

       break;

case 2:printf("\n enter a new end element data:");

       scanf("%d",&val);

       addEnd(val);

       break;

case 3:printf("\n enter a element value:");

       scanf("%d",&val);

       addposition(val);

       break;

case 4:printf("\n enter a location:");

       scanf("%d",&val);

       deleteBeginEndPos(val);

       break;

case 5:printf("\n enter a element to search:");

       scanf("%d",&val);

       x=search(val);

       if(x==1)

       printf("\n element is found");

       else

       printf("\n element not found");

       break;

case 6:display();

       break;

case 7:exit(1);

       break;

default:

    printf("choose valid option\n");


}


    }

}

Comments

Popular posts from this blog

Conversion of Infix to Postfix Notation lab program

Unit-2 chapter-1 STACKS AND ITS APPLICTIONS

Data Structures UNIT-1 Chapter-1