0% found this document useful (0 votes)
35 views

Singly Linked List: Create Structure

This document describes how to implement a singly linked list in C including functions to: 1) Create a linked list by adding nodes to the front or another position. 2) Add, delete, display, and search for data in the linked list. 3) Functions are provided to add nodes at the beginning or another position, delete nodes, display all elements, and search for a given element in the list.

Uploaded by

you_k_gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Singly Linked List: Create Structure

This document describes how to implement a singly linked list in C including functions to: 1) Create a linked list by adding nodes to the front or another position. 2) Add, delete, display, and search for data in the linked list. 3) Functions are provided to add nodes at the beginning or another position, delete nodes, display all elements, and search for a given element in the list.

Uploaded by

you_k_gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Singly linked list

CREATE STRUCTURE
struct node

Declare info;

struct node *link;

}*start; //start is a variable of node

CREAT LINKED LIST


creat_list(int data)

1. Declare *q,*temp as node variable.


2. tmp= (struct node*)malloc(sizeof (struct node));
3. temp->info=data;
4. tmp->link=NULL;
5. if(start==NULL)
start=tmp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;
}
6. exit

ADD DATA at BEGINNING

addatbeg(int data)

1. Declare *tmp as node variable.


2. tmp=(struct node *)malloc(sizeof (struct node));
3. tmp->info=data;
4. tmp->link=start;
5. start=tmp;
6. exit

ADD DATA at given POSITION

addafter(int data,Declare pos)

1. Declare *tmp,*q as node variable.


2. q=start;
3. i=0
4. while(i<pos-1)
{
q=q->link;
if(q==NULL)
{
Write:"There are less elements then given position.
exit;
}
i=i+1
}
4. tmp=(struct node*)malloc(sizeof(struct node));
5. tmp->link=q->link;
6. tmp->info=data;
7. q->link=tmp;
8. Exit

DELETE DATA

del(int data)

1. Declare *tmp,*q as node variable.


2. if(start->info==data)
{
tmp=start;
start=start->link;
free(tmp);
exit;
}
3. q=start;
4. while(q->link->link!=NULL)
{
if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
exit;
}
q=q->link; }
5. if(q->link->info==data)
{
tmp=q->link;
free(tmp);
q->link=NULL;
exit;
}
6. Write: “Element not found”
7. Exit

DISPLAY ELEMENTS

display()

1. Declare *q as node variable.


2. if(start==NULL)
{
Write:"List is Empty :".
exit;
}
3. q=start;
4. Write:" List is :".
5. while(q!=NULL)
{
Write : q->info
q=q->link;
}
7. Exit

SEARCHING THE DATA

search(int data)

1. Declare*ptr as node variable.


2. *ptr=start;
3. Declare pos=1;
4. while(ptr!=NULL)
{
if(ptr->info==data)
{
Write:"Given data is not found at the position.
exit; }

ptr=ptr->link;

pos++;

}
5. if(ptr==NULL)
Write: Item is not found in list .

You might also like