pointer and linked list

download pointer and linked list

of 8

Transcript of pointer and linked list

  • 8/7/2019 pointer and linked list

    1/8

    POINTER:-

    A pointer is a variable that holds the address of another variable.

    POINTER TO POINTER:-Pointer-to-pointer holds address of another pointer. So it requires double

    dereferencing to obtain the value object.

    EXAMPLE:-

    int **p;

    This is a declaration of pointer to pointer for int object.C++ allows the use of pointers that point to pointers, that these, in its turn,point to data (or even to other pointers). In order to do that, we only need toadd an asterisk (*) for each level of reference in their declarations:int a;

    int * b;

    int ** c;

    a = '20';

    b = &a;

    c = &b;

    This, supposing the randomly chosen memory locations for each variableof 7230, 8092 and 10502, could be represented as:

    a b c

    7230 8092 10502

    The value of each variable is written inside each cell; under the cells aretheir respective addresses in memory.The new thing in this example is variable c, which can be used in threedifferent levels of indirection, each one of them would correspond to adifferent value:

    20 7230 8092

  • 8/7/2019 pointer and linked list

    2/8

    y c has type char** and a value of8092y *c has type char* and a value of7230

    y **c has type char and a value of'20'

    PROGRAM:-

    int a=20;

    int **b;

    int **c;

    b=&a;

    c=&b;

    printf(\n value of a is %d,a); 20

    printf(\n &a=%u,&a); 7230

    printf(\n b=%u,b); 7230printf(\n &b=%u,&b); 8092

    printf(\n *b %d,*b); 20

    printf(\n c %u,c); 8092

    printf(\n &c=%u,&c); 10502

    printf(\n **c %u,**c); 20

    printf(\n *c %u,*c); 7230

    LINKED LIST:-

    A linked list or one way list is a linear collection of data elements, callednodes, where the linear order is given by means of pointers .That is eachnode is divided into twoparts: the first part contains the information of theelement, and the second part, called the linked list or next pointer field,contains the address of next node in the list.

    The pointer of last node contains a special value, called the null pointer,which is any invalid address.

    0 and negative number is used for the null pointer.

    EXAMPLE:-

    A hospital ward contains 12 beds, of which 9 are occupied .We want analphabetical listing of the patients. we use variable START to point first

  • 8/7/2019 pointer and linked list

    3/8

    patient. Hence START contains 5, since the first patient, Adams, occupiesbed 5.Also Adamss pointer is equal to 3;Since Dean the next patient.

    START

    REPRESENTATION OF LINKED LIST IN MEMORY:-

    LIST be a linked list. LIST requires two linear arrays, we willcall them here INFO and LINK such that INFO[K] and LINK[K] contain,

    respectively, the information part and the nextpointer field of a node ofLIST.LIST also requires a variable name such as START , which containsthe location of the beginning of the list, and a next pointer sentinel denotedby NULL ,which indicates the end of the list.

    Bed

    Number

    Patient

    1 Kirk

    2

    3 Dean

    4 Maxwell

    5 Adams

    6

    7 Lane

    8 Green

    9 Samuels

    10

    11 Fields

    12 Nelson

    Next

    7

    11

    12

    3

    4

    1

    0

    8

    9

    5

  • 8/7/2019 pointer and linked list

    4/8

    INFO LINK

    START

    1

    2

    3 I

    4

    5 T

    6

    7 O

    8 P

    9

    10 T

    11

    12 E

    13 L

    14

    15 M

    16

    17 E

    18

    19 C

    20

    21 S

    22

    23 L

    24

    25

    21

    12

    15

    23

    199

    0

    3

    8

    5

    7

    10

    17

    13

  • 8/7/2019 pointer and linked list

    5/8

  • 8/7/2019 pointer and linked list

    6/8

    1.Set PTR := START.[Initializes pointer PTR]

    2.Repeat steps 3 and 4 while PTR=NULL.

    3. Apply PROCESS to INFO[PTR].

    4. Set PTR :=LINK[PTR].[PTR now points to the next

    node.]

    [End of step 2 loop.]

    5.Exit.

    SEARCHING A LINKED LIST:-

    LIST is a linked list in memory.This algorithm finds the locationLOC of the node where ITEM first appears in LIST, or sets LOC =NULL.

    1.Set PTR := START.2.Repeat steps while PTR=NULL:3. If ITEM = INFO[PTR], then:

    Set LOC := PTR, and Exit.

    Else:

    Set PTR := LINK[PTR]. [PTR now points to the nextnode.]

    [End of if structure.]

    [End of step 2 loop.]

    4.[Search is unsuccessful.] Set LOC := NULL.5.Exit.

    GARBAGE COLLECTION:-

    y Free storage list or avail list.

    y Garbage collection is invisible to programmer.

    y Garbage collection takes place when:

    CPU is idle

  • 8/7/2019 pointer and linked list

    7/8

    Running out of space

    EXAMPLE:-

    Suppose the list of patients is stored in the linear arraysBED and LINK. Then the available space in the lineararray BED may be linked, the BED [10] is the firstavailable bed, BED [2] Is the next available bed ,andBED[6] is the last available bed. Hence BED[6] has thenull pointer in its nextpointer field; that is LINK[6]=0.

    BED LINK

    START

    AVAIL

    Kirk

    Dean

    Maxwell

    Adams

    Lane

    Green

    Samuels

    Fields

    Nelson

    7

    6

    11

    12

    3

    0

    4

    1

    0

    2

    8

    9

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    5

    10

  • 8/7/2019 pointer and linked list

    8/8

    MALLOC FUNCTION IN C:-

    malloc - a memory allocator

    The malloc() function shall allocate unused space for anobject whose size in bytes is specified by size and whosevalue is unspecified.