LL4

#include <stdio.h>
#include<stdlib.h>
struct Node
{};
struct node
{
    int data;
    struct node* link;
};
struct node* root=NULL;
void display();
void addatend();
void Create()
{}
void addbef(int,int);
int main() {
    int  ch,i,n,x;
  scanf("%d",&ch);

  for(i=0;i<ch;i++)
  {
   addatend();
  }
  scanf("%d",&n);
  scanf("%d",&x);
  addbef(n,x);
  printf("Linked List : ");
  display();
  return 0;
}
void addatend()
{
    struct node* temp;
    temp=(struct node*)malloc(sizeof(struct node));
    //printf("Enter node data\n");
    scanf("%d",&temp->data);
    temp->link=NULL;
    if(root==NULL)
    {
        root=temp;
    }
    else
    {
        struct node* p;
        p=root;
        while(p->link!=NULL)
        {
            p=p->link;
        }
        p->link=temp;
        }
}
void display()
{
    struct node* temp;
    temp=root;
    if(temp==NULL)
    {
        printf(" ");
       
    }
    else
    {
        while(temp!=NULL)
        {
            printf("->%d",temp->data);
            temp=temp->link;
        }
    }
}
void addbef(int n,int x)
{
  int flag=0;
  struct node *temp=root;
  if(temp->data==n)
  {
     struct node * t=(struct node*)malloc(sizeof(struct node));
  t->data=x;
  t->link=root;
  root=t;
  }
else
{
  while(temp->link!=NULL)
  {
    if(temp->link->data==n)
    {
      flag=1;
      break;
    }
  temp=temp->link;
  }
  if(flag==1)
  {
  struct node * t=(struct node*)malloc(sizeof(struct node));
  t->data=x;
  t->link=temp->link;
  temp->link=t;
  }
  else
  {
    printf("Node not found! \n");
  }
}
}

2 comments:

  1. This is not working 0% on evaluation

    ReplyDelete
    Replies
    1. Now its working on 4th time evaluation
      What the shit is this elab

      Delete

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