C Program to implement singly linked list using Pointer

A linked-list is a sequence of data structures which are connected together via links. a linked list is sequence of node and each node has two field. one is value and another is address.
Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list the second most used data structure after array. Following are important terms to understand the concepts of Linked List.

Link − Each Link of a linked list can store a data called an element.
Next − Each Link of a linked list contain a link to next link called Next.
LinkedList − A LinkedList contains the connection link to the first Link called First.

C Program to implement singly linked list using Pointer
As per above define. we considered the following point in linked list.

1. LinkedList contains an link element called first.
2. Each Link carries a data field(s) and a Link Field called next.
3. Each Link is linked with its next link using its next link.
4. Last Link carries a Link as null to mark the end of the list

In this program we will learn how to insert the element in singly linked list. we insert element at the beginning, at the end and in middle.

ALGORITHM:-

Step: 1.  Create a struct and define struct type variables
Step: 2.  Allocate dynamic memory and store the value
Step: 3.  Enter some other value and press -999 (terminate)
Step: 4.  Display the value with address
Step: 5.  Insert element at the beginning of linked list
Step: 6.  Display the value with address
Step: 7.  Insert element at the end of list and middle at the list
Step: 8.  Display the record
Step: 9.  Terminate


PROGRAM CODE:-

#include "stdio.h"
#include "conio.h"
#include "alloc.h"
void display();
struct node
{
        int data;
        struct node *add;
};
int b;
struct node *temp,*first=0,*last=0,*head,*newnode,*pnode;
void create()
{
        printf("Enter list element: ");
        scanf("%d",&b);
        while(b!=-999)
        {
                temp=(struct node*)malloc(sizeof(struct node));
                temp->data=b;
                temp->add=NULL;
                if(first==0)
                {
                        last=first=temp;
                }
                else
                {
                        last->add=temp;
                        last=temp;
                }
                printf("Enter list element: ");
                scanf("%d",&b);
        }
}
void insertb()
{
        printf("enter an element to insert at beginingn");
        scanf("%d",&b);
        temp=(struct node*)malloc(sizeof(struct node));
        temp->data=b;
        temp->add=first;
        first=temp;
}
void insertl()
{
        printf("enter an element to insert at lastn");
        scanf("%d",&b);
        temp=(struct node*)malloc(sizeof(struct node));
        temp->data=b;
        temp->add=NULL;
        last->add=temp;
        last=temp;
}
void middle()
{
        int search;
        printf("enter the precedence element: ");
        scanf("%d",&search);
        head=first;
        while(head!=NULL)
        {
                if(head->add->data==search)
                {
                        newnode=head;
                        break;
                }
                head=head->add;
        }
        if(head==NULL)
        {
                printf("key not found");
        }
        else
        {
                printf("enter the element at middle");
                scanf("%d",&b);
                temp=(struct node*)malloc(sizeof(struct node));
                temp->data=b;
                temp->add=newnode->add;
                newnode->add=temp;
                display();
        }
}
void del()
{
        int s;
        printf("enter the position to be is deleted");
        scanf("%d",&s);
        head=first;
        while(head!=NULL)
        {
                if(head->add->data==s)
                {
                        head->add =head->add->add;
  }
                else
                {
                        printf("data not found");
                        break;
                }
        }
}
void display()
{
        head=first;
        printf("data t add n");
        while(head!=NULL)
        {
                printf("%dt",head->data);
                printf("%dn",head->add);
                head=head->add;
        }
}
void main()
{
        clrscr();
        create();
        display();
        insertb();
        display();
        insertl();
        display();
        middle();
        del();
        display();
        getch();

}


OUTPUT:-

Enter Element in list: 3
4
5
6
-999

Data    Address
3        2088
4        2096
5        2104
6        0

Enter an element to insert at beginning: 123
Data    Address
123      2080
3        2088
4        2096
5        2104
6        0
enter an element to insert at last:  234
Data    Address
123      2080
3        2088
4        2096
5        2104
6        2120
234      0
Enter the precedence element: 4
Data    Address
123      2080
3        2128
10       2088
4        2096
5        2104
6        2120
234      0
C Program to implement singly linked list
how to insert element in singly linked list at the beginning
how to insert element in singly linked list at the end
how to insert element in singly linked list at the middle
how to display the element of an singly linked list

C Program to implement singly linked list using Pointer