Exercises4.2 And Exercises4.3

来源:互联网 发布:淘宝一淘官方网站 编辑:程序博客网 时间:2024/05/16 15:34

Exercises 4.2

E2 Consider a linked stack that includes a method size .That method size requires a loop that moves through the entire stack to count the entires,since the number of entries in the stack is not kept as a separate member in the stack record.

(a)Write a method size for a linked stack by using a loop that moves a pointer variable from node to node to mode to node through the stack.

int Stack ::size()const

/Post: Return the number of entries in the Stack.*/

{

  Node *temp=top_node;

  int count=0;

  while{temp!=NULL){

temp=temp->next;

Count ++;

}

Return count;

}

Exercise 4.3E2 

Void Stack::operator=(const Stack &original)

/Post:The Stack is reset as a copy of Stack original.*/

{

  Stack new_copy(original);

  Node *temp=top_node;

  top_node=new_copy.top_node;

  new_copy.top_node=temp;

}

原创粉丝点击