You are on page 1of 33

structures

Structure, like a record, is a collection of one or more variables, usually of different types, grouped together under a single name. A structure let us use as a unit instead of separate entities. For example employee contains empno, ename and salary.

Declaring sturcuture
A structure declaration create format that may be used to declare structure variables.
Struct <tag name> {

Datatype1 var1,var2,.varn;

...

Datatypen var1,var2,.varn; };

Declaring Structures
Does Not Reserve Space

Reserves Space struct my_example { int label; char letter; char name[20]; } mystruct ;

struct my_example { int label; char letter; char name[20]; };


/* The name "my_example" is

called a structure tag

*/

Accessing structure elements


It is possiable to access the individual members of a structure as separate entities. Structure elements can be accessed in two ways,viz
Using the period(.) Operator Using the arrow(->) operator

A structure member can be accessed by writing


Variable.member Where variable refers to the name of a structure type variable, Member refers to the name of a member with in structure The period that separates the variable name and member name called member operator

Initializing the structure


Like other variables we can initialize the structures Syntax: Storage struct tag_name var={val1,val2valn} Where val1 refers to the value of first member, val2 refers to the value of second member and so on. Ex: Struct emp e={1,krishna,5000};

Assign & compare


OPERATION emp1=emp2
Emp1==emp2

MEANING Assign emp2 to emp1 Compare all members of emp1 and emp2 and returns 1 if they are equal else 0 Return 1 if all members are not equal, other wise zero

Emp1!=emp2

Not all compilers these operations in such cases individual member can be compared using logical operators

Array of structures Nested structures Structures and functions Structure Pointers Self-Referential Structures

Unions
Unions are like structures which together a number of variables The way the two treat these variables entirely different unlike structure the memory will be allocated for the largest element in the union. All the members of the union shares this common area. They are useful for application with multiple members, where values need not be assigned to all of the members simultaneously.

Syntax
union tag_name { Data_type member1; Data_type member2; .. Data_type membern; }union_variables;

Enumeration
Enumeration is a user-defined data type. It is defined using the keyword enum and the syntax is:
enum tag_name {name_0, , name_n} ;

The tag_name is not used directly. The names in the braces are symbolic constants that take on integer values from zero through n. As an example, the statement:
enum colors { red, yellow, green } ;

creates three constants. red is assigned the value 0, yellow is assigned 1 and green is assigned 2.

Typedef & Struct


Often, typedef is used in combination with struct to declare a synonym (or an alias) for a structure: typedef struct { int label ; char letter; char name[20] ; } Some_name ; Some_name */ /* Define a structure */

/* The "alias" is /* Create a struct

Some_name mystruct ; variable */

Bit fields
A bit field is an element of a structure that is defined in terms of bits. Using a special type of struct definition, you can declare a structure element that can range from 1 to 16 bits in length. It is useful

If the storage is limited you can store several Boolean variables in one byte Certain devices transmit information encoded into bits Certain encryptions routines need to access the bits within a byte.

struct student { int stno : 3; int age : 4; int gen : 1; } bit_var; Restrictions : Address of bit field cant be taken Bit field variables cant arrayed Any code used bit-fields machine dependencies

Files
Need for a file using the console oriented IO operation two major problems 1) it is cumbersome and time consume to handle large volume of data through terminals. 2) the entire data is lost when either the computer is turned off ot the program is terminated.

Basic file Operations


Naming a file Opening a file Reading data from a file Writing data to a file Closing a file

File Function
FILE *fp,*fp1,.; FILE *fopen(name,mode) fclose(file_pointer);

Read and write Functions


fgetc(file_pointer) fputc(int c,file_pointer); fscanf(file_pointer,format string,address); Fprintf(file_pointer,format string,arguments); Fread(void *buffer,size_t numbytes, size_t, FILE *fp) Fright (void *buffer,size_t numbytes, size_t, FILE *fp)

Random access long ftell(*fp) int fseek(*fp,long offset,int whence)

Data Structures
It is the arrangement of information in memory lactations by which the accessing and processing of the data will become easy . Depending on how the data is stored in memory and retrieved from memory the data structured can be classified as Linear data structures non liner data structures

Linear data structures


For linear data structures memory will be allocated contiguously. These are STACKS QUEUES

Stack
Stack can be defied as an ordered collection of items into which items can be added or deleted at one end only. Key words of stack Top Push Pop Stack Over Flow Stack Under Flow

Stack operations\
Stack initializations Pushing the elements into the stack Pop the elements from the stack Display

Queues
Queue is an ordered collection of items, The items may be Deleted from one end called front end one end of the queue The items may be inserted from another end called rear end of the queue The queues are two types linear Queue circler Queue

Key words in queue


Push Pop Front Rear Queue over flow Queue under flow

Queue operations
Initialize the queue Pushing the elements Pop the elements Display

Non Linear data structures


For linear data structures memory will be allocated randomly.(Free store) These are Linked lists Trees

Linked lists
Linked lists is a sequence of data elements in which each element in the sequence point to int successor These pointers provide the links that codes the list together Only the way to access an element in the middle of the list is to start at the beginning of the list,element by element until you reach the target element

Different type of linked lists


Single linked lists Double linked lists Circular linked lists

TREES
DEFINATION :A tree is a non-linear data structure, in which items are arranged in sorted sequence. It is represented in hierarchical relationship existing among several data items. It has a special data item called root of a tree. The remaining data items are partitioned into number of mutual exclusive subsets, each of which is itself a tree. They called subtrees. KEY TERMS IN TREES :ROOT :- It is specially designed data item in a tree. It is the first in the hierarchical arrangement of data items. NODE :- Each data item in tree is called a node. It is the basic data structure in tree. It specifies the information and link. DEGREE OF A NODE :- It is number of subtrees of a node in a tree.

TERMINAL NODE:- A node with degree is called terminal or leaf node. NON-TERMINAL NODE:- A node whose degree is not zero called non terminal node. Non-terminal nodes are intermediate nodes in the traversing the tree from its root node to the terminal node. SIBLINGS :- The children nodes of a given parent node is called Siblings. LEVEL:- The tree leveled in such a way that the root is always zero. The immediate children are at level 1. And goon If a node is at level n, then its children will be at level n+1. EDGE :- Connecting line of two nodes. That is, the line drawn from one node to another node is called an edge. PATH:- It is sequence of consecutive edge from source node to destination node.

DEPTH :- It is depth of maximum level of any node in a tree. FOREST :- It is set of disjoint trees, if you remove its root node its became the forest.

BINARY TREE:A binary tree is finite set of data items which consists single item called root and two disjoint binary tree is called Left subtree & Right subtree. In a binary the maximum degree of any node is at most two. There may zero degree or one or two. STRICTLY BINARY TREE:- If every non terminal node in a binary consists of non empty left subtree and right subtree such a tree called strictly binary tree.

C Type Examples
int i; int *j, k; unsigned char *ch; float f[10]; char nextChar(int, char*); int a[3][5][10]; int *func1(float);

int (*func2)(void);

You might also like