Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT

2 P OINTER 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;

3 P OINTER I N 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

4 P OINTER E XAMPLE 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*/

5 E XAMPLE O F POINTER I N C++ # include 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(); }

6 The new operator p = new int Dynamic allocation of a memory cell that can contain 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

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

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

9 L INKED L IST 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.

10 A DVANTAGES OF L INKED L IST OVER A RRAY 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.

11 O PERATION O N L INKED L IST 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

12 O PERATION O N L INKED L IST … C ONT 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.

13 T YPES O F L INKED L IST 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

14 A S INGLY L INKED L IST 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.

15 C REATE N ODE U SING C++ A node in a linked list is usually a struct struct 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

16 A LGORITHM TO C REATE FIRST N ODE 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. DataNull Start Cur Node

17 I NSERT A T F IRST 1- Start 2- Prepare Node 3- Read Data 4- Node ->Data=Data 5- Node->next=Start 6- Start=Node 7-End. DataNull Start Cur Data New Node

18 I NSERT A T L AST 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 5 6 Start Cur 7Null New Node

19 I NSERT A T A NY 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

20 I NSERT A T A NY P OSITION New Node Temp

21 D ELETE N ODE A T F IRST 1-Start 2- temp=Start 3- Start= Start -> next 4- Delete ( temp ) 5- temp=Null 6- End. Start New Node

22 D ELETE A T L AST 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.

23 D ELETE A T A NY P OSITION 1- Start 2- Let Temp=start 3- Read Pos 4- Cn=1 5- while ( Cn< pos – 1 ) 6- temp=temp -> next 7- Cn=Cn+1 8- End While 9- p= temp -> next 10- temp->next=temp ->next -> next 11- Delete (p) 12- p=null 13- end

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

25 S EARCHING 1- Start 2- let temp=start 3- Read key 4- 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

26 A SSIGNMENT Write Algorithm to implement Stack using SLL Write Algorithm to Implement Queue Using SLL Write Algorithm to Sorting SLL


Download ppt "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."

Similar presentations


Ads by Google