Thota Chakradhar - DS

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

THOTA CHAKRADHAR

2020CSB094

//PROGRAM TO ADD ELEMENT AT END OF LIST AND SAERCH FOR ELEMENT:

struct node

int data;

struct node*link;

};

struct node*add_at_end(struct node*head,int data)

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

ptr‐>data=data;

ptr‐>link=NULL;

if(head==NULL)

head=ptr;

ptr==NULL;

return head;

else{

struct node*tmp=head;

while(tmp‐>link!=NULL)

tmp=tmp‐>link;

tmp‐>link=ptr;

ptr,tmp=NULL;

return head;

int search_for_ele(struct node*head,int ele)

struct node*ptr=head;
int pos=1;

while(ptr!=NULL)

if(ptr‐>data==ele)

return pos;

else

ptr=ptr‐>link;

pos++;

int main()

struct node*head=NULL;

int x,data;

while(x!=0){

printf("Do you want to add node at end.(1/0)");

scanf("%d",&x);

if(x!=0){

printf("Enter the element:");

scanf("%d",&data);

head=add_at_end(head,data);

//PRINTING LIST

struct node*ptr=head;

while(ptr!=NULL)

printf("%d\t",ptr‐>data);

ptr=ptr‐>link;

}
ptr=NULL;

//SEARCHING ELEMENT

int e,pos;

printf("\nEnter the elelment to search:");

scanf("%d",&e);

pos= search_for_ele(head,e);

printf("your searched ele is at position=%d",pos);

return 0;

You might also like