Advertisement
Misbah_Uddin_Tareq

5.3.cpp

Dec 2nd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<malloc.h>
  4. struct l_list
  5. {
  6.     int info;
  7.     struct l_list *next;
  8. }start, *node;
  9.  
  10. int search(int);
  11. int ptr=0;
  12. int main()
  13. {
  14.     int no,i,item,pos;
  15.     //start=(struct l_list *)malloc(sizeof(struct l_list));
  16.     start.next=NULL;
  17.     node=&start;
  18.     printf("How many nodes, you want in linked list? ");
  19.     scanf("%d",&no);
  20.     printf("");
  21.     for(i=0;i<no;i++)
  22.     {
  23.         node->next=(struct l_list *)malloc(sizeof(struct l_list));
  24.         printf("Enter element in node %d: ",i+1);
  25.         scanf("%d",&node->info);
  26.         node=node->next;
  27.     }
  28.     node->next=NULL;
  29.     printf("Linked list(only with info field) is:");
  30.  
  31.     node=&start;
  32.     while(node->next!=NULL)
  33.     {
  34.         printf("%d  ",node->info);
  35.         node=node->next;
  36.     }
  37.     printf("Enter item to be searched : ");
  38.     scanf("%d",&item);
  39.     pos=search(item);
  40.     if(pos>0)
  41.     printf("Your item is found %d times",pos);
  42.     else
  43.     printf("Sorry! item is no in linked list.a");
  44.  
  45.     printf("%d\n",ptr);
  46.     return 0;
  47. }
  48.  
  49. int search(int item)
  50. {
  51.     int count=0;
  52.     int n=1;
  53.     node=&start;
  54.     while(node->next!=NULL)
  55.     {
  56.         if(node->info==item)
  57.         {
  58.             count++;
  59.  
  60.         }
  61.         node=node->next;
  62.         ptr++;
  63.     }
  64.     return count;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement