Wednesday, May 13, 2015

singlely Linked list



#include<stdio.h>
#include<stdlib.h>
struct list
{
        int data;
        struct list *next;
};
void addnode(int data);
void insert(int data,int count);
void deletefirst();
void deletemiddle(int count);
int length();
void deletelast(int count);
int getnth(int get);
void deletelist(int count);
void display();
struct list *head=NULL;
struct list *temp;
struct list *temp1;
void addnode(int data)
{

        temp=head;
        if(head==NULL)
        {
                head=(struct list *)malloc(sizeof(struct list));
                head->data=data;
                head->next=NULL;
                head=head;
        }
        else
        {
                while(temp->next!=NULL)
                {
                        temp=temp->next;
                }
                temp1=(struct list *)malloc(sizeof(struct list));
                temp1->data=data;
                temp1->next=NULL;
                temp->next=temp1;
        }
}
void insert(int data,int count)
{
//      struct list *temp;
        struct list *new;
        int i=1;
        temp=head;
        while(i<(count-1))
        {
                temp=temp->next;
                i++;
        }
        new=(struct list *)malloc(sizeof(struct list));
        new->data=data;
        new->next=temp->next;
        temp->next=new;
}
void deletefirst()
{

        temp=head;
        temp1=head;
        temp=temp->next;
        head=temp;
        free(temp1);
}
void deletemiddle(int count)
{
        int i=1;
        temp=head;
        temp1=head;
        while(i<count)
        {
                temp1=temp1->next;
                i++;
        }
        i=1;
        while(i<(count-1))
        {
                temp=temp->next;
                i++;
        }
        temp->next=temp1->next;
        free(temp1);
}
int length()
{
        int count=0;
        temp=head;
        while(temp!=NULL)
        {
                temp=temp->next;
                count++;
        }
        return count;
}
void deletelast(int count)
{
        int i=1;
        temp=head;
        temp1=head;
        while(i<count)
                {
                        temp1=temp1->next;
                        i++;
                }
        i=1;
        while(i<(count-1))
                {
                        temp=temp->next;
                        i++;
                }
        temp->next=NULL;
        free(temp1);

}
int getnth(int get)
{
        int i=1,q;
        temp=head;
        while(i<get)
        {
                temp=temp->next;
                i++;
        }
        q=temp->data;
        return q;
}
void deletelist(int count)
{
        int a,i=1;
        temp=head;
        temp1=head;
        while(i<count)
                {
                        temp1=temp1->next;
                        i++;
                }
        i=1;
        while(i<(count-1))
                {
                        temp=temp->next;
                        i++;
                }
        temp->next=NULL;
        free(temp1);
        a=count-1;
        if(a>0)
        {
        deletelist(a);
        }
        head=NULL;
}
void display()
{
        struct list *temp;
        temp=head;
        if(temp==NULL)
        {
        printf("No records found and cannot delete");
        return;
        }
        while(temp!=NULL)
        {
                printf("%d\n",temp->data);
                temp=temp->next;
        }
}
int main()
{
        int count,options,pos,q;
while(1)
        {
                printf("Press 1 for Insert node\n");
                printf("Press 2 for delete first node\n");
                printf("Press 3 for delete node\n");
                printf("Press 4 for delete last node\n");
                printf("Press 5 for get node\n");
                printf("press 6 for delete list\n");
                printf("Press 7 for display\n");
                printf("Press 8 for exit\n");
                scanf("%d",&options);
                switch(options)
                {

case 1:
                        printf("Enter the data:\n");
                        scanf("%d",&pos);
                        addnode(pos);
                        break;
case 2:
                        deletefirst();
                        break;
case 3:
                        printf("Enter the data:\n");
                        scanf("%d",&pos);
                        deletemiddle(pos);
                        break;
case 4:
                        count=length();
                        deletelast(count);
                        break;
case 5:
                        printf("Enter the data:\n");
                        scanf("%d",&pos);
                        q=getnth(pos);
                        break;
case 6:
                        count=length();
                        deletelist(count);
                        break;
case 7:
                        display();
                        break;
case 8:
                        printf("program terminated by user\n");
                        exit(0);
                        break;
default:
                        printf("please enter the valid option\n");
                        break;


                }
                printf("do you want to continue:Press Y to continue, Press N to exit\n");
                scanf(" %c",&options);
                if(options=='y')
                 continue;
                else
                {
                        printf("program terminated by user\n");
                        exit(0);
                }
        }

}

No comments:

Post a Comment