Sunteți pe pagina 1din 10

Chapter 10 (Linked List,Stack and Queues)

Chapter 10 (Linked List, Stack and Queues)

Linked List – A linked list is a linear collection of data elements called NODES pointing
to the next node by means of pointer. In Linked List, elements called Nodes are created
as and when required, at that memory location which is currently available.
In such process memory locations allotted to the nodes may not be contiguous as shown
below:
1 2 5 9
4 3 10
6 7 8
Whereas the memory allocated in case of array is
1 2 3 4 5 6 7 8 9 10

The Linked lists overcome the drawback of array i.e. you cannot increase the elements
once defined, and if you need less elements to store ,will results into wastage of space.

Memory Allocation
Static Memory Allocation – In this memory technique the memory is reserved for the
defined data type and allocated during compile time, before actual processing, therefore
number of elements to be stores must be predetermined. Arrays are allocated mory using
this technique.
For ex.int a[30];
Dynamic Memory Allocation – The memory technique allocates memory during run
time, as and when required. It also facilitates release of memory, if memory is not
required any more. Data structures like linked list and trees use this technique for their
memory location.
Representation of Linked list in memory
Memory is allocated at run time to the node using new operator. Each node has address
of its node . the last node has NULL pointer in its next part.
Types of Linked List
1. Singly Linked List - All those linked list in which nodes consists of two parts
i.e. contents and pointers to next node are called Single Linked List. Thus in
singly linked list you traverse in forward direction.
Info NULL
Info ∙ Info ∙
2. Doubly Linked List – In this type of lists each node consists of three parts. First part contains the
information and two pointers in which one pointer points to the previous node an one pointer
points to the next node. The traversal is possible in both directions.

NULL Info ∙ Info Null

3. Circular Linked List – This is the form of single list, but in this list the last node
contains the address of first node. Circular queue is implemented with this technique.

Basic Operations on a singly linked list - Various operations can be performed on lists
i.e. searching, Traversal, Insertion, Deletion of node, concatenation, splitting, creation of

Stack andQueue/XII/Page 1 Ms. Chandni Agarwal


Chapter 10 (Linked List,Stack and Queues)

node. But the discussion is limited to insertion(in beginning , at end) and deletion(from
beginning) and traversal only.
Always define a node in the question and these functions are assumed as a class function.
If in the question no class is given , only functions are to developed then the code will
remain same, just either return the top/start pointer or define the variables globally at the
time of structure definition of node in the question(preferably best)

struct node{ int info; // Content part and will contain the information
node *next; // Pointer which will point to next node and contain its address.
};

Insertion
In a linked list the item can be added to the beginning, in the middle, or at the end of the
linked list.
Steps to insert node in the beginning.
1. Declare the temporary node and Allocate the memory to a temporary node in the
following manner:
node *temp = NULL;
temp = new node;

2. Check for OVERFLOW condition. If temp remains NULL that means no space is
allocated.
if (temp == NULL)
{ cout<<”No free space available …OVERFLOW!!!!!”; exit(1); }
3. if the sufficient memory is available then copy / accept the value in info part, and
assign NULL to its next pointer.
temp->info=ele;
temp->next=NULL;
These above three parts are for allocation of new node.
4. if start pointer is NULL, i.e. no nodes are in the linked list, make the temporary
node start node otherwise assign the address of start node to the next pointer of
temporary node and make the temporary node start node in the following manner:
if(start==NULL)
start= temp;
else
{
temp->next=start;
start=temp;}
The code of inserting the node in beginning is given , same code can be applied as
PUSH Function and replacing start pointer with top pointer.
void ins_beg(int ele)
{
node *temp = NULL;
temp = new node;
if (temp == NULL)
{ cout<<”No free space available …OVERFLOW!!!!!”; exit(1); }

Stack andQueue/XII/Page 2 Ms. Chandni Agarwal


Chapter 10 (Linked List,Stack and Queues)

temp->info=ele;
temp->next=NULL;
if(start==NULL)
start= temp;
else
{
temp->next=start;
start=temp;}
}
II Insertion in the end of list
Algoritjm:
1. allocate the node as previously allocated.(step 1- 3).
2. if start pointer is NULL then make the temp node as start node otherwise traverse till
last node part and assign the temp node to the next part of last node.
Note : In case of Queue where insertion takes place at rear end, a separate pointer rear is
maintained , so there is no need of traversing the whole list.
The complete code is given below:
void ins_end(int num)
{
node *tempstart=start, *temp;
temp=new node;
if (temp == NULL)
{ cout<<”No free space available …OVERFLOW!!!!!”; exit(1); }
temp->info=ele;
temp->next=NULL;
if(start==NULL)
{
tempstart->next = temp;
}
else
{
while(tempstart->next!=NULL) // traversing
tempstart =tempstart ->next;

tempstart->next= temp;
tempstart=tempstart->link;
}
}

