/*Linked List
Program to Create Linked List (node implementation) and do common operations in it.
Program to Create Linked List (node implementation) and do common operations in it.
Creation, Traversal, Deletion of nodes
****************************************************************************************************************************/
//preprocessor directives
#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<iostream.h>
//prototype declaration
//return type is void i.e. they do return any values
void create();
void travel();
void nodeDelete();
//variable declaration
int nodeNumber, delInfo=0;
struct node //Structure of node defined
{
int data;
node *ptr;
} *start, *temp, *trvl;
int main( )
{
int choice;
do{
clrscr();
//Menu
cout<<"\n\tWelcome to Node Menu...";
cout<<"\nPress 1 for node creation process..";
cout<<"\nPress 2 for Traversal...";
cout<<"\nPress 3 to delete a node...";
cout<<"\nPress 4 to view value of last deleted node...";
cout<<"\nPress 5 to exit\n\t";
cout<<"\nEnter your choice...";
cin>>choice;
switch(choice)
{
case 1: create();
break;
case 2: travel();
break;
case 3: nodeDelete();
break;
case 4: cout<<"\nLast data part: "<<delInfo;
break;
case 5:
cout<<"\nPreparing to exit...";
cout<<"\nPress Enter...";
getch();
exit(0); //exit() is defined in process.h, 0 is usually way for exiting
break;
default: cout<<"\nWrong choice!! Remember (1-5)";
break;
}
}while(choice != 4);
getch();
return 0;
}
//First two are required for list creation
void create()
{
start = new node; //First node
/* actually right after creation of every node, you should/can check whether it is actually created or not. i.e. did the system had sufficient memory to allocate resources
It can be done using a null check; NULL is a state of a pointer if it doesn't points to some location.
NULL is defined in a lot of header files including our own "stdio.h" which as can be seen is included earlier. Next I'll show how to use NULL.
if (start == NULL)
{
cout<<"\nInsufficient memory";
}
and maybe you can add exit(0) in the condition to quit the program. Because if the system fails to allocate memory then it's of no use*/
/* actually right after creation of every node, you should/can check whether it is actually created or not. i.e. did the system had sufficient memory to allocate resources
It can be done using a null check; NULL is a state of a pointer if it doesn't points to some location.
NULL is defined in a lot of header files including our own "stdio.h" which as can be seen is included earlier. Next I'll show how to use NULL.
if (start == NULL)
{
cout<<"\nInsufficient memory";
}
and maybe you can add exit(0) in the condition to quit the program. Because if the system fails to allocate memory then it's of no use*/
cout<<"\nNew node created...!";
cout<<"\nEnter data: ";
cin>>start->data;
temp = new node; //Second node
cout<<"\nSecond node....";
cout<<"\nEnter data: ";
cin>>temp->data;
start->ptr = temp;
cout<<"\nHow many more nodes you want...?";
cin>>nodeNumber;
int i=0;
for(i=0; i<(nodeNumber); ++i) //Further nodes created
{
trvl = new node;
cout<<"\nEnter data in node number "<<i+3<<": ";
cin>>trvl->data;
temp->ptr = trvl;
temp = temp->ptr;
}
cout<<"\nNodes created...";
}
void travel()
{
cout<<"\nTraversing"; //Traversal
cout<<"\n Node number \t Data";
trvl = start;
for(int i=0; i<(nodeNumber+2); ++i)
{
cout<<endl<<"\t"<<i+1<<"\t\t"<<trvl->data;
trvl = trvl->ptr;
}
}
void nodeDelete()
{
int del;
// clrscr();
travel();
cout<<"\nWhich node you want to delete...?";
cin>>del;
if( del > nodeNumber+2)
cout<<"\nSuch node doesn't exists..., try again";
else
{
temp = trvl = start;
for(int i=0; i<del ; ++i)
trvl = trvl-> ptr;
for( int j=0; j< (del-1) ; ++j)
temp = temp-> ptr;
temp->ptr = trvl->ptr;
delInfo= trvl->data;
nodeNumber--;
nodeNumber--;
}
cout<<"\nNode deleted...";
cout<<"\nYou can view data of last deleted node";
}