From e55d4ceb3edb0487f564a718a66283767284a747 Mon Sep 17 00:00:00 2001 From: Bhumit Rohilla Date: Wed, 28 Sep 2022 13:06:24 +0530 Subject: [PATCH] Added Implementation of DS Added implementation of basic DS in C language --- .../circluar_linkedList.c | 117 ++++++++++++++ .../Implementation_of_DS/circular_queue.c | 142 ++++++++++++++++ .../Implementation_of_DS/linked_list.c | 93 +++++++++++ .../Implementation_of_DS/queue.c | 149 +++++++++++++++++ .../Implementation_of_DS/stack.c | 151 ++++++++++++++++++ 5 files changed, 652 insertions(+) create mode 100644 Program's_Contributed_By_Contributors/Implementation_of_DS/circluar_linkedList.c create mode 100644 Program's_Contributed_By_Contributors/Implementation_of_DS/circular_queue.c create mode 100644 Program's_Contributed_By_Contributors/Implementation_of_DS/linked_list.c create mode 100644 Program's_Contributed_By_Contributors/Implementation_of_DS/queue.c create mode 100644 Program's_Contributed_By_Contributors/Implementation_of_DS/stack.c diff --git a/Program's_Contributed_By_Contributors/Implementation_of_DS/circluar_linkedList.c b/Program's_Contributed_By_Contributors/Implementation_of_DS/circluar_linkedList.c new file mode 100644 index 0000000000..5f98c885fe --- /dev/null +++ b/Program's_Contributed_By_Contributors/Implementation_of_DS/circluar_linkedList.c @@ -0,0 +1,117 @@ +#include +#include + +struct node{ + struct node *rear; + int data; + struct node *front; + +}; +struct head{ + unsigned int count; + struct node *front; +}; +void create(); +void display(); +void insertBegin(); + +int main() +{ + int choice; + struct head *head; + head=(struct head *)malloc(sizeof(struct head)); + head->count=0; + head->front=NULL; + while(1){ + printf("----------------------------------\n"); + printf("1: Create\n2: Insert\n3: Delete\n4: Noel\n5: isEmpty\n6: Display\n"); + scanf("%d", &choice); + switch(choice){ + case 1/*create*/:{ + int data; + printf("Enter data you want to give to the beginning node: "); + scanf("%d", &data); + create(head, data); + break; + } + case 2/*insert*/:{ + int choiceI; + printf("1: Insert At Beginning\n2: Insert At Pos\n3: Insert At End\n"); + scanf("%d", &choiceI); + switch(choiceI){ + case 1:{ + int data; + printf("Enter data you want to enter at beginning: "); + scanf("%d", &data); + insertBegin(head, data); + printf("Element is added\n"); + break; + } + case 2:{ + break; + } + case 3:{ + break; + } + + } + break; + } + case 3/*delete*/:{ + break; + } + case 4/*noel*/:{ + break; + } + case 5/*isempty*/:{ + break; + } + case 6/*display*/:{ + display(head); + break; + } + default :{ + //exit(0); + } + + } + } + return 0; +} + +void create (struct head *head,int data){ + if(head->count!=0){ + head->count=0; + struct node *ptr; + ptr=head->front; + do{ + ptr=ptr->front; + free(ptr->rear); + }while(head->front!=ptr); + head->front=NULL; + } + struct node * node; + node=(struct node *)malloc(sizeof(struct node)); + head->front=node; + head->count++; + node->data=data; + node->front=node; + node->rear=node; +} + +void display(struct head *head){ + struct node * ptr; + ptr=head->front; + do{ + printf("%x\t%d %x\n",ptr->rear, ptr->data, ptr->front); + }while(head->front!=ptr); +} + +void insertBegin(struct head *head,int data){ + struct node *ptr; + ptr=(struct ptr *)malloc(sizeof(struct node)); + ptr->data=data; + ptr->front=head->front; + ptr->rear=head->front->rear; + head->front=ptr; +} diff --git a/Program's_Contributed_By_Contributors/Implementation_of_DS/circular_queue.c b/Program's_Contributed_By_Contributors/Implementation_of_DS/circular_queue.c new file mode 100644 index 0000000000..2a92d1cad9 --- /dev/null +++ b/Program's_Contributed_By_Contributors/Implementation_of_DS/circular_queue.c @@ -0,0 +1,142 @@ +//Implement circular queue +#include +#include +#include + +struct Cqueue{ + int size,front,rear; + int *arr; +}; + +int createCqueue(int n); +int insert(); +int Delete(); +int Noel(); +int isEmpty(); +int isFull(); + +int main() +{ + struct Cqueue *q; + q=(struct Cqueue *)malloc(sizeof(struct Cqueue)); + int choice; + while(1){ + printf("\n1: Create\n2: Insert\n3: Delete\n4: Noel\n5: isEmpty\n6: isFull\nAny: Stop\n"); + scanf("%u", &choice); + switch(choice){ + case 1/*Create*/:{ + int n; + printf("\nEnter number of element you want in the queue:\t"); + scanf("%d", &n); + n++; + free(q); + q=createCqueue(n); + printf("\nCircular Queue is created:\n"); + break; + } + case 2/*Insert*/:{ + int element,check; + printf("\nEnter element you want to enter\n"); + scanf("%d",&element); + check=insert(q,element); + if(check==1){ + printf("\nElement is inserted\n"); + } + else{ + printf("\nCircular queue is full\n"); + } + break; + } + case 3/*Delete*/:{ + int element; + element=Delete(q); + if(element==INT_MAX){ + printf("\nQueue is Empty\n"); + } + else{ + printf("\nElement deleted:\t%d\n", element); + } + break; + } + + case 4/*Noel*/:{ + printf("\nNumber of elements:\t%d", Noel(q)); + break; + } + case 5/*isEmpty*/:{ + if(isEmpty(q)==1) + printf("\nQueue is empty\n"); + else + printf("\nQueue is full\n"); + break; + } + case 6/*isFull*/:{ + if(isFull(q)==1) + printf("\nQueue is full\n"); + else + printf("\nQueue is not full\n"); + break; + } + default/*Stop*/:{} + } + } +} + +int createCqueue(int n){ + struct Cqueue *q; + q=(struct Cqueue *)malloc(sizeof(struct Cqueue)); + q->front=0; + q->rear=0; + q->size=n; + q->arr=(int *)calloc(n, sizeof(int)); +} +int insert(struct Cqueue *q, int element){ + if(isFull(q)==1){ + return 0; + } + else{ + q->rear=((q->rear+1)%q->size); + *(q->arr+q->rear)=element; + return 1; + } +} + +int Delete(struct Cqueue *q){ + if(isEmpty(q)==1){ + return INT_MAX; + } + int element; + q->front=((q->front+1)%(q->size)); + element=*(q->arr+q->front); + return element; +} + +int Noel(struct Cqueue *q){ + int noel=0,f,r,s; + f=q->front; + r=q->rear; + s=q->size; + while(f!=r){ + noel++; + f=(f+1)%s; + } + return noel; +} + +int isEmpty(struct Cqueue *q){ + if(q->front==q->rear){ + return 1; + } + return 0; +} + +int isFull(struct Cqueue *q){ + int s=0; + if(q->rear==0 && q->front!=0){ + s=q->size; + } + if((q->rear+1)%q->size == q->front){ + return 1; + } + return 0; +} diff --git a/Program's_Contributed_By_Contributors/Implementation_of_DS/linked_list.c b/Program's_Contributed_By_Contributors/Implementation_of_DS/linked_list.c new file mode 100644 index 0000000000..4ae4caae27 --- /dev/null +++ b/Program's_Contributed_By_Contributors/Implementation_of_DS/linked_list.c @@ -0,0 +1,93 @@ +#include +#include + +struct Node{ + int data; + struct Node *next; +}; + +struct Node * insertFront(struct Node *,int); +struct Node * insertBack(struct Node *,int); +struct Node * insertAtIndex(struct Node *,int,int); +void insertAfterNode(struct Node *,int); +void print(struct Node *); +int main() +{ + struct Node *head,*second, *third,*fourth; + head=(struct Node *)malloc(sizeof(struct Node)); + second=(struct Node *)malloc(sizeof(struct Node)); + third=(struct Node *)malloc(sizeof(struct Node)); + fourth=(struct Node *)malloc(sizeof(struct Node)); + + head->data=1; + head->next=second; + + second->data=2; + second->next=third; + + third->data=3; + third->next=fourth; + + fourth->data=4; + fourth->next=NULL; + + print(head); + head=insertFront(head,5); + print(head); + head=insertBack(head, 7); + print(head); + head=insertAtIndex(head,1,12); + print(head); + insertAfterNode(fourth,5); + print(head); + return 0; +} + +struct Node * insertFront(struct Node *head,int data){ + struct Node *ptr=(struct Node *)malloc(sizeof(struct Node)); + ptr->data=data; + ptr->next=head; + return ptr; +} + +struct Node * insertBack(struct Node *head,int data){ + struct Node *ptr=(struct Node *)malloc(sizeof(struct Node)); + ptr->data=data; + struct Node *p=head; + while(p->next!=NULL){ + p=p->next; + } + p->next=ptr; + ptr->next=NULL; + return head; +} + +struct Node * insertAtIndex(struct Node *head,int position,int data){ + struct Node *ptr=(struct Node *)malloc(sizeof(struct Node)); + struct Node *p=head; + int i=0; + while(i!=position-1){ + ++i; + p=p->next; + } + ptr->next=p->next; + ptr->data=data; + p->next=ptr; + return head; +} + +void insertAfterNode(struct Node *p,int data){ + struct Node *ptr=(struct Node *)malloc(sizeof(struct Node)); + ptr->data=data; + ptr->next=p->next; + p->next=ptr; +} + +void print(struct Node *ptr){ + printf("\nData:\n"); + while(ptr->next!=NULL){ + printf("%d\n" ,ptr->data); + ptr=ptr->next; + } + printf("%d\n" ,ptr->data); +} diff --git a/Program's_Contributed_By_Contributors/Implementation_of_DS/queue.c b/Program's_Contributed_By_Contributors/Implementation_of_DS/queue.c new file mode 100644 index 0000000000..98df9a65fc --- /dev/null +++ b/Program's_Contributed_By_Contributors/Implementation_of_DS/queue.c @@ -0,0 +1,149 @@ +//Implementation of queue +#include +#include +#include +struct queue{ + int size; + int front; + int end; + int *arr; +}; + +int createQueue(); +int push(struct queue*,int); +int pop(struct queue*); +int peek(struct queue*); +int isEmpty(struct queue*); +int isFull(struct queue*); + +int main(){ + struct queue *q; + q=(struct queue *)malloc(sizeof(struct queue)); + unsigned int n,choice; + while(2){ + printf("1:Create\n2:Push\n3:Pop\n4:Peek\n5:isEmpty\n6:isFull\n"); + scanf("%u", &choice); + switch(choice){ + case 1/*CreateQueue*/:{ + printf("\nEnter the number of elements:\t"); + scanf("%u", &n); + free(q); + q=createQueue(n); + printf("\nQueue is created\n"); + break; + } + case 2/*push*/:{ + int element,check; + printf("\nEnter element you want to push:\t"); + scanf("%d", &element); + check=push(q,element); + if(check==0){ + printf("\nQueue is full\n"); + } + else + printf("\nElement is pushed\n", q->end); + break; + } + case 3/*pop*/:{ + int element,check; + element=pop(q); + if(element==INT_MAX) + printf("\nQueue is empty\n"); + else + printf("\nElement popped: %d\n", element); + + break; + } + case 4/*peek*/:{ + int element; + element=peek(q); + printf("Element %d\n", element); + break; + } + case 5/*isEmpty*/:{ + int check; + check=isEmpty(q); + if(check==0){ + printf("\nQueue is not empty\n"); + } + else{ + printf("\nQueue is empty\n"); + } + break; + } + case 6/*isFull*/:{ + int check; + check=isFull(q); + if(check==0){ + printf("\nQueue is not full\n"); + } + else{ + printf("\nQueue is full\n"); + } + break; + } + default:{ + exit(0); + } + } + if(q->front==q->end){ + q->front=-1; + q->end=-1; + printf("\nReset\n"); + } + } +} + +int createQueue(int n){ + struct queue *q; + q=(struct queue *)malloc(sizeof(struct queue)); + q->size=n; + q->front=-1; + q->end=-1; + q->arr=(int *)calloc(n, sizeof(int)); + return q; +} + +int push(struct queue *q, int element){ + if(isFull(q)==1){ + return 0; + } + else{ + q->end=q->end+1; + *(q->arr+q->end)=element; + return 1; + } +} + +int pop(struct queue *q){ + int element; + if(isEmpty(q)==1){ + return INT_MAX; + } + else{ + element=peek(q); + q->front=q->front+1; + return element; + } +} + +int peek(struct queue *q){ + int element; + element=*(q->arr+q->front+1); + return element; +} + +int isEmpty(struct queue *q){ + if(q->end==-1){ + return 1; + } + return 0; +} + +int isFull(struct queue *q){ + if(q->end==q->size-1){ + return 1; + } + return 0; + +} diff --git a/Program's_Contributed_By_Contributors/Implementation_of_DS/stack.c b/Program's_Contributed_By_Contributors/Implementation_of_DS/stack.c new file mode 100644 index 0000000000..13a18f2f7f --- /dev/null +++ b/Program's_Contributed_By_Contributors/Implementation_of_DS/stack.c @@ -0,0 +1,151 @@ +//Implementation of stack in c; +#include +#include + +struct stack{ + int size; + int top; + int *arr; +}; +int createStack(); +void push(struct stack *s, int,int *); +int pop(struct stack *, int *); +int peek(struct stack *); +int Noel(struct stack *); +int isEmpty(struct stack *); +int isFull(struct stack *); + +int main() +{ + unsigned int n,choise,*check; + check=(int*)calloc(1,sizeof(char)); + struct stack *stac; + stac=createStack(n); + while(1){ + printf("\nAny:-Exit\n1:-create\n2:-push\n3:-pop\n4:-peek\n5:-Noel\n6:-isEmpty\n7:-isFull\n"); + scanf("%d", &choise); + switch(choise){ + case 1/*create*/:{ + printf("Enter the size of the stack: "); + free(stac); + scanf("%u", &n); + stac=createStack(n); + printf("Stack of size %d is crated", n); + break; + } + case 2/*push*/:{ + *check=0; + printf("Enter the element you want to push:"); + int element; + scanf("%d",&element); + push(stac,element,check); + if(*check==1){ + printf("\n%d is pushed into stack", element); + } + else{ + printf("\nStack is full"); + } + break; + } + case 3/*pop*/:{ + *check=0; + int element= pop(stac,check); + if(*check==1){ + printf("\nElement pop: %d", element); + } + else + printf("\nStack is empty"); + break; + } + case 4/*peek*/:{ + printf("%d is on top of the stack", peek(stac)); + break; + } + case 5/*Noel*/:{ + printf("Number of elements in stack: %d", (stac->top+1)); + break; + } + case 6/*isEmpty*/:{ + if(isEmpty(stac)==1){ + printf("Stack is empty"); + } + else + printf("Stack is not empty"); + break; + } + case 7/*isFull*/:{ + if(isFull(stac)==1){ + printf("Stack is full"); + } + else + printf("Stack is not full"); + break; + } + default:{ + free(stac); + free(check); + return 0; + } + } + } +} + +int createStack(int n){ + struct stack *s; + s=(struct stack*)malloc(sizeof(struct stack)); + s->size=n; + s->arr=calloc(n,sizeof(int)); + s->top=-1; + return s; +} + +void push(struct stack *s, int element, int *check){ + if(isFull(s)==1){ + return 0; + } + else{ + s->top=(s->top+1); + int *array=s->arr; + *(array+s->top)=element; + *check=1; + } +} +; +int pop(struct stack *s,int *check){ + if(isEmpty(s)==1){ + return 0; + } + else{ + int element; + element=*(s->arr+s->top); + *(s->arr+s->top)=0; + s->top=s->top-1; + *check=1; + return element; + } +} +int peek(struct stack *s){ + int element; + element=*(s->arr+s->top); + return element; +} + +int Noel(struct stack *s){ + return s->top; +} + +int isFull(struct stack *s){ + if(s->top+1==s->size) + return 1; + else + return 0; + +} + +int isEmpty(struct stack *s){ + if(s->top==-1){ + return 1; + } + else + return 0; +}