Welcome to Scribd. Sign in or start your free trial to enjoy unlimited e-books, audiobooks & documents.Find out more
Download
Standard view
Full view
of .
Look up keyword
Like this
5Activity
0 of .
Results for:
No results containing your search query
P. 1
Stackand Queue

Stackand Queue

Ratings: (0)|Views: 457|Likes:
Published by chandni1972
stack & Queues notes
stack & Queues notes

More info:

Published by: chandni1972 on Jan 22, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

04/03/2011

pdf

text

original

 
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 pointingto the next node by means of pointer. In Linked List, elements called Nodes are createdas 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:12594310678Whereas the memory allocated in case of array is12345678910The Linked lists overcome the drawback of array i.e. you cannot increase the elementsonce 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 thedefined data type and allocated during compile time, before actual processing, thereforenumber of elements to be stores must be predetermined. Arrays are allocated mory usingthis technique.For ex.int a[30];Dynamic Memory Allocation – The memory technique allocates memory during runtime, as and when required. It also facilitates release of memory, if memory is notrequired 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 addressof its node . the last node has NULL pointer in its next part.
Types of Linked List1.Singly Linked List
- All those linked list in which nodes consists of two partsi.e. contents and pointers to next node are called Single Linked List. Thus insingly linked list you traverse in forward direction.
2.
Doubly Linked List – 
In this type of lists each node consists of three parts. First part contains theinformation 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.
3. Circular Linked List – 
This is the form of single list, but in this list the last nodecontains 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 listsi.e. searching, Traversal, Insertion, Deletion of node, concatenation, splitting, creation of 
Stack andQueue/XII/Page
1 Ms. Chandni Agarwal
 
InfoInfoInfo
 NULL
Info
 NULL
InfoNull
 
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 willremain same, just either return the top/start pointer or define the variables globally at thetime of structure definition of node in the question(preferably best)
struct node{ int info; // Content part and will contain the informationnode *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 thelinked list.Steps to insert node in the beginning.1.Declare the temporary node and Allocate the memory to a temporary node in thefollowing manner:node *temp = NULL;temp = new node;2.Check for OVERFLOW condition. If temp remains NULL that means no space isallocated.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, andassign 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 temporarynode 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 asPUSH 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 tilllast 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 ismaintained , 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) // traversingtempstart =tempstart ->next;tempstart->next= temp;tempstart=tempstart->link;}}
Deletion (beginning)
Algorithm1.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 nodeand make it start node by start=start->next.4.display/return the information being deleted. delete temp nodeusing delete pointer.Note : The same code can be used in POP() in Stack just by replacingstart with top and Delete() in Queue but a separate pointer front ismade in Queue for deletion of nodes.Code :
Stack andQueue/XII/Page
3 Ms. Chandni Agarwal

Activity (5)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
Sundeep Parkar liked this
Sundeep Parkar liked this
guptha210 liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->