Engineering Forum

India Education

Engineering Colleges Forum

Pointers part 2

This is a discussion on Pointers part 2 within the Computer engineering forums, part of the ENGINEERING WORLD category; Dynamic Arrays The mechanisms described above to create and destroy dynamic variables of type "int", "char", "float", etc. can also ...


Go Back   Engineering Forum > ENGINEERING WORLD > Computer engineering

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

   

Reply

 

Thread Tools Display Modes
  #1 (permalink)  
Old 08-28-2008, 07:33 PM
aayush_005's Avatar
Administrator
 
Join Date: Aug 2008
Posts: 225
Default Pointers part 2

Dynamic Arrays

The mechanisms described above to create and destroy dynamic variables of type "int", "char", "float", etc. can also be applied to create and destroy dynamic arrays. This can be especially useful since arrays sometimes require large amounts of memory. A dynamic array of 10 integers can be declared as follows:
int *number_ptr;
number_ptr = new int[10];

As we have seen, array variables are really pointer variables, so we can now refer to the 10 integer variables in the array either as
number_ptr[0] number_ptr[1] ... number_ptr[9]

or as
*number_ptr *(number_ptr + 1) ... *(number_ptr + 9)

To destroy the dynamic array, we write
delete [] number_ptr;

The "[]" brackets are important. They signal the program to destroy all 10 variables, not just the first. To illustrate the use of dynamic arrays, here is a program fragment that prompts the user for a list of integers, and then prints the average on the screen:
...
...
int no_of_integers, *number_ptr;

cout << "Enter number of integers in the list: ";
cin >> no_of_integers;

number_ptr = new int[no_of_integers];
if (number_ptr == NULL)
{
cout << "Sorry, ran out of memory.\n";
exit(1);
}

cout << "type in " << no_of_integers;
cout << " integers separated by spaces:\n";
for (int count = 0 ; count < no_of_integers ; count++)
cin >> number_ptr[count];
cout << "Average: " << average(number_ptr,no_of_integers);

delete [] number_ptr;
...
...
Fragment of Program 7.3.1

Dynamic arrays can be passed as function parameters just like ordinary arrays, so we can simply use the definition of the function "average()" from the previous lecture (Section 6.2) with this program.
(BACK TO COURSE CONTENTS)



7.4 Automatic and Dynamic Variables

Although dynamic variables can sometimes be a useful device, the need to use them can often be minimised by designing a well structured program, and by the use of functional abstraction. Most of the variables we have been using in the previous lectures have been automatic variables. That is to say, they are automatically created in the block or function in which they are declared, and automatically destroyed at the end of the block, or when the call to the function terminates. So, for a well structured program, much of the time we don't even have to think about adding code to create and destroy variables.

(N.B. It is also possible to declare variables as being static , i.e. remaining in existence throughout the subsequent execution of the program, but in a well designed, non-object based program it should not be necessary to use any static variables other than the constants declared at the beginning.)
(BACK TO COURSE CONTENTS)



7.5 Linked Lists

In this section a brief description is given of an abstract data type (ADT) called a linked list, which is of interest here because it is implemented using pointers. You will learn much more about abstract data types in general later in the course.

In the implementation given below, a linked list consists of a series of nodes, each containing some data. Each node also contains a pointer pointing to the next node in the list. There is an additional separate pointer which points to the first node, and the pointer in the last node simply points to "NULL". The advantage of linked lists over (for example) arrays is that individual nodes can be added or deleted dynamically, at the beginning, at the end, or in the middle of the list.



In our example, we will describe how to implement a linked list in which the data at each node is a single word (i.e. string of characters). The first task is to define a node. To do this, we can associate a string with a pointer using a structure definition:
struct Node
{
char word[MAX_WORD_LENGTH];
Node *ptr_to_next_node;
};

or alternatively
struct Node;
typedef Node *Node_ptr;

struct Node
{
char word[MAX_WORD_LENGTH];
Node_ptr ptr_to_next_node;
};

