0% found this document useful (0 votes)
69 views39 pages

Array Queue

The document discusses code implementations for several common data structures: 1) An array-based queue with functions for insertion, removal, counting, and displaying elements. 2) An array-based stack with similar push, pop, peek, count and display functions. 3) A binary search tree (BST) implementation with insert, remove, and traversal functions to display the tree in various orders.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views39 pages

Array Queue

The document discusses code implementations for several common data structures: 1) An array-based queue with functions for insertion, removal, counting, and displaying elements. 2) An array-based stack with similar push, pop, peek, count and display functions. 3) A binary search tree (BST) implementation with insert, remove, and traversal functions to display the tree in various orders.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

ARRAY QUEUE

#include <stdio.h>
#include <stdlib.h>
#define size 10

int queue[size],front,rear;

void insert(int);
int remove(void);
int count(void);
void show(void);

int main(void){
int x,flag=0;
front=rear=-1;
char choice;
do{
printf("This Queue allows you to do the following
operations\n");
printf("I --> Insert a value\n");
printf("R --> Remove a value\n");
printf("S --> Show values\n");
printf("C --> Count values\n");
printf("X --> eXit\n");
printf("your choice :");
choice=getc(stdin);
switch(choice){
case 'i':
case 'I':
printf("enter a value:");
scanf("%d",&x);
insert(x);
break;
case 'r':
case 'R':
if(front!=-1)
printf("removed value is :%d\n",remove());
else
printf("queue is now empty!\n");
break;
case 'c':
case 'C':
printf("no.of nodes in the queue
are:%d\n",count());
break;
case 's':
case 'S':
show();
break;
case 'x':
case 'X':
flag++;
printf("Good Bye!\n");
break;
default:
printf("Invalid Choice! try again..\n");
}
system("pause");
system("cls");
fflush(stdin);
}while(!flag);
return 0;
}

void insert(int data){


if(rear==(size-1))
printf("queue is full\n");
else{
if(rear==-1)
front++;
queue[++rear]=data;
printf("inserted...\n");
}
}

int remove(void){
int data=queue[front],k;
if(front==rear)
front=rear=-1;
else{
for(k=0;k<rear;k++)
queue[k]=queue[k+1];
rear--;
}
return data;
}
int count(void){
if(front==-1)
return 0;
else
return (rear-front)+1;
}
void show(void){
int i;
if(front==-1)
printf("queue is empty!\n");
else{
printf("elements in the queue are\n");
for(i=front;i<=rear;i++)
printf("%d ",queue[i]);
printf("\n");
}
}

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

#define size 10

int stack[size],top=-1;

void push(int);
int pop(void);
int peep(void);
int count(void);
void show(void);

int main(void){
int x,flag=0;
char choice;
do{
printf("This Stack allows you to do the following
operations\n");
printf("P --> Push a value\n");
printf("O --> Pop a value\n");
printf("E --> Peep a value\n");
printf("S --> Show values\n");
printf("C --> Count values\n");
printf("X --> eXit\n");
printf("your choice :");
choice=getc(stdin);
switch(choice){
case 'P':
case 'p':
printf("enter an int value:");
scanf("%d",&x);
push(x);
break;
case 'e':
case 'E':
if(top!=-1)
printf("peeped value is :%d\n",peep());
else
printf("stack is now empty!\n");
break;
case 'o':
case 'O':
if(top!=-1)
printf("popped value is :%d\n",pop());
else
printf("stack is now empty!\n");
break;
case 'c':
case 'C':
printf("no.of elements in the stack
are:%d\n",count());
break;
case 's':
case 'S':
show();
break;
case 'x':
case 'X':
flag++;
printf("Good Bye!\n");
break;
default:
printf("Invalid Choice! try again..\n");
}
system("pause");
system("cls");
fflush(stdin);
}while(!flag);
return 0;
}

void push(int data){


if(top==(size-1))
printf("Stack is Full!\n");
else{
stack[++top]=data;
printf("value pushed!\n");
}
}

int pop(void){
return stack[top--];
}
int peep(void){
return stack[top];
}
int count(void){
return top+1;
}
void show(void){
int i;
if(top==-1)
printf("stack is empty!\n");
else{
printf("elements in the stack are\n");
for(i=top;i>=0;i--)
printf("%d ",stack[i]);
printf("\n");
}
}

BST
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<alloc.h>

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

node *root=NULL;
int found;

node* insert(int,node*);
node* remove(int,node*);
void showInOrder(node*);
void showPostOrder(node*);
void showPreOrder(node*);

int main(void){
int x,flag=0;
char choice;
do{
printf("You have following options for BST\n");
printf("I --> Insert\n");
printf("R --> Remove\n");
printf("S --> Show\n");
printf("X --> eXit\n");
printf("choice :");
choice=getche();
printf("\n");
switch(choice){
case 'I':
case 'i':
printf("enter node data:");
scanf("%d",&x);
root=insert(x,root);
break;
case 'r':
case 'R':
found=0;
if(root==NULL){
printf("tree not yet populated!\n");
break;
}
printf("enter node data:");
scanf("%d",&x);
root=remove(x,root);
if(found)
printf("node removed and tree re
organized!\n");
else
printf("node not found!\n");
break;
case 'S':
case 's':
if(root==NULL){
printf("tree not yet populated!\n");
break;
}
printf("1. inorder\n");
printf("2. postorder\n");
printf("3. preorder\n");
printf("option please:");
scanf("%d",&x);
if(x==1)
showInOrder(root);
else if(x==2)
showPostOrder(root);
else
showPreOrder(root);
break;
case 'x':
case 'X':
flag++;
printf("GoodBye!\n");
break;
default:
printf("Invalid Choce.. Try again!\n");
}
getche();
fflush(stdin);
system("cls");
}while(!flag);
}
node* insert(int d,node *p){
if(p==NULL){
p=(node*)calloc(sizeof(node),1);
p->data=d;
p->left=p->right=NULL;
printf("node inserted!\n");
return p;
}else if(p->data>d)
p->left=insert(d,p->left);
else if(p->data<d)
p->right=insert(d,p->right);
else
printf("duplicate data not allowed!\n");
return p;
}
void showInOrder(node *p){
if(p==NULL)
return;
showInOrder(p->left);
if(p==root)
printf("\"");
printf(" %d ",p->data);
if(p==root)
printf("\"");
showInOrder(p->right);
}
void showPostOrder(node *p){
if(p==NULL)
return;
showPostOrder(p->left);
showPostOrder(p->right);
if(p==root)
printf("\"");
printf(" %d ",p->data);
if(p==root)
printf("\"");
}
void showPreOrder(node *p){
if(p==NULL)
return;
if(p==root)
printf("\"");
printf(" %d ",p->data);
if(p==root)
printf("\"");
showPreOrder(p->left);
showPreOrder(p->right);
}
node* remove(int d,node *p){
if(p==NULL)
return p;
else if(p->data>d)
p->left=remove(d,p->left);
else if(p->data<d)
p->right=remove(d,p->right);
else{
found=1;
node *q;
if(p->left==NULL&&p->right==NULL)
q=NULL;
else if(p->left==NULL&&p->right!=NULL)
q=p->right;
else if(p->left!=NULL&&p->right==NULL)
q=p->left;
else{
q=p->right;
if(q->left==NULL)
q->left=p->left;
else{
node *r=q->left;
while(r->left!=NULL)
r=r->left;
r->left=p->left;
}
}
free(p);
return q;
}
return p;
}

CRIDBILLIST
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
#include <conio.h>

struct node{
struct node* prev;
int data;
struct node* next;
};

node *first=NULL,*last=NULL;

void addFirst(int);
void addLast(int);
void addAfter(int,int);
int remove(int);
int count(void);
void forwardShow(void);
void backwardShow(void);

int main(void)
{
int x,flag=0,y;
char choice;
do{
printf("Circular Linked list(double) has the following
options\n");
printf("A : add a node\n");
printf("R : delete a node\n");
printf("C : count nodes\n");
printf("S : show nodes\n");
printf("X : eXit\n");
printf("your choice :");
choice=getche();
printf("\n");
switch(choice){
case 'A':
case 'a':
printf("enter node data:");
scanf("%d",&x);
printf("option(1) as first\n");
printf("option(2) as last\n");
printf("option(3) as after specified
node\n");
printf("your option:");
scanf("%d",&y);
if(y==1)
addFirst(x);
else if(y==2)
addLast(x);
else{
printf("enter node number:");
scanf("%d",&y);
addAfter(x,y);
}
break;
case 'r':
case 'R':
printf("enter node data:");
scanf("%d",&x);
y=remove(x);
if(y==0)
printf("list is empty!\n");
else if(y==-1)
printf("list has no such
node\n");
else
printf("node with data(%d) is
removed\n",x);
break;
case 'x':
case 'X':
flag++;
printf("GoodBye!\n");
break;
case 'c':
case 'C':
printf("no.of nodes
are:%d\n",count());
break;
case 's':
case 'S':
printf("1. for left->right, 2. for
right->left\n");
printf("your choice:");
scanf("%d",&x);
if(x==1)
forwardShow();
else
backwardShow();
break;
default:
printf("invalid choice!\n");
}
system("pause");
fflush(stdin);
system("cls");
}while(!flag);
return 0;
}

int count(void){
int c=0;
node *p=first;
if(first!=NULL){
do{
c++;
p=p->next;
if(p==first)
break;
}while(1);
}
return c;
}
void forwardShow(void){
node *p=first;
if(p==NULL)
printf("list is not yet populated!\n");
else{
printf("list has following nodes\n");
do{
printf("%d",p->data);
if(p->next!=first){
printf("-->");
p=p->next;
}else
break;
}while(1);
printf("==>First\n");
}
}
void backwardShow(void){
node *p=last;
if(p==NULL)
printf("list is not yet populated!\n");
else{
printf("list has following nodes\n");
do{
printf("%d",p->data);
if(p->prev!=last){
p=p->prev;
printf("-->");
}else
break;
}while(1);
printf("==>Last\n");
}
}
void addFirst(int d){
node *p=(node*)calloc(sizeof(node),1);
p->data=d;
if(first==NULL)
first=last=p->next=p->prev=p;
else{
first->prev=last->next=p;
p->prev=last;
p->next=first;
first=p;
}
printf("node added as first node\n");
}
void addLast(int d){
node *p;
if(first==NULL)
addFirst(d);
else{
p=(node*)calloc(sizeof(node),1);
p->data=d;
p->next=first;
p->prev=last;
last->next=first->prev=p;
last=p;
printf("node added as last node\n");
}
}
void addAfter(int d,int c){
node *p,*q;
int n=0;
if(c==0 || first==NULL)
addFirst(d);
else if(c>=count())
addLast(d);
else{
p=first;
while(p->next!=NULL){
n++;
if(c==n){
q=(node*)calloc(sizeof(node),1);
q->data=d;
q->next=p->next;
q->prev=p;
p->next=q;
q->next->prev=q;
printf("node added after node(%d)\n",c);
return;
}
p=p->next;
}
}
}
int remove(int d){
node *p,*q;
if(first==NULL)
return 0;
else{
p=first;
if(p->data==d){ // if first node to be deleted
first=p->next;
if(first==p)
last=first=NULL;
else{
first->prev=last;
last->next=first;
}
free(p);
return d;
}
while(p->next!=NULL){ // after first node to be deleted
q=p->next;
if(q->data==d){
if(q->next!=NULL){ // middle nodes
p->next=q->next;
q->next->prev=p;
}
else{ // last node
last=p;
p->next=first;
first->prev=p;
}
free(q);
return d;
}
p=p->next;
}
return -1;
}
}

CIRSINGLELINK
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
#include <conio.h>

struct node{
int data;
struct node* next;
};

node *first=NULL,*last=NULL;

void addFirst(int);
void addLast(int);
void addAfter(int,int);
int remove(int);
int count(void);
void show(void);

int main(void){
int x,flag=0,y;
char choice;
do{
printf("Circular Linked list(Single) has the following
options\n");
printf("A : add a node\n");
printf("R : delete a node\n");
printf("C : count nodes\n");
printf("S : show nodes\n");
printf("X : eXit\n");
printf("your choice :");
choice=getche();
printf("\n");
switch(choice){
case 'A':
case 'a':
printf("enter node data:");
scanf("%d",&x);
if(x!=0 && x!=-1){
printf("option(1) as first\n");
printf("option(2) as last\n");
printf("option(3) as after
specified node\n");
printf("your option:");
scanf("%d",&y);
if(y==1)
addFirst(x);
else if(y==2)
addLast(x);
else{
printf("enter node
number:");
scanf("%d",&y);
addAfter(x,y);
}
}else
printf("enter other than 0 or -
1!\n");
break;
case 'r':
case 'R':
printf("enter node data:");
scanf("%d",&x);
y=remove(x);
if(y==0)
printf("list is empty!\n");
else if(y==-1)
printf("list has no such
node\n");
else
printf("node with data(%d) is
removed\n",x);
break;
case 'x':
case 'X':
flag++;
printf("GoodBye!\n");
break;
case 'c':
case 'C':
printf("no.of nodes
are:%d\n",count());
break;
case 's':
case 'S':
if(first!=NULL)
show();
else
printf("list yet to be
populated!\n");
break;
default:
printf("invalid choice!\n");
}
system("pause");
fflush(stdin);
system("cls");
}while(!flag);
return 0;
}

int count(void){
int c=0;
node *p=first;
if(first==NULL)
return c;
else{
do{
c++;
p=p->next;
if(p==first)
break;
}while(1);
}
return c;
}
void show(void){
node *p=first;
printf("list has following nodes\n");
do{
printf("%d",p->data);
p=p->next;
if(p==first)
break;
printf("->");
}while(1);
printf("==>first\n");
}
void addFirst(int d){
node *p=(node*)calloc(sizeof(node),1);
p->data=d;
if(first==NULL)
p->next=first=last=p;// chain assignment
else{
p->next=first;
last->next=first=p;
}
printf("node added as first node\n");
}
void addLast(int d){
node *p;
if(first==NULL)
addFirst(d);
else{
p=(node*)calloc(sizeof(node),1);
p->next=first;
p->data=d;
last->next=p;
last=p;
printf("node added as last node\n");
}
}
void addAfter(int d,int c){
node *p,*q;
int n=0;
if(c==0 || first==NULL)
addFirst(d);
if(c>=count())
addLast(d);
else{
p=first;
while(p->next!=NULL){
n++;
if(c==n){
q=(node*)calloc(sizeof(node),1);
q->data=d;
q->next=p->next;
p->next=q;
printf("node added after node(%d)\n",c);
return;
}
p=p->next;
}
}
}
int remove(int d){
node *p,*q;
if(first==NULL)
return 0;
else{
p=first;
if(first->data==d){ // if first node to be deleted
first=first->next;
if(first==p)
last=first=NULL;
else
last->next=first;
free(p);
return d;
}
while(p->next!=first){ // after first node to be deleted
q=p->next;
if(q->data==d){
if(q->next!=first) // middle nodes
p->next=q->next;
else{ // last node
last=p;
p->next=first;
}
free(q);
return d;
}
p=p->next;
}
return -1;
}
}

DBILLIST
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
#include <conio.h>

struct node{
struct node* prev;
int data;
struct node* next;
};

node *first=NULL,*last=NULL;

void addFirst(int);
void addLast(int);
void addAfter(int,int);
int remove(int);
int count(void);
void forwardShow(void);
void backwardShow(void);

int main(void){
int x,flag=0,y;
char choice;
do{
printf("Linked list(double) has the following options\n");
printf("A : add a node\n");
printf("R : delete a node\n");
printf("C : count nodes\n");
printf("S : show nodes\n");
printf("X : eXit\n");
printf("your choice :");
choice=getche();
printf("\n");
switch(choice){
case 'A':
case 'a':
printf("enter node data:");
scanf("%d",&x);
printf("option(1) as first\n");
printf("option(2) as last\n");
printf("option(3) as after specified
node\n");
printf("your option:");
scanf("%d",&y);
if(y==1)
addFirst(x);
else if(y==2)
addLast(x);
else{
printf("enter node number:");
scanf("%d",&y);
addAfter(x,y);
}
break;
case 'r':
case 'R':
printf("enter node data:");
scanf("%d",&x);
y=remove(x);
if(y==0)
printf("list is empty!\n");
else if(y==-1)
printf("list has no such
node\n");
else
printf("node with data(%d) is
removed\n",x);
break;
case 'x':
case 'X':
flag++;
printf("GoodBye!\n");
break;
case 'c':
case 'C':
printf("no.of nodes
are:%d\n",count());
break;
case 's':
case 'S':
printf("1. for left->right, 2. for
right->left\n");
printf("your choice:");
scanf("%d",&x);
if(x==1)
forwardShow();
else
backwardShow();
break;
default:
printf("invalid choice!\n");
}
system("pause");
fflush(stdin);
system("cls");
}while(!flag);
return 0;
}

int count(void){
int c=0;
node *p=first;
while(p!=NULL){
p=p->next;
c++;
}
return c;
}
void forwardShow(void){
node *p=first;
if(p==NULL)
printf("list is not yet populated!\n");
else{
printf("list has following nodes\n");
while(p!=NULL){
printf("%d",p->data);
p=p->next;
if(p!=NULL)
printf("-->");
}
printf("==>NULL\n");
}
}
void backwardShow(void){
node *p=last;
if(p==NULL)
printf("list is not yet populated!\n");
else{
printf("list has following nodes\n");
while(p!=NULL){
printf("%d",p->data);
p=p->prev;
if(p!=NULL)
printf("-->");
}
printf("==>NULL\n");
}
}
void addFirst(int d){
node *p=(node*)calloc(sizeof(node),1);
p->data=d;
p->prev=NULL;
p->next=first;
if(first==NULL)
last=p;
else
first->prev=p;
first=p;
printf("node added as first node\n");
}
void addLast(int d){
node *p;
if(first==NULL)
addFirst(d);
else{
p=(node*)calloc(sizeof(node),1);
p->data=d;
p->next=NULL;
p->prev=last;
last->next=p;
last=p;
printf("node added as last node\n");
}
}
void addAfter(int d,int c){
node *p,*q;
int n=0;
if(c==0 || first==NULL)
addFirst(d);
else if(c>=count())
addLast(d);
else{
p=first;
while(p->next!=NULL){
n++;
if(c==n){
q=(node*)calloc(sizeof(node),1);
q->data=d;
q->next=p->next;
q->prev=p;
p->next=q;
q->next->prev=q;
printf("node added after node(%d)\n",c);
return;
}
p=p->next;
}
}
}
int remove(int d){
node *p,*q;
if(first==NULL)
return 0;
else{
p=first;
if(p->data==d){ // if first node to be deleted
first=p->next;
if(first==NULL)
last=NULL;
else
first->prev=NULL;
free(p);
return d;
}
while(p->next!=NULL){ // after first node to be deleted
q=p->next;
if(q->data==d){
if(q->next!=NULL){ // middle nodes
p->next=q->next;
q->next->prev=p;
}
else{ // last node
last=p;
p->next=NULL;
}
free(q);
return d;
}
p=p->next;
}
return -1;
}
}

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

int main(void){
char ar[]="1$258<i";
int i=atoi(ar);
printf("i is :%d\n",i);
return 0;
}

INFIX2POSTFIX
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#define size 10

int valueStack[size],valueStack_top,operatorStack_top;
char operatorStack[size];

int computeResult(char*);
void valuePush(int);
void operatorPush(char);
int valuePop(void);
char operatorPop();
char peep(void);
int isStackEmpty(void);
int topOperatorGreaterEqual(char,char);
void infix2postfix(char*,char*);

int main(void)
{
char infix[20],postfix[20];
printf("enter u'r valid infix expression:");
gets(infix);
infix2postfix(infix,postfix);
printf("Infix Expression \" %s \" and Postfix Expression \" %s
\"\n",infix,postfix);
printf("final outcome of given expression\" %s \" is
:%d",infix,computeResult(postfix));
printf("\n");
return 0;
}

void valuePush(int val)


{
valueStack[valueStack_top]=val;
valueStack_top++;
}
int valuePop(void)
{
valueStack_top--;
return valueStack[valueStack_top];
}
int computeResult(char *x)
{
int op1,op2; char ch;
do
{
ch=*x;
if(ch==0)
return valuePop();
if(isalpha(ch))
{
printf("enter value for %c:",ch);
scanf("%d",&op1);
valuePush(op1);
}else if(isdigit(ch)){
char p[10];
int i=0;
p[i]=ch;
while(1){
ch=*(x+1);
if(isdigit(ch)){
x++;
i++;
p[i]=ch;
}else
break;
}
p[i+1]=0;
valuePush(atoi(p));
}
else
{
op2=valuePop();
op1=valuePop();
switch(ch)
{
case'+':
valuePush(op1+op2);
break;
case'-':
valuePush(op1-op2);
break;
case'*':
valuePush(op1*op2);
break;
case'/':
valuePush(op1/op2);
break;
case'%':
valuePush(op1%op2);
}
}
x++;
}while(1);
}

int isStackEmpty(void)
{
if(operatorStack_top==0)
return 1;
else
return 0;
}

void operatorPush(char val)


{
operatorStack[operatorStack_top]=val;
operatorStack_top++;
}
char operatorPop(void)
{
operatorStack_top--;
return operatorStack[operatorStack_top];
}
char peep(void)
{
return operatorStack[operatorStack_top-1];
}
int topOperatorGreaterEqual(char x,char y)
{
return (x=='*' || x=='/') >= (y=='*' || y=='/');
}
void infix2postfix(char *src,char *dstn)
{
char ch,opr;
do
{
ch=*src;
if(ch==0)
{
while(isStackEmpty()==0)
{
ch=operatorPop();
*dstn=ch;
dstn++;
}
*dstn=0;
return;
}
if( ch=='(' )
operatorPush(ch);
else if( ch==')' )
{
while(isStackEmpty()==0){
ch=operatorPop();
if(ch=='(')
break;
*dstn=ch;
dstn++;
}
}
else if(isalpha(ch)){
*dstn=ch;
dstn++;
}
else if(isdigit(ch)){
*dstn=ch;
dstn++;
while(1){
opr=*(src+1);
if(isdigit(opr)){
*dstn=opr;
dstn++;
src++;
}else
break;
}
}
else
{
while(isStackEmpty()==0 && peep()!='(')
{
if(topOperatorGreaterEqual(peep(),ch))
{
opr=operatorPop();
*dstn=opr;
dstn++;
}
else
break;
}
operatorPush(ch);
}
src++;
}while(1);
}

Infix to Postfix conversion Algorthm

Assumptions

========
arrays 'ie' and 'pe' are global variables

-> declare variables [ int i,j; and char ch,opr;]

-> let i=j=0;

-> repeat the following as long as ie[i]!=0

a. ch = ie[i];

b. if 'ch' is alphabet write to pe. { pe[j++]=ch; }

c. if 'ch' is '(', then push ch to stack.

d. if 'ch' is ')' then repeat the following

* pop the stack and store in 'opr' { opr = pop(); }

* if 'opr' is '(' then break { if(opr=='(') break; }

* write to pe. { pe[j++'=opr; }

e. if 'ch' is operator

* then do the following as long as stack is not empty and peeped operator precedency
is

greater than or equal to 'ch'

# pop the stack and write to pe. { pe[j++]=pop(); }

* push 'ch' to the stack { push(ch); }

f. add 1 to i { i++;}

-> repeat the following as long as stack is not empty

a. pop the stack and write to pe. { pe[j++]=pop(); }

-> write '\0' to pe { pe[j]=NULL; }

LINK QUEUE
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
#include <conio.h>

struct node{
int data;
struct node *next;
};

node* front=NULL,rear=NULL;

void insert(int);
int remove(void);
int count(void);
void show(void);

int main(void){
int x,flag=0;
char choice;
do{
printf("This Queue allows you to do the following
operations\n");
printf("I --> Insert a value\n");
printf("R --> Remove a value\n");
printf("S --> Show values\n");
printf("C --> Count values\n");
printf("X --> eXit\n");
printf("your choice :");
choice=getche();
printf("\n");
switch(choice){
case 'i':
case 'I':
printf("enter a value:");
scanf("%d",&x);
insert(x);
break;
case 'r':
case 'R':
if(front!=NULL)
printf("removed value is :%d\n",remove());
else
printf("queue is now empty!\n");
break;
case 'c':
case 'C':
printf("no.of nodes in the queue
are:%d\n",count());
break;
case 's':
case 'S':
if(front!=NULL)
show();
else
printf("Queue is empty!\n");
break;
case 'x':
case 'X':
flag++;
printf("Good Bye!\n");
break;
default:
printf("Invalid Choice! try again..\n");
}
system("pause");
system("cls");
fflush(stdin);
}while(!flag);
return 0;
}

void insert(int d){


node *p=(node*)calloc(sizeof(node),1);
p->data=d;
p->next=NULL;
if(front==NULL)
front=p;
else
rear->next=p;
rear=p;
printf("node inserted!\n");
}

int remove(void){
node* p=front;
int x=p->data;
front=front->next;
free(p);
if(front==NULL)
rear=NULL;
return x;
}
int count(void){
int c=0;
node *p=front;
while(p!=NULL){
c++;
p=p->next;
}
return c;
}
void show(void){
node *p=front;
printf("elements in the queue are\n");
while(p!=NULL){
printf("%d",p->data);
p=p->next;
if(p!=NULL)
printf("->");
}
printf("==>NULL\n");
}

LINKSTACK
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <alloc.h>

struct node{
int data;
struct node* next;
};
node* top=NULL;

void push(int);
int pop(void);
int count(void);
void show(void);

int main(void){
int x,flag=0;
char choice;
do{
printf("This Stack allows you to do the following
operations\n");
printf("P --> Push a value\n");
printf("O --> Pop a value\n");
printf("E --> Peep a value\n");
printf("S --> Show values\n");
printf("C --> Count values\n");
printf("X --> eXit\n");
printf("your choice :");
choice=getche();
printf("\n");
switch(choice){
case 'P':
case 'p':
printf("enter a value:");
scanf("%d",&x);
push(x);
break;
case 'e':
case 'E':
if(top!=NULL)
printf("peeped value is :%d\n",top->data);
else
printf("stack is now empty!\n");
break;
case 'o':
case 'O':
if(top!=NULL)
printf("popped value is :%d\n",pop());
else
printf("stack is now empty!\n");
break;
case 'c':
case 'C':
printf("no.of nodes in the stack
are:%d\n",count());
break;
case 's':
case 'S':
if(top!=NULL)
show();
else
printf("stack is empty!\n");
break;
case 'x':
case 'X':
flag++;
printf("Good Bye!\n");
break;
default:
printf("Invalid Choice! try again..\n");
}
system("pause");
system("cls");
fflush(stdin);
}while(!flag);
return 0;
}
void push(int d){
node *p=(node*)calloc(sizeof(node),1);
p->data=d;
p->next=top;
top=p;
printf("node pushed!\n");
}

int pop(void){
node* p=top;
int x=p->data;
top=top->next;
free(p);
return x;
}

int count(void){
node* p=top;
int c=0;
while(p!=NULL){
c++;
p=p->next;
}
return c;
}
void show(void){
node *p=top;
printf("elements in the stack are\n");
while(p!=NULL){
printf("%d",p->data);
p=p->next;
if(p!=NULL)
printf("->");
}
printf("==>NULL\n");
}

SINGLELINK
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
#include <conio.h>

struct node{
int data;
struct node* next;
};

node *first=NULL,*last=NULL;

void addFirst(int);
void addLast(int);
void addAfter(int,int);
int remove(int);
int count(void);
void show(void);

int main(void){
int x,flag=0,y;
char choice;
do{
printf("Linked list has the following options\n");
printf("A : add a node\n");
printf("R : delete a node\n");
printf("C : count nodes\n");
printf("S : show nodes\n");
printf("X : eXit\n");
printf("your choice :");
choice=getche();
printf("\n");
switch(choice){
case 'A':
case 'a':
printf("enter node data:");
scanf("%d",&x);
if(x!=0 && x!=-1){
printf("option(1) as first\n");
printf("option(2) as last\n");
printf("option(3) as after
specified node\n");
printf("your option:");
scanf("%d",&y);
if(y==1)
addFirst(x);
else if(y==2)
addLast(x);
else{
printf("enter node
number:");
scanf("%d",&y);
addAfter(x,y);
}
}else
printf("enter other than 0 or -
1!\n");
break;
case 'r':
case 'R':
printf("enter node data:");
scanf("%d",&x);
y=remove(x);
if(y==0)
printf("list is empty!\n");
else if(y==-1)
printf("list has no such
node\n");
else
printf("node with data(%d) is
removed\n",x);
break;
case 'x':
case 'X':
flag++;
printf("GoodBye!\n");
break;
case 'c':
case 'C':
printf("no.of nodes
are:%d\n",count());
break;
case 's':
case 'S':
show();
break;
default:
printf("invalid choice!\n");
}
system("pause");
fflush(stdin);
system("cls");
}while(!flag);
return 0;
}

int count(void){
int c=0;
node *p=first;
while(p!=NULL){
p=p->next;
c++;
}
return c;
}
void show(void){
node *p=first;
if(p==NULL)
printf("list is not yet populated!\n");
else{
printf("list has following nodes\n");
while(p!=NULL){
printf("%d",p->data);
p=p->next;
if(p!=NULL)
printf("-->");
}
printf("==>NULL\n");
}
}
void addFirst(int d){
node *p=(node*)calloc(sizeof(node),1);
p->data=d;
p->next=first;
first=p;
if(last==NULL)
last=p;
printf("node added as first node\n");
}
void addLast(int d){
node *p;
if(first==NULL)
addFirst(d);
else{
p=(node*)calloc(sizeof(node),1);
p->next=NULL;
p->data=d;
last->next=p;
last=p;
printf("node added as last node\n");
}
}
void addAfter(int d,int c){
node *p,*q;
int n=0;
if(c==0 || first==NULL)
addFirst(d);
else if(c>=count())
addLast(d);
else{
p=first;
while(p->next!=NULL){
n++;
if(c==n){
q=(node*)calloc(sizeof(node),1);
q->data=d;
q->next=p->next;
p->next=q;
printf("node added after node(%d)\n",c);
return;
}
p=p->next;
}
}
}
int remove(int d){
node *p,*q;
if(first==NULL)
return 0;
else{
p=first;
if(first->data==d){ // if first node to be deleted
first=first->next;
if(first==NULL)
last=NULL;
free(p);
return d;
}
while(p->next!=NULL){ // after first node to be deleted
q=p->next;
if(q->data==d){
if(q->next!=NULL) // middle nodes
p->next=q->next;
else{ // last node
last=p;
p->next=NULL;
}
free(q);
return d;
}
p=p->next;
}
return -1;
}
}

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

int size,*a;

void read(void);
void show(void);
int bsearch(int);
int bsearchrec(int,int,int);
void bubble(void);
void insertion(void);
void selection(void);
void quick(int,int);
void shell(void);

int main(void){
int k,p;
char choice;
do{
printf("enter your array size:");
scanf("%d",&size);
a=(int*)calloc(sizeof(int),size);
read();
printf("array values in given order\n");
show();

printf("1.Bubble\n2.Selection\n3.Insertion\n4.Shell\n5.Quick\n");
printf("choose your sorting techniue:");
scanf("%d",&k);
switch(k){
case 1:
bubble();
break;
case 2:
selection();
break;
case 3:
insertion();
break;
case 4:
shell();
break;
case 5:
quick(0,size-1);
break;
default:
printf("invalid choice!\n");
}
printf("array values after sorted!\n");
show();
printf("enter a number to search:");
scanf("%d",&k);
p=bsearchrec(k,0,size-1);
if(p>=0)
printf("%d is found at index %d\n",k,p);
else
printf("%d is not found\n",k);
fflush(stdin);
printf("to stop .. press 'Y' or 'y', to continue press any
other key:");
choice=getc(stdin);
if(choice=='y' || choice == 'Y')
break;
free(a);
fflush(stdin);
system("cls");
}while(1);
return 0;
}
void read(void){
int i=0;
printf("enter %d integers one by one\n",size);
for(;i<size;i++)
scanf("%d",a+i);
fflush(stdin);
}
void show(void){
printf("[");
for(int i=0;i<size;i++){
printf("%d",a[i]);
if(i!=(size-1))
printf(", ");
}
printf("]\n");

}
void bubble(void){
int i=0,j,p;
for(;i<size-1;i++){
for(j=i+1;j<size;j++){
if(a[i]>a[j]){
p=a[i];
a[i]=a[j];
a[j]=p;
}
}
printf("array values after pass(%d)\n",i+1);
show();
}
}
int bsearch(int key){
int start=0,end=size-1,mid;
while(start<=end){
mid=(start+end)/2;
if(a[mid]==key)
return mid;
if(a[mid]>key)
end=mid-1;
else
start=mid+1;
}
return -1;
}
int bsearchrec(int key,int start,int end){
int mid;
if(start<=end){
mid=(start+end)/2;
if(a[mid]==key)
return mid;
else if(a[mid]<key)
return bsearchrec(key,mid+1,end);
else
return bsearchrec(key,start,mid-1);
}else
return -1;
}

void selection(void)
{
int min,temp;
for(int i=0;i<size-1;i++)
{
min=i;
for(int j=i+1;j<size;j++)
if(a[min] > a[j])
min=j;
temp=a[i];
a[i]=a[min];
a[min]=temp;
printf("array values after pass(%d)\n",i+1);
show();
}
}
void insertion(void)
{
int i,j,index;
for(i=1;i<size;i++)
{
index=a[i];
j=i;
while( (j>0) && (a[j-1] > index) )
{
a[j]=a[j-1];
j--;
}
a[j]=index;
printf("array values after pass(%d)\n",i+1);
show();
}
}
void quick(int left,int right)
{
int pivot,l,r;
l=left,r=right,pivot=a[left];
while(left<right)
{
while( (a[right] >= pivot) && (left < right) )
right--;
if(left!=right)
{
a[left]=a[right];
left++;
}
while( (a[left] <= pivot) && (left <right) )
left++;
if(left!=right)
{
a[right]=a[left];
right--;
}
}
printf("pivot(<<%d>>)\n",pivot);
show();
a[left]=pivot,pivot=left,left=l,right=r;
if(left < pivot)
quick(left,pivot-1);
if(right > pivot)
quick(pivot+1,right);
}
void shell(void)
{
int i,j,temp,shellgap;
if(size>10)
shellgap=5;
else if(size>5)
shellgap=3;
else
shellgap=1;
while(shellgap)
{
for(i=0;i<size;i++)
{
j=i;
temp=a[i];
while( (j>=shellgap) && (a[j-shellgap] > temp) )
{
a[j]=a[j-shellgap];
j=j-shellgap;
}
a[j]=temp;
}
printf("at shellgap(%d)\n values are\n",shellgap);
show();
if(shellgap==5)
shellgap=3;
else if(shellgap==3)
shellgap=1;
else
shellgap=0;
}
}
S

You might also like