Implementation Of Link List As Data Structure:
This is a simple C++ code which implements the most important Data Structure Link List .The code contains three files header file,cpp file and the main file which contains main function.You can download all the files by clicking the download button below or you can also copy paste the code.This is header file.
#ifndef FILE_H_INCLUDED
#define FILE_H_INCLUDED
class node
{//start of class
public:
int data;
node *next;
node();//constructor
};//end of class
class linklist
{//start of class
private:
node *root;
public:
linklist();//constructor
~linklist();//destructor
void insert_node(int );
void print();
bool search_node(int );
void delete_node(int );
void destroy_list();
};//end of class
#endif // FILE_H_INCLUDED
#define FILE_H_INCLUDED
class node
{//start of class
public:
int data;
node *next;
node();//constructor
};//end of class
class linklist
{//start of class
private:
node *root;
public:
linklist();//constructor
~linklist();//destructor
void insert_node(int );
void print();
bool search_node(int );
void delete_node(int );
void destroy_list();
};//end of class
#endif // FILE_H_INCLUDED
This is cpp file.
#include<iostream>
#include<windows.h>
#include"file.h"
using namespace std;
//constructor function
node::node()
{
next=NULL;
}
//constructor function
linklist::linklist()
{
root=NULL;
}
void linklist::insert_node(int item)
{//start of function
node *new_node=new node;
new_node->data=item;
node *current=root;
node *previous;
while (current!=NULL && current->data<item)
{//start of while
previous=current;
current=current->next;
}//end of while
new_node->next=current;
if (current==root)
root=new_node;
else
previous->next=new_node;
}//end of function
void linklist::print()
{
node *iterator=root;
while (iterator!=NULL)
{//start of while
cout<<iterator->data<<"--";
iterator=iterator->next;
}//end of while
cout<<"NULL";
}
bool linklist::search_node(int key)
{
node *searcher=root;
while (searcher!=NULL)
{
if (searcher->data==key)
return true;
else
searcher=searcher->next;
}
return false;
}
void linklist::delete_node(int key)
{
if (root->data==key)
{
node *ptr=root;
root=ptr->next;
delete ptr;
}
else
{
node *previous;
node *current=root;
node *temp;
if (current->next==NULL)
current=NULL;
while (current!=NULL)
{
if (current->data==key)
{
temp=current;
previous->next=current->next;
delete temp;
break;
}
else
{
previous=current;
current=current->next;
}
}
}
}
void linklist::destroy_list()
{
node *temp;
while(root!=NULL)
{
temp=root;
root=root->next;
delete temp;
}
}
//destructor funtion
linklist::~linklist()
{
destroy_list();
}
This is main file.
//implementation of link list
//made by Syed Azeem Abbas
#include <iostream>
#include<windows.h>
#include"file.h"
using namespace std;
void load_menu(int );
int CheckKeyPressed(int );
int main()
{
node obj1;
linklist obj2;
int counter=0;
bool condition=true;
load_menu(counter);
while (condition==true)
{//start while
int a=CheckKeyPressed(0);
if (a==40)
{//start if
if (counter<4)
{//start if
counter++;
system("cls");
load_menu(counter);
}//end if
}//end if
else if (a==38)
{//start else if
if (counter>0)
{//start if
counter--;
system("cls");
load_menu(counter);
}//end if
}//end else if
else if(a==13)
{//start else if
switch (counter)
{//start of switch
case 0:
{
system("cls");
int item;
cout<<"Enter the data you want to insert:";
cin>>item;
obj2.insert_node(item);
cout<<"Data inserted:";
cout<<endl<<endl;
system("pause");
load_menu(counter);
break;
}
case 1:
{
system("cls");
obj2.print();
cout<<endl<<endl;
system("pause");
load_menu(counter);
break;
}
case 2:
{
system("cls");
int key;
cout<<"Enter the key you want to find:";
cin>>key;
if (obj2.search_node(key)==true) cout<<"Key is found:";
else cout<<"Key is not found";
cout<<endl<<endl;
system("pause");
load_menu(counter);
break;
}
case 3:
{
system("cls");
int key;
cout<<"Enter the key you want to delete:";
cin>>key;
if (obj2.search_node(key)==true)
{
obj2.delete_node(key);
cout<<"Key deleted:";
cout<<endl<<endl;
}
else
cout<<"Key is not found";
system("pause");
load_menu(counter);
break;
}
case 4:
{
condition=false;
break;
}
}//end of switch
}//end else if
}//end while
system("pause");
return 0;
}
void load_menu(int counter)
{//start of function
system("cls");
cout<<" SELECT THE OPTION FROM MENU BY PRESSING ENTER"<<endl<<endl<<endl;
if (counter==0)
{
cout<<" ->ADD NODE"<<endl;
cout<<" PRINT LINK LIST"<<endl;
cout<<" SEARCH NODE"<<endl;
cout<<" DELETE NODE"<<endl;
cout<<" EXIT"<<endl;
}
else if (counter==1)
{
cout<<" ADD NODE"<<endl;
cout<<" ->PRINT LINK LIST"<<endl;
cout<<" SEARCH NODE"<<endl;
cout<<" DELETE NODE"<<endl;
cout<<" EXIT"<<endl;
}
else if (counter==2)
{
cout<<" ADD NODE"<<endl;
cout<<" PRINT LINK LIST"<<endl;
cout<<" ->SEARCH NODE"<<endl;
cout<<" DELETE NODE"<<endl;
cout<<" EXIT"<<endl;
}
else if (counter==3)
{
cout<<" ADD NODE"<<endl;
cout<<" PRINT LINK LIST"<<endl;
cout<<" SEARCH NODE"<<endl;
cout<<" ->DELETE NODE"<<endl;
cout<<" EXIT"<<endl;
}
else if (counter==4)
{
cout<<" ADD NODE"<<endl;
cout<<" PRINT LINK LIST"<<endl;
cout<<" SEARCH NODE"<<endl;
cout<<" DELETE NODE"<<endl;
cout<<" ->EXIT"<<endl;
}
}//end of function
int CheckKeyPressed(int waitTime)
{
HANDLE h= GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD r;
DWORD w = 1;
DWORD eventss;
DWORD waitResult=0;
int keypressed = false;
int toReturn = 0;
waitResult = WaitForSingleObject(h,waitTime);
if (waitResult == WAIT_OBJECT_0)
{
/*FlushConsoleInputBuffer(h);..commented out as this takes to asynchronous mode on some systems*/
keypressed = ReadConsoleInput(h,&r,1,&eventss);
if (keypressed && r.EventType==KEY_EVENT && r.Event.KeyEvent.bKeyDown)
toReturn = r.Event.KeyEvent.wVirtualKeyCode;
/*this should make sure that checkKeyPressed is not called twice for arrow keys*/
if (toReturn == 224)
toReturn = CheckKeyPressed(waitTime);
FlushConsoleInputBuffer(h);
}
return toReturn;
}
//made by Syed Azeem Abbas
#include <iostream>
#include<windows.h>
#include"file.h"
using namespace std;
void load_menu(int );
int CheckKeyPressed(int );
int main()
{
node obj1;
linklist obj2;
int counter=0;
bool condition=true;
load_menu(counter);
while (condition==true)
{//start while
int a=CheckKeyPressed(0);
if (a==40)
{//start if
if (counter<4)
{//start if
counter++;
system("cls");
load_menu(counter);
}//end if
}//end if
else if (a==38)
{//start else if
if (counter>0)
{//start if
counter--;
system("cls");
load_menu(counter);
}//end if
}//end else if
else if(a==13)
{//start else if
switch (counter)
{//start of switch
case 0:
{
system("cls");
int item;
cout<<"Enter the data you want to insert:";
cin>>item;
obj2.insert_node(item);
cout<<"Data inserted:";
cout<<endl<<endl;
system("pause");
load_menu(counter);
break;
}
case 1:
{
system("cls");
obj2.print();
cout<<endl<<endl;
system("pause");
load_menu(counter);
break;
}
case 2:
{
system("cls");
int key;
cout<<"Enter the key you want to find:";
cin>>key;
if (obj2.search_node(key)==true) cout<<"Key is found:";
else cout<<"Key is not found";
cout<<endl<<endl;
system("pause");
load_menu(counter);
break;
}
case 3:
{
system("cls");
int key;
cout<<"Enter the key you want to delete:";
cin>>key;
if (obj2.search_node(key)==true)
{
obj2.delete_node(key);
cout<<"Key deleted:";
cout<<endl<<endl;
}
else
cout<<"Key is not found";
system("pause");
load_menu(counter);
break;
}
case 4:
{
condition=false;
break;
}
}//end of switch
}//end else if
}//end while
system("pause");
return 0;
}
void load_menu(int counter)
{//start of function
system("cls");
cout<<" SELECT THE OPTION FROM MENU BY PRESSING ENTER"<<endl<<endl<<endl;
if (counter==0)
{
cout<<" ->ADD NODE"<<endl;
cout<<" PRINT LINK LIST"<<endl;
cout<<" SEARCH NODE"<<endl;
cout<<" DELETE NODE"<<endl;
cout<<" EXIT"<<endl;
}
else if (counter==1)
{
cout<<" ADD NODE"<<endl;
cout<<" ->PRINT LINK LIST"<<endl;
cout<<" SEARCH NODE"<<endl;
cout<<" DELETE NODE"<<endl;
cout<<" EXIT"<<endl;
}
else if (counter==2)
{
cout<<" ADD NODE"<<endl;
cout<<" PRINT LINK LIST"<<endl;
cout<<" ->SEARCH NODE"<<endl;
cout<<" DELETE NODE"<<endl;
cout<<" EXIT"<<endl;
}
else if (counter==3)
{
cout<<" ADD NODE"<<endl;
cout<<" PRINT LINK LIST"<<endl;
cout<<" SEARCH NODE"<<endl;
cout<<" ->DELETE NODE"<<endl;
cout<<" EXIT"<<endl;
}
else if (counter==4)
{
cout<<" ADD NODE"<<endl;
cout<<" PRINT LINK LIST"<<endl;
cout<<" SEARCH NODE"<<endl;
cout<<" DELETE NODE"<<endl;
cout<<" ->EXIT"<<endl;
}
}//end of function
int CheckKeyPressed(int waitTime)
{
HANDLE h= GetStdHandle(STD_INPUT_HANDLE);
INPUT_RECORD r;
DWORD w = 1;
DWORD eventss;
DWORD waitResult=0;
int keypressed = false;
int toReturn = 0;
waitResult = WaitForSingleObject(h,waitTime);
if (waitResult == WAIT_OBJECT_0)
{
/*FlushConsoleInputBuffer(h);..commented out as this takes to asynchronous mode on some systems*/
keypressed = ReadConsoleInput(h,&r,1,&eventss);
if (keypressed && r.EventType==KEY_EVENT && r.Event.KeyEvent.bKeyDown)
toReturn = r.Event.KeyEvent.wVirtualKeyCode;
/*this should make sure that checkKeyPressed is not called twice for arrow keys*/
if (toReturn == 224)
toReturn = CheckKeyPressed(waitTime);
FlushConsoleInputBuffer(h);
}
return toReturn;
}
DOWNLOAD THE WHOLE PROJECT:
i run this code it worked perfect very easy and professional code
ReplyDelete