Monday, August 27, 2007

Stack implementation using Array - c program (stack operation)

Stack implementation using Array - c program (stack operation)
===================================================


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

int top = -1;
int stack[size];
int flag = 0;

void push (int *,int);
int pop (int *);
int peep (int *,int);
int update (int *,int);
void display (int *);

void main ()
{
int info,popped_element,position,data,pos_update,update_element,update_data;
char ch;

clrscr ();

do
{
printf("\n\ni<-Insert(push), d<-Delete(pop),p<-peep,u<-update, q<-Quit\n");
fflush (stdin);
printf("\n\nEnter your choice: ");
scanf("%c",&ch);
switch (ch)
{
case 'i':
{
printf("\n\nEnter the element you want to push: ");
scanf("%d",&info);
push (stack,info);
display (stack);
break;
}

case 'd':
{
popped_element = pop(stack);

if (flag == 0)
printf("\n\nThe element popped is %d",popped_element);
if (flag == 1)
printf("\nStack Underflow");
display (stack);
break;
}

case 'p':
{
printf("\n\nEnter the position of the stack from which you want to peep the element: ");
scanf("%d",&position);
data = peep(stack,position);
if (flag == 1)
printf("\nInvalid choice\n");
else
printf("The element peeped is %d",data);
display (stack);
break;
}

case 'u':
{
printf("Enter the position of the stack from which you want to update the data: ");
scanf("%d",&pos_update);
update_element = update(stack,pos_update);
if (flag == 1)
printf("Data can't be updated due to some reason");
else
printf("The data updated is %d",update_element);
display (stack);
break;
}

case 'q':
exit(0);
}
}while (ch != 'q');

getch ();
}

void push (int stack[],int info)
{
if (top == size-1)
printf("\nStack Overflow");
else
{
top++;
stack[top] = info;
}
}

int pop (int stack[])
{
int popped_element;
if (top == -1)
flag = 1;
else
{
flag = 0;
popped_element = stack[top];
top--;
}
return (popped_element);
}

void display (int stack[])
{
int i;
printf("\n\nThe contents of the stack are:\n");
for (i = top ; i >= 0 ; i--)
printf("%d ",stack[i]);
}

int peep (int stack[],int i)
{
int data;
if (top - i + 1 < 0)
flag = 1;
else
{
flag = 0;
data = stack[top-i+1];
}
return (data);
}

int update(int stack[],int pos)
{
int old_data;
if (top - pos + 1 < 0)
flag = 1;
else
{
flag = 0;
old_data = stack[top-pos+1];
printf("Enter the data you want to insert: ");
scanf("%d",&stack[top-pos+1]);
}
return (old_data);
}


Stack implementation using link list - c program
==============================

#include <stdio.h>
#include <conio.h>
struct Stack
{
int info;
struct Stack *next;
};
typedef struct Stack node;
node *first = NULL;
node *push (node *);
node *pop (node *);
void display (node *);

void main ()
{
char ch;

clrscr ();

do
{
fflush (stdin);
printf("\n\ni<-insert(push), d<-delete(pop), q<-quit\n");
printf("\n\nEnter the choice: ");
scanf("%c",&ch);
switch (ch)
{
case 'i':
{
first = push (first);
printf("\n\nAfter the push operation the stack is as follows:\n");
display (first);
break;
}

case 'd':
{
first = pop (first);
printf("\n\nAfter the pop operation the stack is as follows:\n");
display (first);
break;
}

case 'q':
exit (0);
}
}while (ch != 'q');

getch ();
}

node *push(node *first)
{
node *data;
data = (node *)malloc(sizeof(node));
printf("\nEnter the data to be inserted: ");
scanf("%d",&data->info);
data->next = first;
first = data;
return (first);
}

node *pop (node *first)
{
node *temp;
if (first == NULL)
printf("\n\nSTACK IS EMPTY\n");
else
{
temp = (node *)malloc(sizeof(node));
temp = first;
first = first->next;
printf("\nThe element deleted is %d",temp->info);
free (temp);
}
return (first);
}

void display (node *first)
{
while (first != NULL)
{
printf("%d ",first->info);
first = first->next;
}
}

3 comments:

Anonymous said...