Deletion (beginning)
Algorithm
1. Declare the temp pointer.
2. Check for Overflow condition and exit otherwise perform step 3.
3. assign the start address to temporary node.move to the next node
and make it start node by start=start->next.
4. display/return the information being deleted. delete temp node
using delete pointer.
Note : The same code can be used in POP() in Stack just by replacing
start with top and Delete() in Queue but a separate pointer front is
made in Queue for deletion of nodes.
Code :

Stack andQueue/XII/Page 3 Ms. Chandni Agarwal


Chapter 10 (Linked List,Stack and Queues)

int delatbeg()
{
int val;
node *temp;
if(start == NULL)
{
cout<<”No node to delete …..UNDERFLOW!!!!!!!” ; exit(1); }
else
{
temp=start
start =start->next;
val=temp->info;
delete temp;
}

return val;
}
In case of deleting the node at end of list, the start address assigned
to a temporary node, traversal is done till last and the node is
deleted.
In case of deletion of a node from middle position based on the
searching of Item, the address of the next node is assigned to its
previous node.

Traversal of Linked List


Algorithm
1. assign the starting address to the temporary node.
2. move to the next node by the statement = temp=temp->next in a
while loop till the temp->next reaches to NULL value.
Note: In stack and Queue traversal same code can be used. In satck
replace stack with top. And in queue replace start with front and NULL
with rear.
Code:
Void traversal()
{
node *temp=start;
while(temp->next=NULL)
{
cout<<temp->info<< “ “;
temp-temp->next;
}
}
STACK
Stack is LFO structure and physically it can be implemented as
array( Static Stack) or as a Linked List(Dynamic Stack). An Insertion
in Stack is called PUSH and Deletion is called POP. Insertion and
deletion always occurs at one end i.e. top.

Static Stack as an array

#include<iostream.h>
#include<conio.h>
#include<process.h>
const int size = 10;
class Stack{
int stk[size];

Stack andQueue/XII/Page 4 Ms. Chandni Agarwal


Chapter 10 (Linked List,Stack and Queues)

int top;
public:
Stack() { top = -1; }
Void push(int ele) ;
Int pop();
Void disp();
};
Void stack::push(int ele)
{
if(top == size-1)
{
cout<<”Overflow Error…”; return; }
else
stk[top++]=ele; }

int stack::pop()
{
int val;
if(top==-1)
{ cout<<”UNDERFLOW…”;return -1; }
else
{val=stk[top--];
return val;
}
}
void stack::disp()
{
for(int i=top;i>=0; i--)
cout<<” “<<stk[i];
}
void main()
{
Stack S;
s.push(10);
s.disp(); s.pop();
s.disp();
}
* Try to check the program with user given values in the main
program

In case of exit it will come out form the program, in case of


return it will come out from the called function.
In case of non member functions pass the arguments i.e.
Void push(int a[],int &top, int ele)
DYNAMIC STACK (LINKED LIST IMPLMENTATION)
#include<iostream.h>
#include<conio.h>
#include<process.h>
struct node { int info;
node *next;
};

class stack{ node *top;


public:
stack() { top=NULL; }
void push(int ele);
int pop();

Stack andQueue/XII/Page 5 Ms. Chandni Agarwal


Chapter 10 (Linked List,Stack and Queues)

void disp();
};

void stack::push(int ele)


{
node *temp = NULL;
temp = new node;
if (temp == NULL)
{ cout<<”No free space available …OVERFLOW!!!!!”; exit(1); }
temp->info=ele;
temp->next=NULL;
if(top==NULL)
top= temp;
else
{
temp->next=top;
top=temp; }
}
int stack::pop()
{
int val;
node *temp;
if(top == NULL)
{
cout<<”No node to delete …..UNDERFLOW!!!!!!!” ; exit(1); }
else
{
temp=top
top = top ->next;
val=temp->info;
delete temp;
}

return val;
}
void stack::disp()
{
node *temp=top;
cout<<"Displaying Stack........";
for(;temp!=NULL;temp=temp->next)
{
cout<<"Value :"<<temp->info;
}
return;
}
void main()
{ Stack s;
for(int i=0;i<3;i++)
s.push();
s.disp();
getch();
for(i=0;i<3;i++)
{
s.pop();

Stack andQueue/XII/Page 6 Ms. Chandni Agarwal


Chapter 10 (Linked List,Stack and Queues)

s.disp();
getch();
} }

