LL17

#include<stdio.h>
#include<stdlib.h>
struct Node
{
    int data;
    struct Node* next;
};
void push(struct Node** head_ref, int new_data)
{
    struct Node* new_node =(struct Node*) malloc(sizeof(struct Node));
    new_node->data  = new_data;
    new_node->next = (*head_ref);
    (*head_ref)    = new_node;
}
int count(struct Node* head, int search_for)
{
    struct Node* current = head;
    int count = 0;
    while (current != NULL)
    {
        if (current->data == search_for)
           count++;
        current = current->next;
    }
    return count;
}
int main()
{
    struct Node* head = NULL;
int n,b[20],i,a,j;
  scanf("%d",&n);
  printf("Linked list : ");
  for(i=0,j=n-1;i<n;i++,j--)
  {scanf("%d",&a);
   b[j]=a;
   push(&head, a);}
  for(i=0;i<n;i++)
    printf("-->%d",b[i]);
  scanf("%d",&a);
      printf("\nCount of %d : %d",a, count(head, a));
    return 0;
}

8 comments:

  1. Thank me later!

    #include
    using namespace std;



    int pos=-1,count=0,flag=0;

    struct node{

    int info;
    node* next;

    }*start,*save,*ptr,*newptr,*last,*wowptr;



    node* Createnode(int inf){

    ptr=new node;
    ptr->info=inf;
    ptr->next=NULL;
    return ptr;

    }


    void push(node* np){

    if(start==NULL){
    start=last=np;
    }

    else{

    last->next=np;
    last=np;

    }

    }


    void display(node *np){


    cout<<"\nLinked list : ";
    while(np!=NULL){

    cout<<"-->"<info;
    np=np->next;

    }

    }



    void numofoccur(node *noc,int num){

    while(noc!=NULL){

    if(noc->info==num){
    flag++;}

    noc=noc->next;
    }

    }

    void push(struct node** head_ref, int new_data){
    struct node* new_node =(struct node*) malloc(sizeof(struct node));
    new_node->info = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
    }





    int main(){


    int N;

    int info;
    cin>>N;

    while(N--){

    cin>>info;
    newptr=Createnode(info);
    push(newptr);


    }


    int ele;
    cin>>ele;

    display(start);

    numofoccur(start,ele);

    cout<<"\nCount of "<<ele<<" : "<<flag;
    return 0;
    }

    ReplyDelete
  2. #include
    using namespace std;
    struct node{
    int data;
    struct node *next;
    }*first=NULL;
    void create(int A[],int n)
    {
    int i;
    struct node *last;
    first=(struct node *)malloc(sizeof(struct node));
    first->data=A[0];
    first->next=NULL;
    last=first;
    for(i=1;idata=A[i];
    new_node->next=NULL;
    last->next=new_node;
    last=new_node;
    }
    }
    void Display(struct node *p)
    {
    cout<<"Linked list : ";
    while(p!=NULL)
    {
    cout<<"-->"<data;
    p=p->next;
    }
    }
    void count(struct node *k,int key)
    {
    int c=0;
    while(k!=NULL)
    {
    if(k->data==key) {c++;}
    k=k->next;
    }
    cout<>n;
    int a[n];
    for(int i=0;i>a[i];}
    int k;
    cin>>k;
    create(a,n);
    Display(first);
    count(first,k);
    return 0;
    }

    ReplyDelete
  3. The original is correct just in the last loop print the LL in reverse.

    ReplyDelete
  4. #include
    #include
    struct node
    {
    int data;
    struct node*next;
    };
    void push(struct node**head_ref,int new_data)
    {
    struct node* new_node=(struct node*)malloc(sizeof(struct node));
    new_node->data=new_data;
    new_node->next=(*head_ref);
    (*head_ref)=new_node;
    }
    int count(struct node*head,int search_for)
    {
    struct node*current=head;
    int count=0;
    while (current !=NULL)
    {
    if (current->data==search_for)
    count++;
    current=current->next;
    }
    return count;
    }
    int main()
    {
    struct node*head = NULL;
    int N,b[20],i,X,j;
    scanf("%d",&N);
    printf("Linked list : ");
    for(i=0,j=N-1;i=0;i--)
    printf("-->%d",b[i]);
    scanf("%d",&X);
    printf("\nCount of %d : %d",X,count(head,X));
    return 0;
    }

    ReplyDelete

SRM ELAB SOLUTUONS   DATA-STRUCTURE                                                                             **IF THE PROGRAM DON...