diff --git a/data-structures/C/Algorithms/binary-search.c b/data-structures/C/Algorithms/binary-search.c new file mode 100644 index 0000000..921ccc2 --- /dev/null +++ b/data-structures/C/Algorithms/binary-search.c @@ -0,0 +1,36 @@ +#include +//#include + +void main() { + int query,i,halfIndex,fullIndex; + int arr[6] = {11,23,35,56,67,78}; + + //clrscr(); + printf("\n Enter element to search:"); + scanf("%d",&query); + + halfIndex = sizeof(arr)/2; + fullIndex = sizeof(arr)-1; + + if (query == arr[halfIndex]){ + printf("\n Element at %d \n",halfIndex); + } + else if (query < arr[halfIndex]){ + for (i=0;i arr[halfIndex]){ + for (i=arr[halfIndex];i< fullIndex;i++){ + if (query == arr[i]){ + printf("\n Element at %d \n",i); + } + } + } + else { + printf("Element not in array"); + } + //getch(); +} \ No newline at end of file diff --git a/data-structures/C/Algorithms/linear-search.c b/data-structures/C/Algorithms/linear-search.c new file mode 100644 index 0000000..8390347 --- /dev/null +++ b/data-structures/C/Algorithms/linear-search.c @@ -0,0 +1,27 @@ +#include +//#include + +void main() { + int query,i,finalIndex,found=0; + int arr[5] = {11,66,44,22,55}; + //clrscr(); + + printf("Program for linear search\n"); + printf("Enter element to search:"); + scanf("%d",&query); + + finalIndex = sizeof(arr) - 1; + + for (i=0;i<=finalIndex;i++){ //checks for query at every iteration + if (arr[i]==query){ + printf("Element at %d index\n",i); + found = 1; + break; + } + } + + if (found == 0){ + printf("Element not in list\n"); + } + //getch(); +} \ No newline at end of file diff --git a/data-structures/C/LinkedList/link b/data-structures/C/LinkedList/link new file mode 100755 index 0000000..12ab04a Binary files /dev/null and b/data-structures/C/LinkedList/link differ diff --git a/data-structures/C/LinkedList/linked-list-insertion.c b/data-structures/C/LinkedList/linked-list-insertion.c new file mode 100644 index 0000000..799b064 --- /dev/null +++ b/data-structures/C/LinkedList/linked-list-insertion.c @@ -0,0 +1,30 @@ +#include +#include +//#include + +struct node { + int data; + struct node *next; +}; + +void main(){ + int opt,value; + struct node *head = NULL, *last = NULL; + //clrscr(); + + printf("PROGRAM FOR LINKED LIST ELEMENT INSERTION\n"); + printf("1.Add to front\n"); + printf("2.Add to rear\n"); + printf("3.Add to a specific location\n"); + printf("Enter your option"); + scanf("%d",&opt); + + switch(opt){ + case(1): + if (*head == NULL){ + + } + printf("\nEnter Value:"); + scanf("%d",&value); + } +} \ No newline at end of file diff --git a/data-structures/C/LinkedList/linked-list.c b/data-structures/C/LinkedList/linked-list.c new file mode 100644 index 0000000..6636b4b --- /dev/null +++ b/data-structures/C/LinkedList/linked-list.c @@ -0,0 +1,58 @@ +#include +//#include +#include + +struct node { + int data; + struct node *next; +}; + +void main() { + struct node *head=NULL, *tail=NULL; + int opt,opt2,value; + //clrscr(); + + printf("\nPROGRAM FOR LINKED LIST\n"); + printf("1.Show linked list\n"); + printf("2.Insert new node\n"); + printf("3.Delete last node\n"); + printf("ENTER YOUR OPTION:"); + scanf("%d",&opt); + + switch(opt){ + case(1): + break; + + case(2): + + printf("\nInsert At:"); + printf("\n1.Beginning"); + printf("\n2.End"); + printf("\n3.Specific Location"); + printf("\nEnter YOUR OPTION:"); + scanf("%d",&opt2); + + switch(opt2) { + case(1): + struct node* new_node = (struct node*)malloc(sizeof(struct node)); + printf("\nENTER VALUE:"); + scanf("%d",&value); + new_node->data = value; + new_node->next = (*head); + (*head) = new_node; + break; + + case(2): + struct node* new_node = (struct node*)malloc(sizeof(struct node)); + struct node* last = *head; + printf("\nENTER VALUE:"); + scanf("%d",&value); + new_node->data = value; + new_node->next = NULL; + tail->next = new_node; + break; + } + + } + //getch(); +} \ No newline at end of file diff --git a/data-structures/C/Matrix/sparse-martix.c b/data-structures/C/Matrix/sparse-martix.c new file mode 100644 index 0000000..a33cef5 --- /dev/null +++ b/data-structures/C/Matrix/sparse-martix.c @@ -0,0 +1,57 @@ +#include +//#include + +void main() { + int m,n,i,j,index=0; + + struct nonzero { + int row; + int column; + int element; + } nonzero[100]; //list of nonzero element as structure + + printf("\nPROGRAM FOR SPARSE MATRIX AND RETURING NON ZERO ELEMENTS WITH POSITIONS\n"); + printf("Enter number of row in matrix:"); + scanf("%d",&m); + printf("\nEnter number of columns in matrix:"); + scanf("%d",&n); + + int sparse[m][n]; //sparse matrix + + //clrscr(); + // pushing elements + for(i=0;i +#include +//#include + +void main() { + + int opt,i,item,front=0,rear=5; + int cirQueue[8] = {1,2,9,4,6,8}; + int size = 8; + + //clrscr(); + while(1) { + printf("\nPROGRAM FOR CIRCULAR QUEUE IMPLEMENTATION\n"); + printf("1. Show queue\n"); + printf("2. Push to queue\n"); + printf("3. Pop from queue\n"); + printf("4. Exit\n"); + printf("Enter your option:"); + scanf("%d",&opt); + + switch(opt){ + case(1): + + for(i=front;i<=rear;i++){ + printf("%d,",cirQueue[i]); + } + break; + + case (2): + + if(front == (rear+1)%size ) { + printf("Queue Full\n"); + break; + } + else if (front == -1){ + front = 0; + } + printf("Insert item:"); + scanf("%d",&item); + rear = (rear+1)%size; + cirQueue[rear] = item; + break; + + case (3): + + if(front==-1){ + printf("Queue Empty\n"); + } + else if(front==rear){ + front = rear = -1; + } + else { + item = cirQueue[front]; + front = (front+1)%size; + printf("\nElement %d popped out of queue",item); + } + break; + + case (4): + exit(0); + } + } + //getch(); +} \ No newline at end of file diff --git a/data-structures/C/Queue/queue.c b/data-structures/C/Queue/queue.c new file mode 100644 index 0000000..2a2c454 --- /dev/null +++ b/data-structures/C/Queue/queue.c @@ -0,0 +1,61 @@ +#include +#include +//#include + +void main() { + + int i, front=0, rear=5, opt,item; + int queue[10] = {1,2,3,4,5,6}; + //clrscr(): + + while(1) { + printf("\nPROGRAM FOR QUEUE IMPLEMENTATION\n"); + printf("1. Show Queue\n"); + printf("2. Push to Queue\n"); + printf("3. Pop from Queue\n"); + printf("4. Exit\n"); + printf("Enter your option:"); + scanf("%d",&opt); + + switch(opt){ + case (1): + + for(i=front;i<=rear;i++){ + printf("%d,",queue[i]); + } + break; + + case (2): + + if(rear == sizeof(queue) - 1){ + printf("Queue Full"); + } + else if (front == -1 ){ + front =0; + } + printf("\nEnter element to insert:"); + scanf("%d",&item); + rear += 1; + queue[rear] = item; + break; + + case (3): + if (front == -1){ + printf("Queue Empty"); + } + else if(front==rear){ + front=rear=-1; //reinitialization if queue ended somewhere in middle + } + else{ + item = queue[front]; + front++; + printf("Element %d popped from queue",item); + } + break; + + case (4): + exit(0); + } + } + //getch(); +} \ No newline at end of file diff --git a/data-structures/C/README.md b/data-structures/C/README.md new file mode 100644 index 0000000..4945bf3 --- /dev/null +++ b/data-structures/C/README.md @@ -0,0 +1,103 @@ +# DATA STRUCTURES IN C + +## Array +Array is a primitive data structure that stores a collection of similar kind(type) of data in an order(index). + +```int arr[5] = {1,23,54,64,45};``` + +This is an array with space for 5 elements and it can only store data of integer type. + +## Stack +Stack is an abstract data type based on First In Last Out i.e. the first element to be pushed to the stack will be the last to be popped. Like in a book stack, in order to get to the book at the bottom, you have to remove the books at the top. + +A stack has a **top** and **last**. + +### Insertion into Stack +At the beginning the top and last are -1 (not 0 because if last is 0 then there must be an element at 0th index). As soon as first element is inserted into stack the top and last shifts from -1 to 0, as second element is inserted the top shifts to 1, then at next push top shifts to 2 and so on. + +### Algorithm for Pushing to Stack +``` +if sizeofstack - 1 = top + Stack Overflow +endif + +top = top + 1 +stack[top] = item +Element pushed +``` + +### Deletion from Stack +Assuming we have a stack with 5 elements(last=0 and top=4). If we remove an element, the first element from top will be popped and top will be shifted to 3, and at next deletion top will be shifted to 2. When there are no more elements to pop, top and last are resetted to -1. + +### Algorithm for Popping from Stack +``` +if top = -1 + Stack Underflow +endif + +item = stack[top] +top = top - 1 +``` + +**USES**: In Recursive functions + +## Queue +Queue is also an abstract data type based on First In First Out i.e. the first element to be pushed to the queue will be the first one to be pulled out. Like in queue, the first person to get in line will be first to go out. + +A queue has **front** and **rear**. + +### Insertion into Queue +Front and rear -1 while queue is empty. As the first element is inserted into the queue the the front and rear are incremented to 0, as second element is pushed the rear is shifted to 1 and at next push rear is incremented to 2 and so on. + +### Algorithm for Pushing in Queue +``` +if rear = sizeofqueue - 1 + Queue Full +end if + +if front = -1 + front = 0 +endif + +rear = rear + 1 +queue[rear] = item +``` + +### Deletion from Queue +In queue, the deletion stats from front, assuming we have a queue with 5 elements(front=0 and rear=4). At first pop the element at front is removed and front is moved to 1 and at next pop front is moved to 2 and so on. If queue reaches at front=4 and rear=4 then front and rear are resetted to -1. + +### Algorithm for Popping from Queue +``` +if front = -1 + Queue Full +end if + +if front = rear + front = rear = -1 +endif + +else + item = queue[front] + front = front + 1 +``` + +**USES**: In I/O buffer + +## List +List is a similar data type to Array but it can store different kinds(types) of data rather than storing single kind of data. A list can have string, char, int and float in it at the same time. + +## Linked List +Linked List stores data in the form of nodes. A node has data and pointer to next node. It can be useful in situations where we have to dynamically allocate storage to data (allocate only when we get data). A linked list can store only one kind of data (not like list). + +### Implementation of Linked List using Structures +``` +struct node { + int value; + struct *node next; + } +``` + +### Inserting into Linked List +### Algorithm for inserting into Linked List +### Deleting from Linked List +### Algorithm for deleting from Linked List diff --git a/data-structures/C/Stack/stack.c b/data-structures/C/Stack/stack.c new file mode 100644 index 0000000..fe8b03b --- /dev/null +++ b/data-structures/C/Stack/stack.c @@ -0,0 +1,51 @@ +#include +//#include +#include //for importing exit(); + +void main() { + int opt,i,top=5,last=0,size,item; + int stack[10] = {1,2,3,4,6,5}; + size = sizeof(stack); + //clrscr(); + + while(1){ + printf("\nPROGRAM FOR STACK IMPLEMENTATION\n"); + printf("1. Show Stack\n"); + printf("2. Push on Stack\n"); + printf("3. Pop from Stack\n"); + printf("4. Exit\n"); + printf("Enter your option:"); + scanf("%d",&opt); + + switch(opt){ + case(1): + for (i=last;i<=top;i++){ + printf("%d,",stack[i]); + } + break; + + case(2): + if (size-1==top){ + printf("Stack Overflow"); + } + top += 1; + printf("\n Enter element to push:"); + scanf("%d",&item); + stack[top] = item; + break; + + case(3): + if (top == -1){ + printf("\n Stack Underflow \n"); + } + item = stack[top]; + top -= 1; + printf("\n %d removed from stack \n",item); + break; + + case(4): + exit(0); + } + } + //getch(); +} \ No newline at end of file