0
894views
Singly linked list.
  • It is a data structure which is collection of elements in a chain function.

  • In a linked list, there will be a node which will consist of a data and a pointer which will be pointing to the next node.

enter image description here

Program:

include < stdio.h>

include < conio.h>

inlcude < alloc.h>

struct node

$\alpha$

int data;

struct node * nect ;

3;

struct node * first , * last , * pq * q , new x c ;

void insert ( )

<

int pos, i,

new xc = mallos (size of (struct node));

print f (" /n enter element to be inserted and its position");

scan f (:%d %d " , & new xc $\rightarrow$ data, & yes "),

new xc $\rightarrow$ next = NULL;

if (pos) = = 1)

<

new xc $\rightarrow$ next $\rightarrow$ first;

first = new xc;

3

p = first;

for (i = 1 , i < pos - 1 ; iff)

<

if (p ! = NULL)

p = p $\rightarrow$ next;

else

<

print f ("in over flow");

3

3

new xc $\rightarrow$ next = p $\rightarrow$ next ;

p $\rightarrow$ next = newxc;

3

void delete ( )

<

if (first = = NULL)

print f ("/n (inked list empty");

else

p = first;

while (p $\rightarrow$ next ! = NULL)

<

p = p $\rightarrow$ next;

3

q = p $\rightarrow$ next;

free (q);

p $\rightarrow$ next = NULL;

3

void display ( )

<

if (first = = NULL)

print f ("\n (inked list empty");

else

<

p = first;

while (p $\rightarrow$ next ! = NULL)

<

print f (" \t % d " , p $\rightarrow$ data);

p = p $\rightarrow$ next;

3

3

void main ( )

<

int ch;

first = NULL;

last = NULL;

print f ("\n menu \n I - insert a node at the specified location \n 2 - Delete a node from end \n

3 - Display the list in 4-exit");

print f (") \n enter choice");

scan f ("% d" , & ch);

switch (ch)

<

Case 1:

insert ();

break ;

Case 2:

delete ( );

break ;

Case 3 :

display ( );

break ;

Case 4 :

ch = 0;

break;

default:

print f ("\n invalid choice");

3

3 // end of do

while (ch ! = 4);

3

Please log in to add an answer.