Implementation of singly linklist

A link list is a data structure that maintain the list of items that are logically connected with links (pointer). Link list is of four types:-

1) Singly link list:-
                              In singly link list every nodes contains two information inside it first data field and second field is pointer field .
Data field:-   It stores the actual value of the data items.
Pointer field:- It stores the address of next node or data item  

 Code:-

#include<stdio.h>
#include<stdlib.h>

// define structure of a node

struct node
{
int data;
struct node* link;
};

struct node *start=NULL;

// create a node

struct node * create_node()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
return temp;
}

// functio for insert element at first position

void insert_first()
{
struct node *temp;
temp=create_node();
printf(“Enter a elementn”);
scanf(“%d”,&temp->data);
temp->link=start;
start=temp;
}
// function for insert element at the end of the list
void insert_end()
{
struct node *temp,*t;
temp=create_node();
printf(“Enter a elementn”);
scanf(“%d”,&temp->data);
if(start==NULL)
{
temp->link=start;
start=temp;
}
else
{
t=start;
temp->link=NULL;
while(t->link!=NULL)
t=t->link;
t->link=temp;
}
}

// function for delete element from the starting

void delte_first()
{
struct node * temp;
if(start==NULL)
printf(“List is emptyn”);
else
{
temp=start;
start=temp->link;
free(temp);
}
}

// function for delete element from the end of the list

void delete_end()
{
struct node * temp,*t;
if(start==NULL)
{
printf(“List is emptyn”);
}
else
{
t=start;
if(t->link==NULL)
{
start=NULL;
free(t);
}
else
{
while(t->link->link!=NULL)
{
t=t->link;
}
temp=t->link;
t->link=NULL;
free(temp);
}
}
}

// function for display the element of List

void display()
{
struct node * temp;
if(start==NULL)
printf(“List is emptyn”);
else
{
temp=start;
while(temp!=NULL)
{
printf(“%d “,temp->data);
temp=temp->link;
}
}
}
int main()
{
int ch;
printf(“1. insert element at startingn”);
printf(“2. insert element at the end of the listn”);
printf(“3. delete element from the startingn”);
printf(“4. delete element from the end of the listn”);
printf(“5. display element of the listn”);
printf(“6. exitn”);
while(1)
{
printf(“Enter your choicen”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: insert_first();
break;
case 2: insert_end();
break;
case 3: delte_first();
break;
case 4: delete_end();
break;
case 5: display();
break;
case 6: exit(0);
default: printf(“Wrong keyn”);
}
}
return 0;
}


Recommended Post:

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-

Leave a Reply

Your email address will not be published.