d ata s tructure ali abdul karem habib msc.it. p ointer a pointer is a variable which represents the...

26
DATA STRUCTURE Ali Abdul Karem Habib MSc.IT

Upload: edgar-anderson

Post on 03-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

DATA STRUCTURE

Ali Abdul Karem Habib

MSc.IT

Page 2: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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;

Page 3: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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

Page 4: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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*/

Page 5: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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(); }

Page 6: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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

Page 7: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

(a) Declaring pointer variables (b) pointing to statically allocated memory (c) assigning a value(d) allocating memory dynamically (e) assigning a value

Page 8: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

(f) copying a pointer(g) allocating memory dynamically and assigning a value(h) assigning NULL to a pointer variable(i) deallocating memory

Page 9: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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.

Page 10: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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.

Page 11: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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

Page 12: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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.

Page 13: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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

Page 14: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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.

Page 15: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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

Page 16: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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

Page 17: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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

Page 18: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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

Page 19: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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

Page 20: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

INSERT AT ANY POSITION

New Node

Temp

Page 21: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

Temp

DELETE NODE AT FIRST 1-Start 2- temp=Start 3- Start= Start -> next 4- Delete ( temp ) 5- temp=Null 6- End .

StartNew Node

Page 22: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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 .

Page 23: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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

Page 24: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

TRAVERSING

1- Start 2- temp=start 3- While ( temp <> null ) 4- print (temp -> Data ) 5- temp = temp -> next 6- End while 7- End .

Page 25: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

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

Page 26: D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to

ASSIGNMENT Write Algorithm to implement Stack using

SLL Write Algorithm to Implement Queue Using

SLL Write Algorithm to Sorting SLL