Visit: https://mediapriimr.viden.io
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};
//struct node* head;
void insert(struct node** head,int x)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->next=*head;
*head=temp;
//return head;
}
void deleteList(struct node** head,int pos)
{
struct node* temp,*prev;
int count=0;
temp=(*head);
while(temp!=NULL)
{
prev=temp;
temp=temp->next;
count++;
if(count==pos)
{
if(temp->next==NULL)
{
prev->next=NULL;
break;
}
prev->next=temp->next;
break;
}
}
}
void reverse(struct node** head)
{
struct node* current,*ptr=NULL,*next=NULL;
current=(*head);
while(current!=NULL)
{
next=current->next;
current->next=ptr;
ptr=current;
current=next;
}
(*head)=ptr;
}
void append(struct node** head_ref,int data)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
struct node *last= *head_ref;
temp->data=data;
temp->next=NULL;
if((*head_ref) == NULL)
{
(*head_ref)= temp;
return;
}
while(last->next!= NULL)
{
last=last->next;
}
last->next=temp;
//temp->next=NULL;
}
bool search(struct node* head, int x)
{
struct node* current = head;
while (current != NULL)
{
if (current->data == x)
return true;
current = current->next;
}
return false;
}
void print(node* head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf("\n");
}
int main()
{
struct node* head=NULL;
int n,i,x;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x);
append(&head,x);
}
// deleteList(&head,3);
print(head);
reverse(&head);
print(head);
search(head, 21)? printf("Yes") : printf("No");
}