Archive for the ‘link list’ Category

printing Reverse of linked list without reversing it actually

August 27, 2007

1st Method: by a recursive routine or

2nd Method:using a stack.

1st:Recursive
go to the end then print the nodes, i.e. recursive call comes before printing the nodeā€¦
void printreverse(node *node1)
{
if (node1)
{
printreverse(node1->link);
printf(“%d -> “,node1->data);
}
}

2nd method: using stack

Push all the nodes, pop one by one and print them

void printreverse(node* node1)
{
node *tmpnode;

Stack *stack = CreateStack();

while(node1)
{
Push(stack, node1);
node1 = node1->next;
}

tmpnode = Pop(stack);

while(tmpnode)
{
tmpnode = Pop(stack);
printf(“%d -> “, tmpnode->data);
}

DestroyStack(stack);
}

All about Linklist

August 27, 2007

#include<iostream>
#include<stdio.h>
using namespace std;
//class started
class linklist
{
private:
struct node
{
int data;
node *next;
}*p;
public:
linklist();
void addatbeg(int num);
void addatend(int num);
void addatloc(int num,int loc);
void reverse();
int count();
void del(int num);
void display();
~linklist();
};

//initialised..constructor
linklist::linklist()
{ p=NULL; }
//add node at the starting of list

void linklist::addatbeg(int num)
{
node *temp;
temp=new node;
if(p==NULL)
{
temp->data=num;
temp->next=NULL;
p=temp;
}
else if(p!=NULL)
{
temp->data=num;
temp->next=p;
p=temp;
}
}
//add a node at end of the list

void linklist::addatend(int num)
{
node *temp,*r;
if(p==NULL)
{
temp=new node;
temp->data=num;
temp->next=NULL;
p=temp;
}
else if(p!=NULL)
{
temp=p;
while(temp->next!=NULL)
temp=temp->next;
r=new node;
r->data=num;
r->next=NULL;
temp->next=r;
}
}
//add a node at any particular location
void linklist::addatloc(int num,int loc)
{
node *temp,*r;
if(p==NULL)
{
temp=new node;
temp->data=num;
temp->next=NULL;
p=temp;
}
else if(p!=NULL)
{
temp=p;
for(int i=0;i<loc;i++)
{ temp=temp->next;
if(temp==NULL)
{ cout<<“\n there is less than “<<loc<<” element in list :”;
return;
}
}
r=new node;
r->data=num;
r->next=temp->next;
temp->next=r;
}
}
//delete any node given any node->data

void linklist::del(int num)
{
node *temp,*old;
temp=p;
while(temp!=NULL)
{
if(temp->data==num)
{
if(temp==p)
temp=temp->next;
else
old->next=temp->next;
delete temp;
return;
}
else
{
old=temp;
temp=temp->next;
}
}
cout<<“\n element “<<num<<” is not found :”;

}

//reverse the link list 1st method

void linklist::reverse()
{
node *curr,*next1,*prev;
curr=p;
prev=NULL;
while(curr!=NULL)
{
next1=curr->next;
curr->next=prev;
prev=curr;
curr=next1;
}
p=prev;
}

//count the node in list

int linklist::count()
{
node *temp=p;
int count=0;
while(temp!=NULL)
{
temp=temp->next;
count++;
}
return count;
}
//display the list
void linklist::display()
{
node *temp=p;
cout<<“\n\n data is :”;
while(temp!=NULL)
{ cout<<temp->data<<” “;
temp=temp->next;
}
}
//memory delloacated,destructor
linklist::~linklist()
{
node *temp;
while(p!=NULL)
{
temp=p->next;
delete p;
p=temp;
}
}
//start or program..main
int main()
{
linklist l;
l.addatbeg(3);
l.addatbeg(4);
l.addatbeg(78);
l.addatend(32);
l.addatbeg(67);
l.addatbeg(34);
l.addatend(39);
l.addatbeg(234);
l.addatbeg(1);
l.addatloc(5,3);
l.addatloc(9,4);
l.display();
cout<<“\n\n\n total no of node is :”<<l.count();
l.del(32);
l.display();
cout<<“\n\n\n total no of node is :”<<l.count();
cout<<endl<<endl;
cout<<“\n\n\n reverse of link list is :”;
l.reverse();
l.display();
cout<<endl<<endl;
system(“pause”);
return 0;
}