Data Structures and Algorithms: What Are Pointers?
Data Structures and Algorithms: What Are Pointers?
Lab 1
type *var-name;
Here, type is the pointer's base type; it must be a valid C++ type and var-name is the name of the
pointer variable. The asterisk you used to declare a pointer is the same asterisk that you use for
multiplication. However, in this statement the asterisk is being used to designate a variable as a
pointer.
Null Pointer:
If you do not know the exact address to be assigned to a pointer Null value should be
assigned to it. A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries,
including iostream. Consider the following program
#include <iostream>
return 0;
}
To check for a null pointer you can use an if statement as follows −
if(ptr) // succeeds if p is not null
if(!ptr) // succeeds if p is null
Pointers vs Arrays:
Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable in
many cases. A pointer that points to the beginning of an array can access that array by
using either pointer arithmetic or array-style indexing.
#include <iostream>
int main () {
int var[MAX] = {10, 100, 200};
int *ptr;
return 0;
}
#include <iostream>
#include <ctime>
int main () {
unsigned long sec;
getSeconds( &sec );
return 0;
}
return;
}
Data Structures and Algorithms
Lab 1
#include <iostream>
using namespace std;
// function declaration:
double getAverage(int *arr, int size);
int main () {
// an int array with 5 elements.
int balance[5] = {1000, 2, 3, 17, 50};
double avg;
return 0;
}
return avg;
}
Structures:
C/C++ arrays allow you to define variables that combine several data items of the same
kind, but structure is another user defined data type which allows you to combine data
items of different kinds.
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a
new data type, with more than one member, for your program. The format of the struct
statement is this. For example, to define a book structure
struct Books {
char title[50];
char author[50];
Data Structures and Algorithms
Lab 1
char subject[100];
int book_id;
} book;
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
void printBook( struct Books book ) {
Data Structures and Algorithms
Lab 1
Pointers to Structures
You can define pointers to structures in very similar way as you define pointer to any
other variable as follows −
struct Books *struct_pointer;
Now, you can store the address of a structure variable in the above defined pointer
variable. To find the address of a structure variable, place the & operator before the
structure's name as follows −
struct_pointer = &Book1;
To access the members of a structure using a pointer to that structure, you must use the
-> operator as follows −
struct_pointer->title;
Let us re-write above example using structure pointer, hope this will be easy for you to
understand the concept
#include <iostream>
#include <cstring>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// Book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// Book 2 specification
strcpy( Book2.title, "Telecom Billing");
Data Structures and Algorithms
Lab 1
return 0;
}
Problems:
1.Write a program to add, subtract and multiply two complex numbers using
structures to function.
int main()
{
struct node ob;
return 0;
}
In the above example ‘link’ is a pointer to a structure of type ‘node’. Hence, the structure
‘node’ is a self-referential structure with ‘link’ as the referencing pointer.
An important point to consider is that the pointer should be initialized properly before
accessing, as by default it contains garbage value.
Types of Self Referential Structures
1. Self Referential Structure with Single Link
2. Self Referential Structure with Multiple Links
Self Referential Structure with Single Link: These structures can have only one self-
pointer as their member. The following example will show us how to connect the objects
of a self-referential structure with the single link and access the corresponding data
members. The connection formed is shown in the following figure.
#include <stdio.h>
struct node {
int data1;
char data2;
struct node* link;
};
int main()
{
struct node ob1; // Node1
// Intialization
ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;
// Initialization
ob2.link = NULL;
ob2.data1 = 30;
ob2.data2 = 40;
Problem:
Declare a self referential structure tNode as follows:
Create 5 objects of tNode class at system stack. Link the nodes with one another in a long chain through
next pointer. Try it and test node data through first node object.
Self Referential Structure with Multiple Links: Self referential structures with multiple links
can have more than one self-pointers. Many complicated data structures can be easily
constructed using these structures. Such structures can easily connect to more than one nodes
at a time. The following example shows one such structure with more than one links.
#include <stdio.h>
struct node {
int data;
struct node* prev_link;
struct node* next_link;
};
int main()
{
struct node ob1; // Node1
// Intialization
ob1.prev_link = NULL;
ob1.next_link = NULL;
ob1.data = 10;
Data Structures and Algorithms
Lab 1
// Intialization
ob2.prev_link = NULL;
ob2.next_link = NULL;
ob2.data = 20;
// Intialization
ob3.prev_link = NULL;
ob3.next_link = NULL;
ob3.data = 30;
// Forward links
ob1.next_link = &ob2;
ob2.next_link = &ob3;
// Backward links
ob2.prev_link = &ob1;
ob3.prev_link = &ob2;