Sei sulla pagina 1di 10

Structures and Unions

Topics to be covered 1. Defining a structure. 2.Structure initialization 3.Accessing the structure members 4.Array of structures. 5.Structurs and Pointers. 6. Array within Structures 7.Structures within structures 8.uses of structures 9.Unions 10.Difference between structures and unions

Structure Definition Struct tag_name {

data_type member1; data_type member2; }structure variables;

Struct employee {

Char name[10]; Int salary; Int id; } The above definition creates a structure with tag name employee, but memory will not be allocated Struct employee cts_emp, cts1_emp; Another way to declare the variables is by using typedef in c. for example typedef struct employee EMP; EMP is userdefined data type EMP cts_emp, cts1_emp;

Structure declaration Struct { Char name[10]; Int salary; Int id; }cs_emp,is_emp;

Structure initialization The syntax for structure initialization is Struct struct_name variable = {v1,v2.vn}; Consider the structure definition for an employee with the fileds shown below Struct

{ Char name[10]; Int salary; Int id; }; The above structure can be initialized as shown below Struct employee { Char name[10]; Int salary; Int id; }a = {xxxxx,10,20};

Instead of defining along with the definition, we can initialize during declaration Struct employee a={xxxxx,10,20}; Array of structures Struct employee a[3] = { {xxxxx,10,20}, {xxxxx,10,20}, {xxxxx,10,20}; }; EMP a[3] = { {xxxxx,10,20},

{xxxxx,10,20}, {xxxxx,10,20}; };

Array within Structures Struct student { Char name[10]; Int marks[3]; }; Initialization Struct student a[] = { {xxxxx,{1,2,3}}, {xxxxx,{1,2,3}}, }; Structures within structures Struct subject { Int marks1; Int marks2; Int marks3; }; Struct student {

Char name[10]; Struct subject marks; };

Uses of structures Can be used to pass arguments to minimize the members of func When more than one data has to be returned from the func

Unions

Union tag_name { Data_type member1; Data_type member2; }variable; Union item { Int I; Double d; Char a; };

Linked List

Linked list is a data structure which is collection of zero or more nodes. Where each node has some information.

Difference between Array and Linked list

Advantages of array: Data accessing is faster Simple

Disadvantages of array: The size of array is fixed Array items are stored contiguously Insertion and deletion of elements is tedious job

Advantages of Linked List The size is not fixed Data can be stored in non contiguous blocks of memory Insertion and deletion of nodes is easier

Disadvantages of Linked List The linked list require extra space because each node in the list has a special field called link field which holds address of next node Accessing of a particular node in the list may take more time when compared with arrays. The list has to be traversed from the first node to the end of the list.

Struct node { Int info; Struct node *link; };

Typedef struct node * NODE;

Void main() {

NODE first = NULL; Int item; For(;;) { first = insert_front(item, first); } delete_front(first); }

NODE getnode() { NODE x; X = (NODE)malloc(sizeof(Struct node)); If(x == NULL)

{ printf(memory not allocated);

} return x; }

NODE insert_front(int item, NODE first) { NODE temp; Temp = getnode(); Temp->info = item; Temp->link = first; return temp; }

NODE insert_rear(int item, NODE first) { NODE temp; NODE cur; temp = getnode(); temp->info = item; temp->link = NULL; if(first == NULL)

return temp; cur = first; while(cur->link != NULL) { Cur = cur->link; } Cur->link = temp; return first; }

NODE delete_front(NODE first) { NODE temp; If(first == NULL) { Printf(list is empty); return first; } temp = first; first = first->link; freenode(temp); return first; }

Potrebbero piacerti anche