Role of Data Structure in Programming Language: I Abstract Data Type
Role of Data Structure in Programming Language: I Abstract Data Type
Abstract— This document provides the class of The Basic data structures which will discuss in the
models (information structure models) for paper will be as follows with their implementation in
characterizing computations in terms of the data programming language:
structures to which they give rise during execution, Stacks
shows how such models can be used to characterize Queues
programming languages, considers in some detail Linked list
the data structures generated during the execution Binary Trees
of programs in C/C++ languages, indicates how Graphs
information structure models may be used in the
semantic definition and formal characterization of
programming languages. ABSTRACT DATA TYPE
INTRODUCTION
An abstract data type is a data type that is organized in
such a way that the specification of the values and the
A data structure is an arrangement of data in a
specification of the operations on those values are
computer's memory or even disk storage. An example
separated from the representation of the values and the
of several common data structures are arrays, linked
implementation of the operations.
lists, queues, stacks, binary trees, and hash tables.
Algorithms, on the other hand, are used to manipulate
For example, consider list abstract data type. A list is
the data contained in these data structures as in
basically a collection of elements which can be ordered
searching and sorting.
or unordered. The primitive operations on a list may
include adding new elements, deleting elements,
Many algorithms apply directly to a specific data
determining number of elements in the list, and
structures. When working with certain data structures
applying a particular process to each element in a list.
we need to know how to insert new data, search for a
Here, we are not concerned with how a list is
specified item, and deleting a specific item.
represented and how the above mentioned operations
are implemented. We only need to know that it is a list
Commonly used algorithms include are useful for:
whose elements are of a given type, and what we can do
with the list.
Searching for a particular data item (or record).
Sorting the data. There are many ways to sort
There are several eg. of Abstract data types and stack
data.
and queues are common among those.
Traversing through all the items in a data
The basic operation of stack and queue are as shown
structure. (Visiting each item in turn so as to
below.
display it or perform some other action on these
items)
STACK PUSH AND POP TOP
1
2
3 where type is a valid type (like int, float...), name is a
valid identifier and the elements field (which is always
QUEUE ENQUEUE AND DEQUEUE enclosed in square brackets []), specifies how many of
3 2 1 these elements the array has to contain.
FRONT REAR
Therefore, in order to declare an array called billy as
Note that each of above ADTs has two parts the one shown in the above diagram it is as simple as:
(a) a particular data structure, and
(b) operations applied on the data structure. int billy [5];
We call these data type abstract because we have said
nothing about how they are implemented.
ROLE OF ARRAY IN DATA STRUCTURE
ARRAY
Arrays are used to implement mathematical vectors and
matrices, as well as other kinds of rectangular tables.
An array is a series of elements of the same type placed
Many databases, small and large, consist of (or include)
in contiguous memory locations that can be individually
one-dimensional arrays whose elements are records.
referenced by adding an index to a unique identifier.
Arrays are used to implement other data structures, such
That means that, for example, we can store 5 values of
as heaps, hash tables, deques, queues, stacks, strings,
type int in an array without having to declare 5
and VLists.
different variables, each one with a different identifier.
Instead of that, using an array we can store 5 different
One or more large arrays are sometimes used to emulate
values of the same type, int for example, with a unique
in-program dynamic memory allocation, particularly
identifier.
memory pool allocation.
For example, an array to contain 5 integer values of
Arrays can be used to determine partial or complete
type int called billy could be represented like this:
control flow in programs, as a compact alternative to
(otherwise repetitive), multiple IF statements. They are
known in this context as control tables and are used in
conjunction with a purpose built interpreter whose
control flow is altered according to values contained in
the array. The array may contain subroutine pointers (or
where each blank panel represents an element of the
relative subroutine numbers that can be acted upon by
array, that in this case are integer values of type int.
SWITCH statements) - that direct the path of the
These elements are numbered from 0 to 4 since in execution.
arrays the first index is always 0, independently of its
length.
MULTIDIMENSIONAL ARRAY
Like a regular variable, an array must be declared
Multidimensional arrays can be described as "arrays of
before it is used.
arrays". For example, a bidimensional array can be
A declaration for an array in C++ is: imagined as a bidimensional table made of elements, all
of them of a same uniform data type.
#define WIDTH 5
jimmy represents a bidimensional array of 3 per 5 #define HEIGHT 3
elements of type int. The way to declare this array in
C++ would be: int jimmy [HEIGHT][WIDTH];
int n,m;
int jimmy [3][5];
int main ()
{
and, for example, the way to reference the second for (n=0;n<HEIGHT;n++)
element vertically and fourth horizontally in an for (m=0;m<WIDTH;m++)
expression would be: {
jimmy[n][m]=(n+1)*(m+1);
jimmy[1][3] }
return 0;
}
There are a pile of 6 Books on a Table. We want to add ROLE OF QUEUE IN DATA STRUCTURE
one book to the pile. We simply add the book on the
TOP of the pile. What if we want the third book from
the new 7 book pile? We then lift each book one Queues provide services in algorithm, computer
by one from the TOP until the third book reaches the science, transport, and operations research where
top. Then we take the third book and replace all the various entities such as data, objects, persons, or events
others back into the pile by adding them from the TOP. are stored and held to be processed later. In these
TOP is the most important word as far as stacks are contexts, the queue performs the function of a buffer.
concerned. Data is stored in a Stack where adding of
data is permitted only from the top. Removing/Deleting Queues are common in computer programs, where they
Data is also done from the top. are implemented as data structures coupled with access
routines, as an abstract data structure or in object-
ROLE OF STACK IN DATA STRUCTURE oriented languages as classes. Common
implementations are circular buffers and linked lists.
Stacks are used on every Processor. Each processor has
a stack where data and addresses are pushed or added to
the stack. The ESP Register adds as a Stack Pointer that
refers to the top of the stack in the Processor. Adding STRUCTURE
Data to the
Stack is known as Pushing and deleting data from the
stack is known as Popping. Item
struct linked_list
{
float age;
struct linked_list *next; during execution of the program. A linked list does not
} require any extra space therefore it does not waste extra
struct Linked_list node1,node2; memory. It provides flexibility in rearranging the items
efficiently.
node1.age
PROGRAM
#include<stdio.h>
#include<malloc.h>
node1.next
struct node
{
int item;
struct node *next;
node2
}*start=NULL;
void main()
node2.age
{
int ch,n;
clrscr();
node2.next while(1)
{
printf("n1. Create List");
printf("n2. Display the list");
The next pointer of node1 can be made to point to the printf("nEnter any choice:");
node 2 by the same statement. scanf("%d",&ch);
node1.next=&node2; getch();
switch(ch)
{
case 1:{scanf("%d",&n);
This statement stores the address of node 2 into the field create_list(n);break;}
node1.next and this establishes a link between node1 case 2:{display();break;}
and node2 similarly we can combine the process to default:{printf("Wrong choice");return;}
create a special pointer value called null that can be }; /*end of switch*/
stored in the next field of the last node } /*end of while*/
create_list(int data)
A linked list is a dynamic data structure and therefore
{
the size of the linked list can grow or shrink in size
struct node *q,*tmp; tree. The formal recursive definition is: a binary tree is
tmp=malloc(sizeof(struct node)); either empty (represented by a null pointer), or is made
tmp->item=data; of a single node, where the left and right pointers
tmp->next=NULL; (recursive definition ahead) each point to a binary tree.
if(start==NULL) /*if list is empty*/
start=tmp;
else
{ /*element inserted at the end*/
q=start;
while(q->next=NULL)
{
q=q->next;
q->next=tmp;
} /*end of while*/
} /* end of else*/
} /*end of create_list()*/
display()
{
struct node *q;
if(start==NULL)
{ A "binary search tree" (BST) or "ordered binary tree" is
printf("List is empty"); a type of binary tree where the nodes are arranged in
return; order: for each node, all elements in its left subtree are
} /*end of if()*/ less-or-equal to the node (<=), and all the elements in its
q=start; right subtree are greater than the node (>). The tree
printf("nnList is :n"); shown above is a binary search tree -- the "root" node is
while(q=NULL) a 5, and its left subtree nodes (1, 3, 4) are <= 5, and its
{ right subtree nodes (6, 9) are > 5. Recursively, each of
printf("t%d",q->item); the subtrees must also obey the binary search tree
q=q->next; constraint: in the (1, 3, 4) subtree, the 3 is the root, the 1
} /*end of while()*/ <= 3 and 4 > 3. Watch out for the exact wording in the
problems -- a "binary search tree" is different from a
} /*end of display()*/ "binary tree".
.
A binary tree is made of nodes, where each node
contains a "left" pointer, a "right" pointer, and a data Insert()
element. The "root" pointer points to the topmost node Insert() -- given a binary search tree and a number,
in the tree. The left and right pointers recursively point insert a new node with the given number into the tree in
to smaller "sub trees" on either side. A null pointer the correct place. The insert() code is similar to
represents a binary tree with no elements -- the empty lookup(), but with the complication that it modifies the
tree structure. As described above, insert() returns the
new tree pointer to use to its caller. Calling insert() with
the number 5 on this tree...
GRAPH
2
/ \
1 10
A graph is an abstract data structure that is meant to
returns the tree... implement the graph concept from mathematics.
/*
Helper function that allocates a new node
with the given data and NULL left and right
pointers.
*/
struct node* NewNode(int data) {
struct node* node = new(struct node); // "new" is
like "malloc"
node->data = data;
node->left = NULL;
node->right = NULL;
REPRESENTATION
return(node);
} Different data structures for the representation of graphs
are used in practice:
FURTHER READING:
http://www.haskell.org/haskellwiki/Research_papers/Da
ta_structures#Lists
http://portal.acm.org/citation.cfm?id=1115882
Using Pass by Reference for Swapping in C.
REFERENCES:
http://www.cplusplus.com/doc/tutorial/arrays/
http://www.dreamincode.net/forums/topic/10157-data-
structures-in-c-tutorial/
http://www.suite101.com/content/introduction-to-
arrays-a25228
http://www.suite101.com/content/introduction-to-
pointers-a25224