0% found this document useful (0 votes)
7 views3 pages

Datastruct Notes # 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Datastruct Notes # 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

DATASTRUCT NOTES #2 6.

[insert element] set A[K] = ITEM


7. [reset N] set N=N+1
ALGORITHM [LINEAR SEARCH] 8. Exit
Steps:
1. Assume the target has not been found. LINKED LIST
2. Start with the initial array element.  A collection of items [nodes] that can be
3. Repeat while the target is not found and scattered about in memory, not
there are more array elements. necessarily in consecutive memory
4. If the current element matches the locations.
target, set a flag to indicate that the o A node, implemented as a struct,
target has been found. consists of two members:
Else, advance to the next array element.
1. Component member
5. If the target is found, return the target - Contains one of the data values
index as the search result. in the list.
Else, return -1 as the search result. 2. Link member

Note: don’t forget to break the loop if item An abstract diagram of a linked list:
is already found.
Component Link
ALGORITHM [BINARY SEARCH] (Data) (Location of next
Steps: node)
1. [Initialize segment variables] set:
 First = LB Head – starting index
 Last = UB
 Mid = (First+Last)/2 Linked list represented as an array:
2. Repeat steps 3 and 4 while first<=last
and A[Mid] != ITEM Struct NodeType {
3. If ITEM<A[Mid], set last = mid – 1 int component;
Else, set first = mid + 1 [end of if int link;
structure] };
4. Set mid = (first+last)/2
[end of step 2 loop] NodeType list[1000];
5. If A[Mid} == ITEM, set Loc = Mid int head;
Else, set Loc = Null [end of structure]
6. Exit
Linked list sample code:
RECURSIVE ALGORITHM [BINARY
SEARCH]
#include <stdio.h>
procedure binary search(x,i,j)
m: =[(i+j)/2] struct list {
if x = am, then char letter;
location = m struct list * next;
else if (x<am and i<m), then } a,b,c,d,e;
binary search(x,i,m-1)
else if (x>am and j>m), then int main() {
binary search(x ,m+1,j) struct list *list_pointer = &a;
else location = 0 int count = 0;

ALGORITHM [INSERT INTO AN ARRAY] a.letter = 'L';


This algorithm inserts an ITEM into array A with: a.next = &b;
 N – elements
b.letter = 'U';
 K – position to insert
 J – counter b.next = &c;
Where K<=N c.letter = 'N';
c.next = &d;
Steps: d.letter = 'A';
1. [Initialize counter] set: d.next = &e;
 J=N e.letter = 'S';
2. Repeat steps 3 and 4 while J>=K e.next = NULL;
3. [move Jth element downward] set:
 A[J+1]=A[J] while(list_pointer != 0) {
4. [decrease counter] set J=J-1 printf("%c ", list_pointer->letter);
5. [end of step 2 loop]
count++;
list_pointer = list_pointer->next;
}
Linked list ARRAY VER sample code:  new DataType[intExpression];
#include <stdio.h> its demonstration:
int *intPtr;
#define size 5
char *nameStr;
struct list {
char data;
intPtr = new int;
struct list * next; nameStr = new char[6];
}value[size];
An example of creating dynamic data
int main() { and then accessing the data through
int count = 0; pointers:
struct list value[size]; #include <string.h>
struct list *list_pointer = &value[0]; :
:
value[0].data = 'L'; int *intPtr = new int;
value[0].next = &value[3]; char *nameStr = new char[6];
value[1].data = 'N';
value[1].next = &value[2]; *intPtr = 357;
value[2].data = 'A'; Strcpy(nameStr, “Ben”);
value[2].next = &value[4];
value[3].data = 'U';
value[3].next = &value[1]; Allocating Dynamic Data on the Free
value[4].data = 'S'; Store
*intPtr = 357;
value[4].next = NULL;
strcpy(nameStr, “Ben”);
(insert illustration)
while(list_pointer != 0) {
printf("%c ", list_pointer->data);
count++;
list_pointer = list_pointer->next;
}
printf("\nNumber of elements: %d", count); Two forms of the delete operation:
 delete Pointer;
return 0;  delete [] Pointer;
}
MEMORY LEAK
- the loss of available memory space that
occurs when dynamic data is allocated
POINTERS
but never deallocated.
 Is a variable that tells where to find
something else
An example of using dynamic data:
 It contains the address or location of
int *ptr1 = new int;
another variable.
int *ptr2 = new int;
o It will contain the ADDRESS of the
VARIABLE BEING POINTED TO,
not the actual value. *ptr2 = 44;
 DECLARATION: *ptr1 = *ptr2;
o data-type* variable; ptr1 = ptr2;
o data-type *variable, *variable; delete ptr2;

Result of executing the code fragment:


Implementation may be:
INITIAL CONDITIONS:
1. Using ARRAYS (Static)
2. Using POINTERS (Dynamic)

DYNAMIC DATA AFTER:


 variables created during execution of a
program by means of special operations.
These operations are new and delete.

Two forms of the new operation:


 new DataType;
INACCESSIBLE OBJECT
- a dynamic variable on the free store
without any pointer pointing to it.

DANGLING POINTER
- a pointer that points to a variable that
has been deallocated.

REVISED CODE FRAGMENT:


#include <stdio.h> //for NULL
:
int *ptr1 = new int;
int *ptr2 = new int;

*ptr2 = 44;
*ptr1 = *ptr2;
delete ptr1; //to avoid an inaccessible object
ptr1 = ptr2;
delete ptr2;
ptr1 = NULL; // to avoid a dangling pointer
}

You might also like