I am the sort of guy who loves to seek hot stuff. Right now I am building my own solar panels. I'm making it all alone without the aid of my staff. I'm using the net as the only path to acheive that. I saw a really awesome website that explains how to contruct pv panels and so on. The web site explains all the steps involved in photovoltaic panel building.

I am not sure bout how correct the info given there iz. If some people over here who have xp with these works can have a look and give your feedback in the site it will be awesome and I would really appreciate it, because I really like solar panel construction.

Tnx for reading this. U guys rock.

Anonymous said...

Amiable brief and this fill someone in on helped me alot in my college assignement. Thanks you seeking your information.

Anonymous said...


Farmers want that technology because it works and it's safe: Two decades of experience here has yielded no harm to people or the environment. The EU's food safety watchdog has given its OK for cultivation of more genetically modified crops. But politicians won't give their OK.

The irony is that while Europe keeps its farmers from growing genetically modified crops, its citizens consume them in rising abundance. Europe especially depends on bioengineered soybeans from abroad for the animal feed used to produce meat, milk and eggs. The same crops officials won't approve for planting routinely enter the continent via import.

Unless the Europeans change their ways, they will have to keep importing more from the U.S. and other [url=http://www.agoshow.net/2010-MLB-All-Star-25/]2010 MLB All Star[/url] foreign sources, because their hypocritical policies make their agricultural sector less productive than it should be. Consider the recent report from researchers in Spain and Britain that found Europe will be unable to produce enough food to meet its ag policy goals without genetically modified crops: "Ultimately, the EU will become almost entirely dependent on the outside world for food and feed and scientific progress," concluded researcher Paul Christou of the University of Lleida's Centre Agrotecnio in Spain.

The result of Europe's politically motivated rejection of modern methods is not a hungry Europe. Even in its economic malaise, Europe can afford plenty of food. But its policies hurt the needy in poor nations that would benefit from scaled-up European farm production: Europe's failure to grow as much food as it could, and instead boost imports, puts upward pressure on prices. High commodity prices make life more costly for those who spend a large share of their income on food.

Europeans compound the damage they do to the poor by discouraging the developing world from adopting genetically modified crops. European opponents of modern practices claim that embracing them would put poor farmers at the mercy of big companies such [url=http://www.agoshow.net/Phillies-34-Roy-Halladay-blue-2010-All-Star-Jerseys-64/]Phillies 34 Roy Halladay blue 2010 All Star Jerseys[/url] as Monsanto that sell state-of-the-art seed, fertilizer and pesticide. They also falsely claim the jury's out on whether genetically modified crops can increase yields.

Some of their scare tactics are terribly patronizing: The opponents suggest, for instance, that [url=http://www.agoshow.net/2009-All-Star-New-York-Yankees-42-Mariano-Rivera-Blue-Jerseys-20/]2009 All Star New York Yankees 42 Mariano Rivera Blue Jerseys[/url] farmers in the [url=http://www.agoshow.net/Mets-7-Jose-Reyes-blue-2010-All-Star-Jerseys-41/]Mets 7 Jose Reyes blue 2010 All Star Jerseys[/url] developing world should continue planting inferior seeds in the interest of promoting biodiversity. Those same vulnerable farmers should be content to lose part of their crop to weeds, the Luddites say, because using up-to-date technology could produce new strains resistant to weedkillers a manageable problem routinely exaggerated into a dire environmental threat.

An analysis by U.K.-based research [url=http://www.agoshow.net/Yankees-24-Robinson-Cano-Red-2010-All-Star-Jerseys-61/]Yankees 24 Robinson Cano Red 2010 All Star Jerseys[/url] firm PG Economics helps to explain why farmers embrace genetic engineering. Between 1996 and 2011, the technology has sharply boosted the incomes that farmers can earn from each planted acre. The result is greater economic and food security. Nearly every farmer who starts growing genetically modified crops decides to keep growing them.

Europe has fought this losing battle for too long. We hope upcoming free-trade talks with the U.S. allow common sense to trump baseless fears. Another of the world's leading economies needs to accept that bioengineering can safely help feed a hungry world.


[url=http://xi.purifying.info/memberlist.php?mode=viewprofile&u=710538]more[/url]