Study like there’s no tomorrow because if you keep putting off your studies for tomorrow, you’ll probably be too late.
--Your friends at LectureNotes

Note for Data Structure using C - DS By Dilawez Alam

  • Data Structure using C - DS
  • Note
  • Guru govind Singh indraprastha university - GGSIPU
  • Electrical and Electronics Engineering
  • 4 Topics
  • Uploaded 1 year ago
Dilawez Alam
Dilawez Alam
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

DATA STRUCTURE DETAILED NOTES FOR END TERM DATA STRUCTURE : - A data structure defines a way of organizing all data items that considers not only the elements stored but also their relationship to each other. The term data structure is used to describe the way data is stored. For e.g. contact list on our mobile phones A data structure is said to be linear if its elements form a sequence or a linear list. The linear data structures like an array, stacks, queues and linked lists organize data in linear order. A data structure is said to be non linear if its elements form a hierarchical classification where, data items appear at various levels e.g. trees like heap , b-tree, avl tree etc Primitive Data Structures are the basic data structures that directly operate upon the machine instructions. They have different representations on different computers. Integers, floating point numbers, character constants, string constants and pointers come under this category. Array Linked List Define Array is a collection of elements having same data type with common name. Linked list is an ordered collection of elements which are connected by li Access In array, elements can be accessed using index/subscript value, i.e. elements can be randomly accessed like arr[0], arr[3], etc. So array provides fast and random access. In linked list, elements can’t be accessed randomly but can be accessed only sequentiallyand accessing element takes 0(n) time. In array, elements are stored in consecutive manner in memory. In linked list, elements can be stored at any available place as address of node is stored in previous node. Insertion & Deletion Insertion & deletion takes more time in array as elements are stored in consecutive memory locations. Insertion & deletion are fast & easy in linked list as only value of pointer is needed to change. Memory Allocation In array, memory is allocated at compile time i.e. Static Memory Allocation. In linked list, memory is allocated at run time i.e . Dynamic Memory Allocation. Types Array can be single dimensional, two dimension or multidimensional. Linked list can be singly, doubly or circular linked list. Dependency In array, each element is independent, no connection with previous element or with its location. In Linked list, location or address of elements is stored in the link part of previous element/node. Extra Space In array, no pointers are used like linked list so no need of extra space in memory for pointer. In linked list, adjacency between the elements are maintained using pointers or links, so pointers are used and for that extra memory space is needed. Memory Structure

Text from page-2

Non-primitive data structures are more complicated data structures and are derived from primitive data structures. They emphasize on grouping same or different data items with relationship between each data item. Arrays, linked lists, trees and files come under this category Abstract Data Type (ADT): . An abstract data type in a theoreticalconstruct that consists of data as well as the operations to be performed on the data while hiding implementation. For example, a stack is a typical abstract data type. Items stored in a stack can only be added and removed in certain order – the last item added is the first item removed. We call these operations, pushing and popping. Time Complexity:The time needed by an algorithm expressed as a function of the size of a problem is called the TIME COMPLEXITY of the algorithm. The time complexity of a program is the amount of computer time it needs to run to completion Space Complexity:The space complexity of a program is the amount of memory it needs to run to completion Array advantages 1. Simple and easy to use 2. Any element can be accessed by using its index “I” . 3.Searching is easy Disadvantages :- 1.Array is declared at compile time so its size once declared remains fixed throughout the program. 2. Insertion and deletion in array is difficult as all the elements are needed to be shifted. Linked list advantages :- a. Linked lists are dynamic data structures. i.e., they can grow or shrink during the execution of a program. b. Linked lists have efficient memory utilization. Here, memory is not pre-allocated. Memory is allocated whenever it is required and it is de-allocated (removed) when it is no longer needed. c. Insertion and Deletions are easier and efficient. Linked lists provide flexibility in inserting a data item at a specified position and deletion of the data item from the given position. d. Many complex applications can be easily carried out with linked lists.

Text from page-3

Disadvantages of linked lists: 1. It consumes more space because every node requires a additional pointer to store address of the next node. 2. Searching a particular element in list is difficult and also time consuming. Types of Linked Lists: Basically we can put linked lists into the following four items: a. Single Linked List. b. Double Linked List. c. Circular Linked List. d. Circular Double Linked List. A single linked list is one in which all nodes are linked together in some sequential manner. Hence, it is also called as linear linked list. A double linked list is one in which all nodes are linked together by multiple links which helps in accessing both the successor node (next node) and predecessor node (previous node) from any arbitrary node within the list A circular linked list is one, which has no beginning and no end. A single linked list can be made a circular linked list by simply storing address of the very first node in the link field of the last node. A circular double linked list is one, which has both the successor(next) pointer and predecessor (previous) pointer in the circular manner.

Text from page-4

malloc() is a system function which allocates a block of memory in the "heap" and returns a pointer to the new block. It is used to allocate memory at RUNTIME free() is the opposite of malloc(), which de-allocates memory Calloc () is also similar to malloc but it initializes every element to a value 0, Which is the only difference. The basic operations in a single linked list are: 1. Creation. 2. Insertion. 3. Deletion. 4. Traversing Creating a node for Single Linked List:Creating a singly linked list starts with creating a node. The information is stored in the memory, allocated by using the malloc() function. node* getnode() { node* newnode; newnode = (node *) malloc(sizeof(node)); printf("\n Enter data: "); scanf("%d", &newnode -> data); newnode -> next = NULL; return newnode; } The function getnode(), is used for creating a node, after allocating memory for the structure of type node, the information for the item (i.e., data)

Lecture Notes