diff --git a/Linkedlist/LinkedListJazib.c b/Linkedlist/LinkedListJazib.c new file mode 100644 index 0000000..671e620 --- /dev/null +++ b/Linkedlist/LinkedListJazib.c @@ -0,0 +1,142 @@ +#include +#include + +int count=0; + +struct node{ + int a; + struct node *link; +}*head,*ptr; + + +void push(){ + printf("Current count is %d\n",count); + int b,pos,i; + ptr=(struct node*)malloc(sizeof(struct node)); + struct node *current; + struct node *prev; + printf("Enter the element you want to insert\n"); + scanf("%d",&b); + printf("Enter the position\n"); + scanf("%d",&pos); + if(pos>count+1||pos<0){ + printf("Position does not exist\n"); + return; + } + ptr->a=b; + ptr->link=NULL; + + if(head==NULL){ + head=ptr; + count++; + } + + else{ + if(pos==1){ + ptr->link=head; + head=ptr; + count++; + } + + else{ + current=head; + for(i=1;i<=count;i++){ + if(pos-1==i){ + ptr->link=current->link; + current->link=ptr; + count++; + break; + } + current=current->link; + } + } + } +} + + +void pop(){ + int pos,i; + struct node *current; + if(head==NULL){ + printf("Underflow\n"); + return; + } + printf("Enter the position\n"); + scanf("%d",&pos); + + if(pos>count||pos<0){ + printf("Position does not exist\n"); + return; + } + + if(pos==1){ + ptr=head; + head=head->link; + free(ptr); + count--; + } + else { + current=head; + for(i=1;i<=count;i++){ + if(pos-1==i){ + ptr=current->link; + current->link=current->link->link; + free(ptr); + count--; + break; + } + current=current->link; + } + } +} + +void print(){ + if(head==NULL){ + printf("\nEmpty\n\n"); + return; + } + ptr=head; + printf("\n"); + while(ptr!=NULL){ + printf("%d ",ptr->a); + ptr=ptr->link; + } + printf("\n\n"); +} + +void rev(){ + struct node *current; + struct node *prev; + struct node *next; + prev=NULL; + current=head; + next=head; + while(current!=NULL){ + next=next->link; + current->link=prev; + prev = current; + current = next; + } + head=prev; + +} + +int main(){ +int choice; +do{ + printf("Enter your choice\n"); + printf("1. Push\n2. Pop\n3. Reverse\n4. Print\n5. Exit\n"); + scanf("%d",&choice); + switch(choice){ + case 1: push(); + break; + case 2: pop(); + break; + case 3: rev(); + break; + case 4: print(); + break; +} +}while(choice!=5); + return 0; +} \ No newline at end of file diff --git a/Queues/QueueUsingPointerJazib.c b/Queues/QueueUsingPointerJazib.c new file mode 100644 index 0000000..845a0ef --- /dev/null +++ b/Queues/QueueUsingPointerJazib.c @@ -0,0 +1,81 @@ +#include +#include + + +struct node{ + int a; + struct node *link; +}*front,*ptr,*rear; + + +void push(){ + int b; + ptr=(struct node*)malloc(sizeof(struct node)); + printf("Enter the element you want to insert\n"); + scanf("%d",&b); + ptr->a=b; + ptr->link=NULL; + if(front==NULL&&rear==NULL){ + front=ptr; + rear=ptr; + } + else { + rear->link=ptr; + rear=ptr; + } +} + + +void pop(){ + struct node *ptr; + if(front==NULL&&rear==NULL){ + printf("Underflow\n"); + return; + } + if(front==rear){ + ptr=front; + free(ptr); + front=NULL; + rear=NULL; + } + else { + ptr=front; + front=front->link; + free(ptr); + } +} + +void print(){ + struct node *ptr; + if(front==NULL){ + printf("\nEmpty\n\n"); + return; + } + ptr=front; + printf("\n"); + while(ptr!=NULL){ + printf("%d ",ptr->a); + ptr=ptr->link; + } + printf("\n\n"); +} + +int main(){ +int choice,b; +front=NULL; +rear=NULL; +do{ + printf("Enter your choice\n"); + printf("1. Push\n2. Pop\n3. Print\n4. Exit\n"); + scanf("%d",&choice); + switch(choice){ + case 1: push(); + break; + case 2: pop(); + break; + case 3: print(); + break; +} +}while(choice!=4); + return 0; +} \ No newline at end of file diff --git a/Stacks/stackUsingArrayJazib.c b/Stacks/stackUsingArrayJazib.c new file mode 100644 index 0000000..baac8b2 --- /dev/null +++ b/Stacks/stackUsingArrayJazib.c @@ -0,0 +1,52 @@ +#include +int a[5],f=-1; + + +void push(){ +int b; +if(f==4){ + printf("Overflow\n"); + return; +} +printf("Enter the element you want to insert\n"); +scanf("%d",&b); +f++; +a[f]=b; +} + + +void pop(){ +if(f==-1){ + printf("Underflow\n"); + return; +} +f--; +} + +void print(){ + int i; + for(i=0;i<=f;i++){ + printf("%d ",a[i]); + } + printf("\n"); +} + +int main(){ +int choice; +do{ +printf("Enter your choice\n"); +printf("1. Push\n2. Pop\n3. Print\n4. Exit\n"); +scanf("%d",&choice); +switch(choice){ + case 1: push(); + break; + case 2: pop(); + break; + case 3: print(); + break; +} +}while(choice!=4); + + +return 0; +} \ No newline at end of file diff --git a/Stacks/stackUsingPointerJazib.c b/Stacks/stackUsingPointerJazib.c new file mode 100644 index 0000000..111e3d0 --- /dev/null +++ b/Stacks/stackUsingPointerJazib.c @@ -0,0 +1,69 @@ +#include +#include + + +struct node{ + int a; + struct node *link; +}*top,*ptr; + + +void push(){ + int b; + ptr=(struct node*)malloc(sizeof(struct node)); + printf("Enter the element you want to insert\n"); + scanf("%d",&b); + ptr->a=b; + ptr->link=NULL; + if(top==NULL) + top=ptr; + else { + ptr->link=top; + top=ptr; + } +} + + +void pop(){ + struct node *ptr; + if(top==NULL){ + printf("Underflow\n"); + return; + } + ptr=top; + top = top -> link; + free(ptr); +} + +void print(){ + struct node *ptr; + if(top==NULL){ + printf("Empty\n"); + return; + } + ptr=top; + while(ptr!=NULL){ + printf("%d ",ptr->a); + ptr=ptr->link; + } + printf("\n"); +} + +int main(){ +int choice,b; +top=NULL; +do{ + printf("Enter your choice\n"); + printf("1. Push\n2. Pop\n3. Print\n4. Exit\n"); + scanf("%d",&choice); + switch(choice){ + case 1: push(); + break; + case 2: pop(); + break; + case 3: print(); + break; +} +}while(choice!=4); + return 0; +} \ No newline at end of file