Applications of Stack
1. reversing the string.
2. Converting Decimal to binary,Octal
3. Polish Notation (conversion pf Infix to Postfix/Prefix,
Evaluation of Pstfix/Prefix expression.
Note : In the question of Conversions , The Infix expression is given
and postfix form is desired. Don’t evaluate the expression until asked
and be careful about the rules of conversion. In case of evaluation,
postfix form is given.
Queue – Logically queue is a FIFO structure. And physically
it can be implemented in terms of array as well as Linked
List. Insertion takes place at rear end and deletion takes
place at front end.
Static QUEUE
#include<iostream.h>
#include<conio.h>
#include<process.h>
const int size=10;
class queue{
int ar[size];
int front,rear;
public:
queue()
{
front=rear=-1;
}
void ins(int ele);
int del();
void disp();
};
void queue::ins(int ele)
{
if(rear==size-1)
{ cout<<"OVERFLOW.......!!!"; exit(1); }
else
if(rear== -1)
{rear=front=0; ar[rear]=ele; }
else
{ rear++; ar[rear]=ele; } }

int queue::del()
{
int val;
if(front==-1)
{ cout<<"UNDERFLOW.......!!!"; exit(1); }
else
{val=ar[front];
if(front==rear)
front=rear=-1;
else
front++;
}

Stack andQueue/XII/Page 7 Ms. Chandni Agarwal


Chapter 10 (Linked List,Stack and Queues)

return val; }

void queue::disp()
{
if(front==-1) return;
for(int i=front ;i<rear;i++)
cout<<ar[i];
getch();
}
void main()
{
queue q;
for(int j=0;j<10;j++)
q.ins(j);
q.disp(); cout<<q.del()<<endl; q.disp();
cout<<q.del()<<endl; q.disp(); }

Dynamic Queue(LINKED LIST IMPEMENTATION)


#include<iostream.h>
#include<process.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
struct node {
char name[25];
int age;
node *next;
};
class Queue {
node *front,*rear;
public:
Queue()
{
front=rear=NULL;
}
void Qins();
void Qdel();
void Qdisp();
};
void Queue::Qins()
{
node *temp;
temp=new node;
if(temp==NULL)
{
cout<<"OVERFLOW.....";
exit(0);
}
else
{
cout<<"Enter Name :"; gets(temp->name);
cout<<"Enter age:"; cin>>temp->age;
temp->next=NULL; }
if(rear==NULL)
front=rear=temp;
else
{ rear->next=temp; rear=temp; }

Stack andQueue/XII/Page 8 Ms. Chandni Agarwal


Chapter 10 (Linked List,Stack and Queues)

return;
}

void Queue::Qdel()
{
node *temp;
if(front==NULL)
{
cout<<"UNDERFLOW...";
getch();
exit(0);
}
else
{
temp=front; front=front->next; cout<<temp->name; cout<<temp->age;
cout<<"Deleted Record ..\n";
cout<<"Press any key to proceed...and out of pop\n";
getch();
delete temp; }
return; }
void Queue::Qdisp()
{
node *temp=front;
cout<<"Displaying Stack........";
for(;temp!=NULL;temp=temp->next)
{
cout<<"Name :"<<temp->name; cout<<"\n Age :"<<temp->age;
}
return;
}
void main()
{
clrscr();
Queue q;
for(int i=0;i<3;i++)
q.Qins();
clrscr();
q.Qdisp();
getch();
for(i=0;i<4;i++)
{
q.Qdel(); cout<<"\n\n"; q.Qdisp(); getch();
} }

Variations in Queues
Queues can be used in several forms and ways depending upon the
requirements of the program. There are two popular variations of queues
are circular queue and dequeus (double ended queues).
Circular queues are implemented in circular form rather than a straight
line. It also overcomes the drawback of unutilized space of Linear
queues.
Deque are the refined queues in which element can be added or removed
at either end but not in the middle.There are two variations of a deque
– an input restricted deque is a deque which allows insertions at only
one end but allows deletion at both ends of the list.An output
restricted deque is a deque which allows deletions at only one end but
allows insertion at both ends of the list.

Stack andQueue/XII/Page 9 Ms. Chandni Agarwal


Chapter 10 (Linked List,Stack and Queues)

Circular Queue (Static – ARRAY IMPLEMENTATION)


Code Given In book Pgno.553
Circular Queue (DYNAMIC – LINKED LIST IMPLEMNATTION)
Insertion
void Queue::Qins()
{
node *temp;
//Same as Linear Que Insertion
if(rear==NULL)
front=rear=temp;
else
{ rear->next=temp; rear=temp;
front->next=rear; //Only this is to be written in case of CircQ }
return;
}
Deletion
void Queue::Qdel()
{
//Same as Qdeletion

else
{
temp=front; front=front->next; front->next=rear; //only this change
cout<<temp->name; cout<<temp->age;
//Same Code as in Qdel
return; }

Stack andQueue/XII/Page 10 Ms. Chandni Agarwal

S-ar putea să vă placă și