0% found this document useful (0 votes)
9 views6 pages

Data Structures in C

Uploaded by

iamravihere05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views6 pages

Data Structures in C

Uploaded by

iamravihere05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Data Structures: a named location that can be used to store and organize data

Array: a collection of elements stored at contigous memory locations

Algorithm: collection of steps to solve the problem.

Linked List:
Linear DS
elements can stored as per memory availability
can access elements on linear
store homogenous elements(similar)
dynamic in size
easy insertion and deletion
starting element or node is the key termed as head
If head node is lost, linked list is lost
allocate memory with malloc function

Stack:
linear ds
follows LIFO (Last in first out)
only top elements is available to accessed
insertion and deletion takes place from top
major opr:
push()- used insert element
pop() - removes the top element from stack
isEmpty()-returns true is stack empty
peek()-to get the top element of stack
all opr are works in const time eg: O(1)

Demonstration of stack:
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

//skeleton of stack
strctStack{
int top;
unsigned size;
int*arr;
//create stack
structStack*create(unsigned size)
{
structStack*stack=(structStack*)malloc(sizeof(structStack));
stack->size=size;
stack->top=-1
stack->arr=(int*)malloc(stack->size*sizeof(int));
returnstack;
}

//stack is full when top is equal to last index

int isFull(structStack*stack)
{
return stack->top==stack->size -1
}

//empty when top is -1


int isEmpty(struct Stack*stack)
{
return stack->top==-1
}

//to add item in stack

void push(struct Stack*stack, int item)


{
if (isFull(stack))
return;
stack->arr[++stack->top]=item;
}

//to remove item from stack

int pop(struct Stack*stack)


{
if(isEmpty(stack))
return -1;
return stack->arr[stack->top--];
}

//to get the top element

int peek(struct Stack*stack)


{
if(isEmpty(stack))
return
INT_MIN;
return stack->arr[stack->top]

Queue:

linear ds
follows FIFO: First in first out
insertion can takes places from rear end
deletion takes place from front end
major opr:
enqueue(ele)-used to insert element at top
dequeue()-removes top element
peekfirst()-to get the first element of queue
peeklast()-to get the last element of queue
all opr works in cost time eg: o(1)

DEMONSTRATION OF QUEUE:

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

//skeleton of queue

structQueue
{
int front,rear,size;
unsigned capacity;
int* array;
}
//to create a queue

structQueue*createQueue(unsigned capacity)
{
struct Queue*queue=(struct Queue*)malloc(sizeof(structQueue);
queue->capacity=capacity
queue->front=queue->size=0
queue->rear=capacity-1
queue->array=(int*)malloc(queue->capacity*sizeof(int));
return queue;
}

//queue is full when size equal to capacity

int isFull(struct Queue*queue)


{return(queue->size=queue->capacity);}

//to add an item

void enqueue(struct Queue*queue,int item)


{
if isFull(queue))
return;
queue->rear=(queue->rear +1)%queue->capacity;
queue->array[queue->rear]=item
queue->size=queue->size+1
printf("%d enqueued to queue\n",item);
}

//empty when size equal to zero

int isEmpty(struct Queue*queue)


{return(queue->size==0);}

//to remove an item

int dequeue(struct Queue*queue)


{
if (isEmpty(queue))
return INT_MIN;
int item=queue->array[queue->front];
queue->front=(queue->front+1)%queue->capacity
queue->size=queue->size-1
return item;
}

//to get first element


int front(struct Queue*queue)
{
if(isEmpty(queue))
return INT_MIN;
return(queue->array[queue->front]);
}

//to get last element


int rear(struct Queue*queue)
{
if(isEmpty(queue))
return INT_MIN;
return(queue->array[queue->rear]);
}

Binary Tree:

hierarchical ds
topmost element is known as the root of tree
every node can have at most 2 children in tree
can access elements randomly using index
common traversal methods:
preorder(root):print-left-right
postorder(root):left-right-print
inorder(root):left-print-right

DEMONSTRATION OF BINARY TREE:

#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

//skeleton of tree

struct node
{
int data;
struct node*left;
struct node*right;
}

//to create tree


struct node*create(int data)
{
//allocate memory

struct node*node=(struct node*)malloc(sizeof(struct node));


//assign data
node->data=data

//initialize left and right children NULL


node->left=NULL
node->right=NULL
return (node);
}

//to postorder the tree

void postorder(struct node*node)


{
if(node==NULL)
return;
postorder(node->left);
postorder(node->right);
printf("%d",node->data);
}

//to preorder the tree


void preorder(struct node*node)
{
if(node==NULL)
return;
printf("%d",node->data);
pretorder(node->left);
pretorder(node->right);
}

//to inorder the tree


void inorder(struct node*node)
{
if(node==NULL)
return;
inorder(node->left);
printf("%d",node->data);
inorder(node->right);
}

Binary Search Tree:

binary tree with additional restriction


restrictions:
left child must always less than root node
right child must always greater than root node
insertion,deletion,search is much more efficient than binay tree
Inorder traversal gives sorted elements

DEMONSTRATION OF BST:

#include<stdio.h>
#include<stdlib.h>

struct node
{
int key;
struct node *left, *right;
}

//to create bst

struct node*create(int item)


{
struct node*temp=(struct node*)malloc(sizeof(struct node));
temp->key=item;
temp->left=temp->right=NULL;
return temp;
}

void inorder(struct node*root)


{
if(root!=NULL)
{
inorder(root->left);
printf("%d\n",root->key);
inorder(root->right);
}

//to insert a node in bst

struct node*insert(struct node*node,int key)


{
if (node == NULL)
return create(key)
if(key < node->key)
node->left=insert(node->left,key);
else if(key > node-> key)
node->right=insert(node->right,key);
return node;
}

Heap:

binary heap can be visualize array into complete binary tree


arr[0] is treated as root
length(arr)-size of array
heapsize(arr)-size of heap
we are dealing with min and max elements
for ith node:
(i-1)/2 - Parent
(2*i)+1 - Left Child
(2*i)+2 - Right Child
time complexity for insertion and deletion is log(n)

Hash:
uses special hash function
hash function maps element to a address for storage
this provide const time access
collision is handled by collision resolution techniques
Collision - try to map more than one element in particular location
collision resolution technique:
chaining
open addressing

You might also like