Single listed list appened ,atbegin,delete,insert middle


#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node* link;

};

struct  node* root=NULL;
void appened()
{
    struct node* temp;
    temp=(struct node*)malloc(sizeof(struct node));
        printf("enter node data\n");
        printf("temp add %p\n",temp);
        scanf("%d",&temp->data);
        temp->link=NULL;
    if(root==NULL)
    {
        printf("node create");
        printf("temp add %p\n",temp);
        root=temp;
    }
   
    else
    {
        struct node* p;
        p=root;
        while(p->link!=NULL)
        {
            printf("%p",p->link);
            p=p->link;
        }
        p->link=temp;
    }
}
int main()
{
    appened();
    appened();
    appened();
    appened();
}

 #include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node* link;
};
struct node* root;
void appened()
{
    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()
{       int count=0;
    struct node* temp;
    temp=root;
    while(temp!=NULL)
    {
        count++;
        printf("%d\t",temp->data);
        temp=temp->link;
    }
        printf("number of  %d\t",count);
}
int main()
{
    appened();
    appened();
    appened();
    display();

}

 #include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node* link;
};
struct node* root;
void atbegin()
{
    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
    {
        temp->link=root;
        root=temp;
    }

}
void display()
{       int count=0;
    struct node* temp;
    temp=root;
    while(temp!=NULL)
    {
        count++;
        printf("%d\t",temp->data);
        temp=temp->link;
    }
        printf("number of  %d\t",count);
}
int main()
{
  
    atbegin();
    atbegin();
    atbegin();
    display();

}


#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node* link;
};
struct node* root;
void addbegin()
{
              struct node* temp;
        temp=(struct node*)malloc(sizeof(struct node));
        printf("enter data\n");
        scanf("%d",&temp->data);
        temp->link=NULL;

        if(root==NULL)
        {
            root=temp;
        }
        else
        {
            temp->link=root;
            root=temp;
        }
   
}
void length()
{      
   
    struct node* temp;
    temp=root;
    int len=0;
    while(temp!=NULL)
    {
        len++;
        temp=temp->link;
    }
    printf(" len %d\t",len);

}

void middle()
{
    int location,len=0;
    struct node* temp;
    printf("enter location\t");
    scanf("%d",&location);
    temp=root;
    while(temp!=NULL)
    {
        temp=temp->link;
        len++;
        printf("len %d\t",len);
    }
    if(len<location)
    {
        printf("limit over flow\n");
    }
    else
    {
        struct node*temp;
        temp=(struct node*)malloc(sizeof(struct node));
        printf("enter data\n");
        scanf("%d",&temp->data);
        temp->link=NULL;
        struct node* p;
        p=root;
        int i=1;
        while(i>location)
        {
            p=p->link;
            i++;

        }
        temp->link=p->link;
        p->link=temp;
       

    }
   

}
void display()
{
    struct node *temp;
    temp=root;
    while(temp!=0)
    {
        printf("%d\t",temp->data);
        temp=temp->link;
    }
}
void delet()
{
    struct node* temp,*p,*q;
    temp=(struct node*)malloc(sizeof(struct node));


    if(root==NULL)
    {
        printf("no data present \n");
        exit(0);
    }
          int location,i=1;
      printf("enter your location\n");
      scanf("%d",&location);
      p=root;
      if(location==1)
      {
                     temp=root;
          root=temp->link;
          temp->link=NULL;
          free(temp);
         
      }
      while(i<(location-1))
      {
          p=p->link;
          i++;
      }
      q=root;
      while(i<location)
      {
          q=q->link;
          i++;
      }
      q=p->link;
      p->link=q->link;
      q->link=NULL;
      free(q);


}
int main()
{
addbegin();
addbegin();
addbegin();
length();
delet();
//middle();
display();
}

Comments

Popular posts from this blog

circular linked list

calendar program odd days method

C in Depth