100% found this document useful (1 vote)
124 views4 pages

Header File: Link List

This document contains C++ code that implements a linked list data structure using node pointers. It includes a Node class to define the nodes of the list and a List class that implements functions to add nodes, delete nodes, display the list, reset pointers, and delete the entire list. The code provides function definitions for each list operation and includes a driver program that tests creating a list of 9 elements, deleting a member, and deleting the whole list.

Uploaded by

krunal
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 PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
124 views4 pages

Header File: Link List

This document contains C++ code that implements a linked list data structure using node pointers. It includes a Node class to define the nodes of the list and a List class that implements functions to add nodes, delete nodes, display the list, reset pointers, and delete the entire list. The code provides function definitions for each list operation and includes a driver program that tests creating a list of 9 elements, deleting a member, and deleting the whole list.

Uploaded by

krunal
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 PDF, TXT or read online on Scribd
You are on page 1/ 4

Link List

Header file
/*
===========================
Author: Krunal Soni
Purpose: To explain the concept node pointer
===========================
All rights reserved by KSONI
*/
#include <iostream>
#include <stdlib.h>
using namespace std;
/*
=======================
Master Class
=======================
*/
class llist{
public:
llist();
bool addmember();
bool deletemember();
bool displaylist();
bool resetptr();
bool deletelist();

private:
/*
=======================
Slave Class
=======================
*/
class node{
public:
int member;
node *next;
node(){
member=0;
next=NULL;
}
~node(){
};

};
/*
=======================
end of Slave Class
=======================
*/
node *nodeptr,*writeptr,*readptr,*prevptr;
};
/*
=======================
end of Master Class
=======================
*/
Function description file:

/*
===========================
Author: Krunal Soni
Purpose: To explain the concept node pointer
===========================
All rights reserved by KSONI
*/
#include "llist.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

/*
=======================
Function definations
=======================
*/
llist::llist(){
nodeptr=NULL;
}
/*
=======================
function is to reset pointers to beginging of the list
=======================
*/
bool llist::resetptr(){
writeptr=nodeptr;
readptr=nodeptr;
prevptr=nodeptr;
return true;
}
/*
=======================
function is to add node in the list
=======================
*/
bool llist::addmember(){
cout<<"please enter the number"<<endl;
if(!nodeptr){
nodeptr=new node();
resetptr();
cin>>writeptr->member;
}
else{
writeptr->next=new node();
writeptr=writeptr->next;
cin>>writeptr->member;
}
return true;
}
/*
=======================
function is to display the list
=======================
*/
bool llist::displaylist(){
resetptr();
cout<<"Start-->";
if(readptr)
while(readptr){
cout<<readptr->member<<"-->";
readptr=readptr->next;
}
cout<<"End"<<endl;
return true;
}
/*
=======================
function is to delete specific node in the list
=======================
*/
bool llist::deletemember(){
cout<<"enter the number which you wanna delete"<<endl;
int scanval;
cin>>scanval;
cout<<"[List before delete]"<<endl;
displaylist();
resetptr();
/*
=======================
to delete first node in the list
=======================
*/
if(prevptr->member==scanval){
readptr=readptr->next;
delete prevptr;
nodeptr=prevptr=readptr;
}
/*
=======================
to delete anyother node in the list
=======================
*/
else{
while(readptr->next){
prevptr=readptr;
readptr=readptr->next;
if(readptr->member==scanval){
prevptr->next=readptr->next;
delete readptr;
readptr=prevptr;
}

}
}
cout<<"[List after delete]"<<endl;
displaylist();
return true;
}
/*
=======================
function is to delete complete list
=======================
*/
bool llist::deletelist(){
resetptr();
while(readptr){
prevptr=readptr;
readptr=readptr->next;
delete prevptr;
}
nodeptr=NULL;
return true;
}

Application file:
/*
===========================
Author: Krunal Soni
Purpose: To explain the concept node pointer
===========================
All rights reserved by KSONI
*/
#include "llist.h"
#include <iostream>
#include <stdlib.h>
using namespace std;

/*
=======================
Driver function
=======================
*/

int main(int argc, char* argv[]){


llist l;
// ===========================
// creating list of 9 elements
for(int i=0;i<9;i++){
l.addmember();
}
// ==========================

// displaying list after making list


l.displaylist();
// checking delete member option
l.deletemember();
// deleting whole list
l.deletelist();
// to make sure no memory leak
l.displaylist();
return 0;
}

You might also like