Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<malloc.h>
- struct node * addelement(struct node * ,int);
- void display(struct node *);
- void search(struct node *, int);
- struct node
- {
- int data;
- struct node *ptr;
- };
- int main()
- {
- int a,b,num;
- struct node *head;
- printf(" \n Enter the elements to be added to the linked list ");
- scanf("%d %d",&a,&b);
- head=NULL;
- head=addelement(head,a);
- head=addelement(head,b);
- display(head);
- printf("\n The element to be searched in the list ");
- scanf("%d",&num);
- search(head,num);
- }
- struct node * addelement(struct node *head,int info)
- {
- struct node *newnode;
- newnode=(struct node *)malloc(sizeof(struct node));
- newnode->data=info;
- if(head==NULL)
- {
- newnode->ptr=NULL;
- return newnode;
- }
- else
- {
- newnode->ptr=head;
- return newnode;
- }
- }
- void display(struct node *head)
- {
- while(head!=NULL)
- {
- printf("%d\t",head->data);
- head=head->ptr;
- }
- }
- void search(struct node *head,int x)
- { int flag=0;
- while(head!=NULL)
- {
- if(head->data==x)
- {
- flag=1;
- break;
- }
- else
- {
- head=head->ptr;
- }
- }
- if(flag==1)
- printf("\n The element %d is found in the list",x);
- else
- printf("\n The element is not present in the list \n");
- }
- 41,1-8
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement