-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1346 from BhumitRohilla/master
Added Implementation of DS
- Loading branch information
Showing
5 changed files
with
652 additions
and
0 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
Program's_Contributed_By_Contributors/Implementation_of_DS/circluar_linkedList.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
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; | ||
} |
142 changes: 142 additions & 0 deletions
142
Program's_Contributed_By_Contributors/Implementation_of_DS/circular_queue.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
//Implement circular queue | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <limits.h> | ||
|
||
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; | ||
} |
93 changes: 93 additions & 0 deletions
93
Program's_Contributed_By_Contributors/Implementation_of_DS/linked_list.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
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); | ||
} |
Oops, something went wrong.