0% found this document useful (0 votes)
4 views2 pages

C Struct Node Implementation Guide

This C program implements a simple linked list data structure to store integer values. It includes functions to add nodes, delete nodes, and display the list. The main function provides a menu to allow the user to choose these options and interactively build and modify the list. Pointers are used to traverse and link the nodes as new elements are added or removed from the list.

Uploaded by

Vindesh Kumar
Copyright
© All Rights Reserved
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)
4 views2 pages

C Struct Node Implementation Guide

This C program implements a simple linked list data structure to store integer values. It includes functions to add nodes, delete nodes, and display the list. The main function provides a menu to allow the user to choose these options and interactively build and modify the list. Pointers are used to traverse and link the nodes as new elements are added or removed from the list.

Uploaded by

Vindesh Kumar
Copyright
© All Rights Reserved
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

#include <stdio.

h>
#include<stdlib.h>
struct node
{
int val;
struct node *left;
struct node *right;
};

struct node* getnode( )


{
return ( struct node *)malloc( sizeof( struct node ));
};
struct node* top; int c=0;
struct node* add( struct node *);
struct node* del( struct node *);
void display( struct node *);

int main()
{
struct node *F;
int ch;
F = NULL; top=F;
while( 1 )
{
printf("1. Add new element\n");
printf("2. Delete element\n");
printf("3. Display elements \n");
printf("4. Exit \n");
scanf("%d", &ch );
if( ch == 1 )
F = add ( F );
else if( ch == 2 )
F=del(F);
else if(ch==3)
display( F );
else if( ch == 4 )
return;
}
}

struct node* add( struct node *FIRST)


{
struct node *T;
if( FIRST == NULL )
{
FIRST = getnode();
T =top=FIRST;
T->left=NULL;
}
else
{ T=getnode();
top->right=T; T->left=top;
top=T;
}
printf("Enter a val\n"); c++;
scanf("%d",&T->val );
T->right= NULL;
return FIRST;
}
struct node* del( struct node *FIRST)
{
struct node *T;
if(( FIRST== NULL)&&(c<1))
{
printf("Underflow\n"); return FIRST;
}
T=top;
printf("The deleted value is : %d\n",T->val);
if(c!=1)
{top=T->left;
T->left= NULL; top->right=NULL;}
else if(c==1)
{FIRST=NULL;}
c--;
return FIRST;
}

void display( struct node *T)


{ if(T==NULL)
{
printf("Empty\n"); return;
}
while( T != NULL )
{
printf("%d ",T->val);
T = T->right;
}
printf("\n");
}

You might also like