(Note the semicolon after the "}".) The word "struct" is a reserved word in C++ (analogous to the notion of a record in Pascal). In the first line of the alternative (second) definition of a node above, "Node" is given an empty definition. This is a bit like a function declaration - it signals an intention to define "Node" in detail later, and in the mean time allows the identifier "Node" to be used in the second "typedef" statement.
(BACK TO COURSE CONTENTS)



The "." and "->" Operators

Having defined the structure "Node", we can declare variables of this new type in the usual way:
Node my_node, my_next_node;

The values of the (two) individual components of "my_node" can be accessed and assigned using the dot "." operator:
cin >> my_node.word;
my_node.ptr_to_next_node = &my_next_node;

In the case that pointers to nodes have been declared and assigned to nodes as follows:
Node_ptr my_node_ptr, another_node_ptr;
my_node_ptr = new Node;
another_node_ptr = new Node;

we can either use dot notation for these types of statement:
cin >> (*my_node_ptr).word;
(*my_node_ptr).ptr_to_next_node = another_node_ptr;

or write equivalent statements using the "->" operator:
cin >> my_node_ptr->word;
my_node_ptr->ptr_to_next_node = &my_next_node;

In other words, "my_node_ptr->word" simply means "(*my_node_ptr).word".
(BACK TO COURSE CONTENTS)



Creating a Linked List

Below is a function which allows a linked list to be typed in at the keyboard one string at a time, and which sets the node pointer "a_list" to point to the head (i.e. first node) of the new list. Typing a full-stop signals that the previous string was the end of the list.
void assign_list(Node_ptr &a_list)
{
Node_ptr current_node, last_node;

assign_new_node(a_list);
cout << "Enter first word (or '.' to end list): ";
cin >> a_list->word;
if (!strcmp(".",a_list->word))
{
delete a_list;
a_list = NULL;
}
current_node = a_list; /* LINE 13 */

while (current_node != NULL)
{
assign_new_node(last_node);
cout << "Enter next word (or '.' to end list): ";
cin >> last_node->word;
if (!strcmp(".",last_node->word))
{
delete last_node;
last_node = NULL;
}
current_node->ptr_to_next_node = last_node;
current_node = last_node;
}
}
Fragment of Program 7.5.1

We will assume that the function "assign_new_node(...)" used in the above definition is exactly analogous to the function "assign_new_int(...)" in Program 7.1.5.

Here's how the function "assign_list(...)" works in words and diagrams. After the line
assign_new_node(a_list);

The state of the program is:



Assuming the user now types in "my" at the keyboard, after line 13 the program state is:



After the first line inside the "while" loop, the program state is:



Assuming the user now types in "list" at the keyboard, after the "while" loop has finished executing for the first time the situation is:



After the first line in the second time around the "while" loop, we have:



Assuming the user now types in "." at the keyboard, after the "while" loop has finished executing for the second time the situation is:



Since the condition for entering the "while" loop no longer holds, the function exits, the temporary pointer variables "current_node" and "last_node" (which were declared inside the function body) are automatically deleted, and we are left with:



(BACK TO COURSE CONTENTS)



Printing a Linked List

Printing our linked lists is straightforward. The following function displays the strings in the list one after another, separated by blank spaces:
void print_list(Node_ptr a_list)
{
while (a_list != NULL)
{
cout << a_list->word << " ";
a_list = a_list->ptr_to_next_node;
}
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-31-2009, 04:16 PM
Junior Member
 
Join Date: May 2009
Location: 1
Posts: 2
Send a message via ICQ to GodVIAGRA
Default VIAGRA $1.15 per pill BUYING VIAGRA samples before BUYING


ONLINE VIAGRA BUYING
ONLINE VIAGRA BUYING - BUY VIAGRA in mexico
BUYING VIAGRA in uk
PURCHASE VIAGRA $1.15 per pill | ACHAT VIAGRA eur 0.90 par comprime >>> ACHAT VIAGRA CLIQUEZ ICI <<<
BUY VIAGRA in mexico
BUYING VIAGRA IN CANADA - BUY VIAGRA from canadian
VIAGRA ONLINE BUYING
Forums - Voir le profil: BuyingViagra
BUYING VIAGRA in mexico
__________________
_________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off
Forum Jump


All times are GMT. The time now is 02:10 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.