Header Ads Widget

Self referential structure (Notation of linklist ) / Singly link list in C

 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  

To create a singly link list the structure of node can be define as follows :-+
/* Implementation of singly link list  */                                                                         

#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;
}

// function for insert element at first position

void insert_first()
{
struct node *temp;
temp=create_node();
printf("Enter a element\n");
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 element\n");
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 empty\n");
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 empty\n");
}
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 empty\n");
else
{
temp=start;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->link;
}
}
}
int main()
{
int ch;
printf("1. insert element at starting\n");
printf("2. insert element at the end of the list\n");
printf("3. delete element from the starting\n");
printf("4. delete element from the end of the list\n");
printf("5. display element of the list\n");
printf("6. exit\n");
while(1)
{
printf("Enter your choice\n");
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 key\n");
}
}
return 0;
}




Recommended Post:

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-


                                                                                                                                                                     

Post a Comment

0 Comments