Date post: | 03-Jan-2016 |
Category: |
Documents |
Upload: | edgar-anderson |
View: | 213 times |
Download: | 0 times |
DATA STRUCTURE
Ali Abdul Karem Habib
MSc.IT
POINTER A pointer is a variable which represents the
location of a data item . We can have a pointer to any variable type.
Pointer Operators- The address operator & gives the
``address of a variable''. - The indirection operator * gives the
``contents of an object pointed to by pointer''.
To declare a pointer to a variable do: int *ptr;
POINTER IN C++ Declare pointers to any data type using dereference
operator *int* int_ptr;char* ch_ptr;A pointer takes its value as the address in memory of
another variable
int b, a = 6;int* a_ptr;
a_ptr = &a; /* ‘&’ : address of operator */
B= *a_ptr ; /* ‘*’ : dereference operator */
6
address of ‘a’
a
a_ptr
POINTER EXAMPLE
Consider the effect of the following code:
int x = 1, y = 2;
int *ip; /*pointer to an integer*/
ip = &x; /*assign address of x to
ip*/
y = *ip; /*assign value of x to y*/
x = ip; /*assigns address of x to
x*/
*ip = 3; /*assigns 3 to value of x*/
EXAMPLE OF POINTER IN C++ # include < iostream.h >
Void main () {
Int a =30, *ptr ; ptr=&a; Cout<<“ Print the address Of a “<<endl; Cout<< ptr; Cout<< “ The containt Of a “<< endl; Cout<<*ptr; getch(); }
The new operator p = new int • Dynamic allocation of a memory cell that
cancontain an integer . The delete operator returns dynamically
allocated memory to the system for reuse . A pointer to a deallocated memory (*p) cell is
p = NULL
(a) Declaring pointer variables (b) pointing to statically allocated memory (c) assigning a value(d) allocating memory dynamically (e) assigning a value
(f) copying a pointer(g) allocating memory dynamically and assigning a value(h) assigning NULL to a pointer variable(i) deallocating memory
LINKED LIST
It is another type of data structure which are dynamic allocation .
It is collection of especially designed data elements called nodes linked to one another by means of pointers.
Each node is divided into 2 parts first part contains the Data and the second contains Pointer which points to the next node.
ADVANTAGES OF LINKED LIST OVER ARRAY
Array have a fixed dimension. Once the size of an array is decided it can not be increased during execution.
Array elements are always stored in contiguous memory locations .
Insertion and deletion in array is complex because it requires shifting of elements in array.
It is a dynamic data structure, i.e. a linked list can grow and shrink in size during its lifetime.
The nodes of linked list (elements) are stored at different memory locations .
Insertion and deletion in linked list is easy because it does not requires shifting of elements in it.
OPERATION ON LINKED LIST
Creation :- Creation operation is used to create
a linked list which one node .
Insertion: - Insertion operation is used to insert
a new node at any specified location in the
linked list. A new node may be inserted.
(a) At the beginning of the linked list
(b) At the end of the linked list
(c) At any specified position in between in a
linked list
Deletion :- Same Above
OPERATION ON LINKED LIST … CONT
Traversing : - Traversing is the process of
going through all the nodes from one end to
another end of a linked list.
Searching :- Usually searching operations is
employed not only while a data item is
needed but in case of insertion and deletion
at specified location search operation is
performed before nodes can be inserted or
deleted.
TYPES OF LINKED LIST
Basically we can divide the linked list into the
following three types in the order in which
node are arranged.
Singly linked list
Doubly linked list
Circular linked list
A SINGLY LINKED LIST
is one in which all nodes are linked together in some sequential manner. By single pointer Hence it is also called Linear linked list.
Each node in a singly Linked List has two parts one to hold the data and other to point to address of the successor node (next node).
The problem with this list is that we cannot access the predecessor nodes from the current i.e. we cannot backward traverse in a singly linked list.
CREATE NODE USING C++ A node in a linked list is usually a structstruct Node{ int data ;
Node *next;};
Node * Start , *Cur;
Node *p; // pointer to node p = new Node; // allocate node Note :- Access to the Node elements by Pointer using
-> operator p->data , p->next Access to the Node Structure by reference of structure
using Node item ; item. data , item. next
ALGORITHM TO CREATE FIRST NODE
1- Start 2- If Start=Null then 3- Prepare Node 4- Read data 5- Node ->data= data 6- Node ->next=Null 7- Start = Node 8- Cur = Node 9- End .
Data NullStart
Cur
Node
INSERT AT FIRST 1- Start 2- Prepare Node 3- Read Data 4- Node ->Data=Data 5- Node->next=Start 6- Start=Node 7-End.
Data NullStart
CurData
New Node
INSERT AT LAST 1- Start 2- Prepare Node 3- Read Data 4- Node ->Data=Data 5- Cur ->next=Node 6- Cur=node 7- Cur ->next=Null 8- End
56Start
Cur
7 Null
New Node
INSERT AT ANY POSITION 1- Start 2- Prepare Node 3- Read Data 4- Read Pos 5- Node -> Data=Data 6- temp =start 7- Let Cn=1 8- while( Cn < pos-1) 9- temp=temp-> next 10- Cn=Cn+1 11- End While 12- Node -> next=temp ->next 13- temp ->next=node 14- End
INSERT AT ANY POSITION
New Node
Temp
Temp
DELETE NODE AT FIRST 1-Start 2- temp=Start 3- Start= Start -> next 4- Delete ( temp ) 5- temp=Null 6- End .
StartNew Node
DELETE AT LAST
1- Start 2- Let Temp = Start 3- While ( temp ->next <> Cur) 4- temp=temp ->next 5- End While 6- Delete (Cur) 7- Cur=temp 8-Cur->next=null; 9- temp=null 10- End .
DELETE AT ANY POSITION
1- Start 2- Let Temp=start 3- Read Pos4- Cn=15- while ( Cn< pos – 1 ) 6- temp=temp -> next7- Cn=Cn+1 8- End While 9- p= temp -> next10- temp->next=temp ->next -> next11- Delete (p) 12- p=null 13- end
TRAVERSING
1- Start 2- temp=start 3- While ( temp <> null ) 4- print (temp -> Data ) 5- temp = temp -> next 6- End while 7- End .
SEARCHING
1- Start 2- let temp=start3- Read key4- While ( temp <> null) 5- If ( temp -> Data = key ) then 6- print ( “ Found “)7- else
Print ( “Not Found “ ) 8- temp = temp -> next ) 9- End While 10 - End
ASSIGNMENT Write Algorithm to implement Stack using
SLL Write Algorithm to Implement Queue Using
SLL Write Algorithm to Sorting SLL