Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » Data File StructureRSS Feeds

Singly Linked list

Posted By: Francesca Hughes     Category: C Programming     Views: 8949

Program of Singly Linked list.

Code for Singly Linked list in C Programming

#include<stdio.h>
#include<conio.h>
struct node
{
    int no;

    struct node *next;
};
    struct node *head;
    struct node  *p;
    struct node *temp;
    int ch1;

void main()
{

    void ins();
    void del();
    void disp();

    int ctr,num;
    char ch;

    clrscr();


    head = (struct node *)malloc(sizeof(struct node));

    printf("\nEnter the no::");
    flushall();
    scanf("%d",&head->no);

    printf("\nDo u want to create another node::");
    flushall();
    scanf("%c",&ch);

    p=head;
    while(ch=='y' || ch=='Y')
    {
        p->next=(struct node *)malloc(sizeof(struct node));
        p=p->next;

        printf("\nEnter the no::");
        flushall();
        scanf("%d",&p->no);

        printf("\nDo u want to create another node::");
        flushall();
        scanf("%c",&ch);
    }
    p->next=NULL;

    clrscr();
    printf("\n1)Insert \n2)Delete \n3)Traverse");
    flushall();
    scanf("%d",&num);

    if(num==1)
    {
        ins();
    }
    if(num==2)
    {
        del();
    }
    if(num==3)
    {
        disp();
    }
    getch();
}

void disp()
{
        temp=head;
        while(temp!=NULL)
        {
            printf("\n%d",temp->no);
            temp=temp->next;
        }

}
void ins()
{
    int af;
    printf("\n1)Begining\n2)Middle\n3)End");
    flushall();
    scanf("%d",&ch1);
    temp=(struct node*)malloc(sizeof(struct node));
    printf("\nEnter the no::");
    flushall();
    scanf("%d",&temp->no);

    if(ch1==1)
    {

        temp->next=head;
        head=temp;
        disp();
    }
    if(ch1==2)
    {
        p=head;
        printf("\nAfter which node u want to insert::");
        flushall();
        scanf("%d",&af);

        while(p->no!=af)
        {
            p=p->next;
        }
        temp->next=p->next;
        p->next=temp;
        disp();
    }
    if(ch1==3)
    {
        p=head;
        while(p->next!=NULL)
        {
            p=p->next;
        }
        p->next=temp;
        temp->next=NULL;
        disp();
    }
}
void del()
{
    int num,af;

    printf("\n1)Begining\n2)Middle\n3)End");
    flushall();
    scanf("%d",&num);

    if(num==1)
    {
        p=head;
        head=p->next;
        free(p);
        disp();

    }
    if(num==2)
    {
        p=head;
        printf("\nAfter which node u want to del::");
        flushall();
        scanf("%d",&af);

        while(p->no!=af)
        {
            p=p->next;
        }
        temp=p->next;
        p->next=temp->next;
        free(temp);
        disp();
    }
    if(num==3)
    {
        p=head;

        while(p->next->next!=NULL)
        {
            p=p->next;
        }
        temp=p->next;
        p->next=NULL;
        free(temp);
        disp();
    }
}
  
Share: 


Didn't find what you were looking for? Find more on Singly Linked list Or get search suggestion and latest updates.

Francesca Hughes
Francesca Hughes author of Singly Linked list is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!