LinkList
// linklist.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
//Make a class named node whose instances will be used in LinkList //class
class node
{
friend class linklist;
private:
int val;
node *next;
public:
//Sets value of variable val to the argument passed.
void setvalue(int x)
{
val=x;
}
//Returns value of variable val
int getvalue()
{
return val;
}
//Sets the node type object to the argument passed
void setnext(node *a)
{
next=a;
}
//Returns the address of next because "next" is a node type //pointer.
node * getnext()
{
return next;
}
};
class linklist
{
private:
int size; //Total nodes in linklist
node* head; //head has address of first node of linklist.
node *current;
public:
linklist()
{
current=NULL;
size=0;
}
//This function returns the address of node in linklist whose //location is passed to the function.
node *getnode(int loc)
{
node *x;
if(loc<=0 || loc>size)
x=NULL;
else
{
//Assign x address of first node
x=head;
//Start from first location to the location passed
for(int i=1;i<loc;i++)
x=x->getnext(); //Assign x address of next node.
}
return x; //When loop completes return the value of x.
}
//Insertnode function accepts two arguments first is value of //node second is location of node where to insert the node.
void insertnode(int val,int loc)
{
if(loc<=0 || loc>size+1 )//Invalid Location
cout<<"invalid index"<<endl;
else
{
node *temp=new node();//Create a new object of type node.
temp->setvalue(val); //Set the value of node.
if(size==0) //If linklist is empty.
{
//Assign next to NULL it is first node.
temp->setnext(NULL);
//Assign head address of first node.
head=temp;
}
//If node is being inserted on first location
else if(loc==1)
{
//Assign x address of node already on that location.
node *x=getnode(loc);
//Set next of temp to x.
temp->setnext(x);
//Assign head to temp because now it is on first location
head=temp;
}
//If node is being inserted on last location
else if(loc==size)
{
node *x =getnode(size);//Get address of last node
x->setnext(temp); //Set next of x to temp.
//Set next of temp to NULL because now temp is last node.
temp->setnext(NULL);
}
else //If node is being inserted in middle.
{
//Get address of previous node.
node *x=getnode(loc-1);
//Set next of temp to the next of x.
temp->setnext(x->getnext());
//Now set next of x to temp.
x->setnext(temp);
}
size++;//Increase size to 1.
}
}
//Display function displays all elements of linklist.
void display()
{
node *p;
p=head;//Assign p the address of first node.
while(p) //Loop continues until nodes exists.
{
//Display value of node.
cout<<p->getvalue()<<endl;
//Assign p address of next node.
p=p->getnext();
}
}
//Deletenode function deletes node from linklist on basis of //location passed to it.
void deletenode(int loc)
{
node *last;
node *current1;
if(loc==size) //If last node is deleted.
{
//Get address of second last node in current1.
current1=getnode(size-1);
//Assign next of current1 to NULL because now it is last node.
current1->setnext(NULL);
//Assign current1 address of last node it is node to be deleted
current1=getnode(size);
}
if(loc==1) //If first node is being deleted
{
//Assign last the address of second node.
last=getnode(loc+1);
//Assign head address of last because now itwill be first node.
head=last;
//Assign current1 address of first node it is node to be deleted
current1=getnode(loc);
}
else //Node is being deleted from middle.
{
//Get address of previous node from the node to be deleted in //current1.
current1=getnode(loc-1);
//Get address of node which is after the node to be deleted in //last.
last=getnode(loc+1);
//Set next of current1 to last.
current1->setnext(last);
//Assign current1 address of node to be deleted
current1=getnode(loc);
}
delete current; //Now delete the node.
size--; //Decrease size to 1.
}
};
int _tmain(int argc, _TCHAR* argv[])
{
linklist li; //Create object of linklist.
//Call insertnode function with value 10 & location 1.
li.insertnode(10,1);
//Again call function insertnode with value 50 & location 1.Now //when you will call it with location 1 again first node will go //to second place
li.insertnode(50,1);
//Again call insertnode.
li.insertnode(20,2);
//Again call insertnode.
li.insertnode(30,3);
//Again call insertnode.
li.insertnode(40,4);
//Now display node values before a node is deleted.
cout<<"Before deletion list is"<<endl;
li.display();
//Call deletenode function with location 2.
li.deletenode(2);
//Now display node values after a node is deleted.
cout<<"After deletion list is"<<endl;
li.display();
return 0;
}
Comments
Post a Comment