/*1*/#include
/*2*/#include
/*3*/#define MAX 10 //The maximum size of the stack
/*4*/void push(int stack[], int *top, int value) //function for insertion
{
/*5*/ if(*top < top =" *top">= 0 )//if stack has elements
{
/*13*/ *value = stack[*top];
/*14*/*top = *top - 1;
}
/*15*/ else // if stack is empty
{
/*16*/ printf("The stack is empty can not pop a value\n");
/*17*/ exit(0);
}
}
/*18*/int main()
{
/*19*/int stack[MAX];
/*20*/int top = -1; //initialise stack top
/*21*/int n,value;
/*22*/do
{
/*23*/do
{
/*24*/ printf("Enter the element to be pushed\n");
/*25*/scanf("%d",&value);
/*26*/push(stack,&top,value); // insert element
/*27*/printf("Enter 1 to continue\n");
/*28*/scanf("%d",&n);
/*29*/} while(n == 1);
/*30*/printf("Enter 1 to pop an element\n");
/*31*/scanf("%d",&n);
/*32*/while( n == 1)
{
/*33*/pop(stack,&top,&value); //delete element
/*34*/ printf("The value poped is %d\n",value);
/*35*/ printf("Enter 1 to pop an element\n");
/*36*/ scanf("%d",&n);
}
/*37*/ printf("Enter 1 to continue\n");
/*38*/ scanf("%d",&n);
/*39*/ } while(n == 1);
/*40*/return 0;
}
//stack implementation using linked list
/*1*/# include
/*2*/# include
/*3*/struct node //declare a structure named node with data and link to the next node
{
/*4*/ int data;
/*5*/ struct node *link;
};
/*6*/struct node *push(struct node *p, int value) //function for inserting elements
{
/*7*/ struct node *temp;
/*8*/temp=(struct node *)malloc(sizeof(struct node)); // creates new node using data value passed as parameter
/*9*/if(temp==NULL) //no memory available
{
/*10*/ printf("No Memory available Error\n");
/*11*/exit(0); // terminate the program
}
/*12*/temp->data = value; //set value of element (same as *temp.data=value)
/*13*/temp->link = p; //set the link(same as *temp.link=p)
/*14*/p = temp; //set p to temp;
/*15*/return(p);
}
/*16*/struct node *pop(struct node *p, int *value) // function for deleting elements
{
/*17*/struct node *temp;
/*18*/if(p==NULL) //condition for empty stack
{
/*19*/printf(" The stack is empty can not pop Error\n");
/*20*/exit(0);
}
/*21*/*value = p->data;//set value to data (same as *value=*p.data)
/*22*/temp = p; //set temp to p
/*23*/p = p->link;//set new link
/*24*/free(temp);//free memory
/*25*/return(p);
}
/*26*/int main()
{
/*27*/struct node *top = NULL; // initialize top of stack to null
/*28*/int n,value;
/*29*/do
{
/*30*/ do
{
/*31*/printf("Enter the element to be pushed\n");
/*32*/scanf("%d",&value);
/*33*/top = push(top,value);//insertion
/*34*/printf("Enter 1 to continue\n");
/*35*/scanf("%d",&n);
/*36*/ } while(n == 1);
/*37*/printf("Enter 1 to pop an element\n");
/*38*/scanf("%d",&n);
/*39*/while( n == 1)
{
/*40*/top = pop(top,&value);//deletion
/*41*/printf("The value poped is %d\n",value);
/*42*/printf("Enter 1 to pop an element\n");
/*43*/ scanf("%d",&n);
}
/*44*/printf("Enter 1 to continue\n");
/*45*/scanf("%d",&n);
/*46*/ } while(n == 1);
/*47*/return 0;
}
//array implementation of queue
/*1*/#include
/*2*/#include
/*3*/#define MAX 10 // The maximum size of the queue
/*4*/void insert(int queue[], int *rear, int value)
{
/*5*/if(*rear < rear=" *rear" front ="="" front =" *front" value =" queue[*front];" front="rear="(-1);" n ="="" n ="="" n ="="">//for printf() and scanf()
/*2*/# include
/*3*/struct node //declare a structure named node with data and link to the next node
{
/*4*/int data;
/*5*/struct node *link;
};
/*6*/void insert(struct node **front, struct node **rear, int value) //function for inserting elements
{
/*7*/struct node *temp;
/*8*/temp=(struct node *)malloc(sizeof(struct node)); // creates new node using data value passed as parameter
/*9*/ if(temp==NULL) //no memory available
{
/*10*/printf("No Memory available Error\n");
/*11*/exit(0);
}
/*12*/temp->data = value; //set value of element (same as *temp.data=value)
/*13*/temp->link=NULL;
/*14*/if(*rear == NULL)
{
/*15*/ *rear = temp;
/*16*/ *front = *rear;
}
/*17/ else
{
/*18*/ (*rear)->link = temp;
/*19*/ *rear = temp;
}
}
/*20*/void del(struct node **front, struct node **rear, int *value)
{
/*21*/struct node *temp;
/*22*/if((*front == *rear) && (*rear == NULL))
{
/*23*/ printf(" The queue is empty cannot delete Error\n");
/*24*/exit(0);
}
/*25*/*value = (*front)->data;
/*26*/temp = *front;
/*27*/ *front = (*front)->link;
/*28*/ if(*rear == temp)
/*29*/ *rear = (*rear)->link;
/*30*/ free(temp);
}
/*31*/int main()
{
/*32*/struct node *front=NULL,*rear = NULL;
/*33*/int n,value;
/*34*/ do
{
/*35*/ do
{
/*36*/ printf("Enter the element to be inserted\n");
/*37*/scanf("%d",&value);
/*38*/ insert(&front,&rear,value);
/*39*/ printf("Enter 1 to continue\n");
/*40*/ scanf("%d",&n);
/*41*/ } while(n == 1);
/*42*/ printf("Enter 1 to delete an element\n");
/*43*/ scanf("%d",&n);
/*44*/ while( n == 1)
{
/*45*/del(&front,&rear,&value);
/*46*/printf("The value deleted is %d\n",value);
/*47*/ printf("Enter 1 to delete an element\n");
/*48*/ scanf("%d",&n);
}
/*49*/printf("Enter 1 to continue\n");
/*50*/scanf("%d",&n);
/*51*/ } while(n == 1);
/*52*/return 0;
}
//circular queue
/*1*/#include
/*2*/#include
/*3*/#define MAX 10 // The maximum size of the queue
/*4*/void insert(int queue[], int *rear, int front, int value)
{
/*5*/*rear= (*rear +1) % MAX; //to avoid problem of linear queue
/*6*/if(*rear == front) //if space is not available
{
/*7*/ printf("The queue is full can not insert a value\n");
/*8*/exit(0);
}
/*9*/ queue[*rear] = value;
}
/*10*/void del(int queue[], int *front, int rear, int * value)
{
/*11*/if(*front == rear) // queue empty
{
/*12*/printf("The queue is empty can not delete a value\n");
/*13*/exit(0);
}
/*14*/*front = (*front + 1) % MAX; //increment front
/*15*/*value = queue[*front];//set new value
}
/*16*/int main()
{
/*17*/int queue[MAX];
/*18*/int front,rear;
/*19*/int n,value;
/*20*/front=0; rear=0;
/*21*/ do
{
/*22*/ do
{
/*23*/ printf("Enter the element to be inserted\n");
/*24*/scanf("%d",&value);
/*25*/ insert(queue,&rear,front,value); //insertion
/*26*/ printf("Enter 1 to continue\n");
/*27*/ scanf("%d",&n);
/*28*/ } while(n == 1);
/*29*/printf("Enter 1 to delete an element\n");
/*30*/scanf("%d",&n);
/*31*/while( n == 1)
{
/*32*/del(queue,&front,rear,&value); //deletion
/*33*/printf("The value deleted is %d\n",value);
/*34*/printf("Enter 1 to delete an element\n");
/*35*/scanf("%d",&n);
}
/*36*/ printf("Enter 1 to continue\n");
/*37*/ scanf("%d",&n);
/*38*/} while(n == 1);
/*39*/return 0;
}
No comments:
Post a Comment