answersLogoWhite

0


Best Answer

I guess you mean operation top: return the topmost element without deleting it from the stack.

int top (const struct stack *from, stacktype *into)

{

if (from->elements==0) return -1; /* EMPTY */

*into = from->array[from->elements -1];

return 0; /* OK */

}

compare with pop:

int pop (struct stack *from, stacktype *into)

{

if (from->elements==0) return -1; /* EMPTY */

*into = from->array[from->elements -1];

--from->elements;

return 0; /* OK */

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is peep operation in stack using array in c?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is peep stack?

It is the space between the balls and the penis!!


What is stack define the operations of stack?

In modern computer languages, the stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation top (also known as peek and peak) can return the current top element of the stack without removing it from the stack. This section gives pseudocode for adding or removing nodes from a stack, as well as the length and top functions. Throughout we will use null to refer to an end-of-list marker or sentinel value, which may be implemented in a number of ways using pointers. In modern computer languages, the stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation top[1] (also known as peek and peak) can return the current top element of the stack without removing it from the stack. This section gives pseudocode for adding or removing nodes from a stack, as well as the length and top functions. Throughout we will use null to refer to an end-of-list marker or sentinel value, which may be implemented in a number of ways using pointers. record Node { data // The data being stored in the node next // A reference to the next node; null for last node } record Stack { Node stackPointer // points to the 'top' node; null for an empty stack } function push(Stack stack, Element element) { // push element onto stack new(newNode) // Allocate memory to hold new node newNode.data := element newNode.next := stack.stackPointer stack.stackPointer := newNode } function pop(Stack stack) { // increase the stack pointer and return 'top' node // You could check if stack.stackPointer is null here. // If so, you may wish to error, citing the stack underflow. node := stack.stackPointer stack.stackPointer := node.next element := node.data return element } function top(Stack stack) { // return 'top' node return stack.stackPointer.data } function length(Stack stack) { // return the amount of nodes in the stack length := 0 node := stack.stackPointer while node not null { length := length + 1 node := node.next } return length } As you can see, these functions pass the stack and the data elements as parameters and return values, not the data nodes that, in this implementation, include pointers. A stack may also be implemented as a linear section of memory (i.e. an array), in which case the function headers would not change, just the internals of the functions. Implementation A typical storage requirement for a stack of n elements is O(n). The typical time requirement of O(1) operations is also easy to satisfy with a dynamic array or (singly) linked list implementation. C++'s Standard Template Library provides a "stack" templated class which is restricted to only push/pop operations. Java's library contains a Stack class that is a specialization of Vector. This could be considered a design flaw because the inherited get() method from Vector ignores the LIFO constraint of the Stack. Here is a simple example of a stack with the operations described above (but no error checking) in Python. class Stack(object): def __init__(self): self.stack_pointer = None def push(self, element): self.stack_pointer = Node(element, self.stack_pointer) def pop(self): e = self.stack_pointer.element self.stack_pointer = self.stack_pointer.next return e def peek(self): return self.stack_pointer.element def __len__(self): i = 0 sp = self.stack_pointer while sp: i += 1 sp = sp.next return i class Node(object): def __init__(self, element=None, next=None): self.element = element self.next = next if __name__ == '__main__': # small use example s = Stack() [s.push(i) for i in xrange(10)] print [s.pop() for i in xrange(len(s))] The above is admittedly redundant as Python supports the 'pop' and 'append' functions to lists.


Two stacks in one array?

/*implementing two stacks in 1 array*/ #include #include #include int size,top1=-1,top2,a[100]; void push(); void pop(); void peep(); void main() { int i,ch; char c='y'; clrscr(); printf("\Enter size:"); scanf("%d",&size); top2=size; while(c=='y') { clrscr(); printf("\n ###### MENU #####\n"); printf("\n1: push"); printf("\n2: pop"); printf("\n3: peep"); printf("\n4: exit") ; printf("\n Enter choice:"); fflush(stdin); scanf("%d",&ch); switch(ch) { case 1:push(); break; case 2:pop(); break; case 3:peep(); break; case 4:exit(0); default:printf("\n wrong choice"); } printf("\n Another operation(y/n):"); fflush(stdin); scanf("%c",&c); } exit(0); getch(); } void push() { int item,ch; if(top1+1==top2) { printf("stack is full "); printf("\n cant push "); } else { printf("\nEnter item to be inserted:"); scanf("%d",&item); printf("\n enter choice(1 for stack1 else stack2):"); scanf("%d",&ch); if(ch==1) { top1++; a[top1]=item; } else { top2--; a[top2]=item; } } } void pop() { int ch; printf("\n Enter from which stack u want to pop(1 for stack1/else stack2):"); scanf("%d",&ch); if(ch==1) { if(top1==-1) printf("\n cant delete from stack 1 its empty"); else printf("\n item deleted is:%d" ,a[top1--]); } else { if(top2==size) printf("\n cant delete from s2its empty"); else printf("\n item deleted is: %d" ,a[top2++]); } } void peep() { int i; printf("\n stck 1:" ); for(i=0;i=top2;i--) printf(" %d",a[i]); }


What is a palindrome for a quick look?

Peep Eye


Examples of inherited from a chicken?

class Chicken {public:virtual~Chicken( ){ }virtual voidcall( ) = 0;};class Rooster : public Chicken {voidcall( ){ cout

Related questions

What is peep stack?

It is the space between the balls and the penis!!


Can you give me a sentence using the word peep?

Peep candies are very good.


How do you tie a peep sight?

Try using a little BoPeep knot.


What is stack define the operations of stack?

In modern computer languages, the stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation top (also known as peek and peak) can return the current top element of the stack without removing it from the stack. This section gives pseudocode for adding or removing nodes from a stack, as well as the length and top functions. Throughout we will use null to refer to an end-of-list marker or sentinel value, which may be implemented in a number of ways using pointers. In modern computer languages, the stack is usually implemented with more operations than just "push" and "pop". The length of a stack can often be returned as a parameter. Another helper operation top[1] (also known as peek and peak) can return the current top element of the stack without removing it from the stack. This section gives pseudocode for adding or removing nodes from a stack, as well as the length and top functions. Throughout we will use null to refer to an end-of-list marker or sentinel value, which may be implemented in a number of ways using pointers. record Node { data // The data being stored in the node next // A reference to the next node; null for last node } record Stack { Node stackPointer // points to the 'top' node; null for an empty stack } function push(Stack stack, Element element) { // push element onto stack new(newNode) // Allocate memory to hold new node newNode.data := element newNode.next := stack.stackPointer stack.stackPointer := newNode } function pop(Stack stack) { // increase the stack pointer and return 'top' node // You could check if stack.stackPointer is null here. // If so, you may wish to error, citing the stack underflow. node := stack.stackPointer stack.stackPointer := node.next element := node.data return element } function top(Stack stack) { // return 'top' node return stack.stackPointer.data } function length(Stack stack) { // return the amount of nodes in the stack length := 0 node := stack.stackPointer while node not null { length := length + 1 node := node.next } return length } As you can see, these functions pass the stack and the data elements as parameters and return values, not the data nodes that, in this implementation, include pointers. A stack may also be implemented as a linear section of memory (i.e. an array), in which case the function headers would not change, just the internals of the functions. Implementation A typical storage requirement for a stack of n elements is O(n). The typical time requirement of O(1) operations is also easy to satisfy with a dynamic array or (singly) linked list implementation. C++'s Standard Template Library provides a "stack" templated class which is restricted to only push/pop operations. Java's library contains a Stack class that is a specialization of Vector. This could be considered a design flaw because the inherited get() method from Vector ignores the LIFO constraint of the Stack. Here is a simple example of a stack with the operations described above (but no error checking) in Python. class Stack(object): def __init__(self): self.stack_pointer = None def push(self, element): self.stack_pointer = Node(element, self.stack_pointer) def pop(self): e = self.stack_pointer.element self.stack_pointer = self.stack_pointer.next return e def peek(self): return self.stack_pointer.element def __len__(self): i = 0 sp = self.stack_pointer while sp: i += 1 sp = sp.next return i class Node(object): def __init__(self, element=None, next=None): self.element = element self.next = next if __name__ == '__main__': # small use example s = Stack() [s.push(i) for i in xrange(10)] print [s.pop() for i in xrange(len(s))] The above is admittedly redundant as Python supports the 'pop' and 'append' functions to lists.


What is a sentence using the word voyeurism?

He was accused of voyeurism for secretly watching his neighbors through their windows.


What does a chicken sound like?

peep peep peep!


What is palindrome for what you see with?

Peep (refers to catching a quick sighting of something) eye (to look at something) sees


What are the ratings and certificates for The Wombles - 1973 Peep-Peep-Peep 1-6?

The Wombles - 1973 Peep-Peep-Peep 1-6 is rated/received certificates of: Australia:G


When was Bo Peep Bo Peep created?

Bo Peep Bo Peep was created on 2011-09-28.


Two stacks in one array?

/*implementing two stacks in 1 array*/ #include #include #include int size,top1=-1,top2,a[100]; void push(); void pop(); void peep(); void main() { int i,ch; char c='y'; clrscr(); printf("\Enter size:"); scanf("%d",&size); top2=size; while(c=='y') { clrscr(); printf("\n ###### MENU #####\n"); printf("\n1: push"); printf("\n2: pop"); printf("\n3: peep"); printf("\n4: exit") ; printf("\n Enter choice:"); fflush(stdin); scanf("%d",&ch); switch(ch) { case 1:push(); break; case 2:pop(); break; case 3:peep(); break; case 4:exit(0); default:printf("\n wrong choice"); } printf("\n Another operation(y/n):"); fflush(stdin); scanf("%c",&c); } exit(0); getch(); } void push() { int item,ch; if(top1+1==top2) { printf("stack is full "); printf("\n cant push "); } else { printf("\nEnter item to be inserted:"); scanf("%d",&item); printf("\n enter choice(1 for stack1 else stack2):"); scanf("%d",&ch); if(ch==1) { top1++; a[top1]=item; } else { top2--; a[top2]=item; } } } void pop() { int ch; printf("\n Enter from which stack u want to pop(1 for stack1/else stack2):"); scanf("%d",&ch); if(ch==1) { if(top1==-1) printf("\n cant delete from stack 1 its empty"); else printf("\n item deleted is:%d" ,a[top1--]); } else { if(top2==size) printf("\n cant delete from s2its empty"); else printf("\n item deleted is: %d" ,a[top2++]); } } void peep() { int i; printf("\n stck 1:" ); for(i=0;i=top2;i--) printf(" %d",a[i]); }


Palindrome of a chick's little noise?

peep


What is the palindrome for to look secretly or quickly?

peep