Sunday, 7 December 2014

Program For Dynamic Implementation Of Stack

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node *next;
}*top;

void popStack()
{
struct Node *temp, *var=top;
if(var==top)
{
top = top->next;
free(var);
}
else
printf("\nStack Empty");
}

void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (top == NULL)
{
top=temp;
top->next=NULL;
}
else
{
temp->next=top;
top=temp;
}
}

void display()
{
struct Node *var=top;
if(var!=NULL)
{
printf("\nElements are as:\n");
while(var!=NULL)
{
printf("\t%d\n",var->Data);
var=var->next;
}
printf("\n");
}
else
printf("\nStack is Empty");
}

int main(int argc, char *argv[])
{
int i=0;
clrscr();
top=NULL;
printf(" \n1. Push to stack");
printf(" \n2. Pop from Stack");
printf(" \n3. Display data of Stack");
printf(" \n4. Exit\n");
while(1)
{
printf("Choose Option:");
scanf("%d",&i);
switch(i)
{
case 1:
{
int value;
printf("Enter a value to push into Stack:");
scanf("%d",&value);
push(value);
break;
}
case 2:
{
popStack();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
struct Node *temp;
while(top!=NULL)
{
temp = top->next;
free(top);
top=temp;
}
exit(0);
}
default:
{
printf("\nwrong choice for operation");
}
}
}
getch();
a}

Wednesday, 3 December 2014

To Print Stars In Different Manner

#include<stdio.h>
#include<conio.h>
void up();
void down();
void main()
{
int f;
clrscr();
printf("===::  DUSHYANT MAINWAL  ::===\n\n");

do
{
printf("\n\nEnter your opinion:\n1. Upward\n2. Downword\n3. Quit\n");
scanf("%d",&f);
switch(f)
{
case 1: up();
break;
case 2: down();
break;
case 3: break;
default: printf("Invalid Choice\n");
}
}
while(f!=3);
getch();
}
void up()
{
int n,i,j,k,s,m=1;
printf("\nEnter how many stars you want in First line\n");
scanf("%d",&n);
s=n;
for(k=1;k<=s;k++)
{
for(i=1;i<=n;i++)
{
printf(" *");
}
printf("\n");
for(j=1;j<=m && j<=s;j++)
{
printf(" ");
}
m=m++;
n=n--;
}
}
void down()
{
int n,i,j,m=1,k,s;
printf("\nEnter how many star you want in last line\n");
scanf("%d",&n);
s=n;
for(k=1;k<=s;k++)
{
for(i=1;i<=n;i++)
{
printf(" ");
}
for(j=1;j<=m && j<=s;j++)
{
printf(" *");
}
printf("\n");
n=n--;
m=m++;
}
}