欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

用C++實(shí)現(xiàn)一個(gè)鏈?zhǔn)綏5膶?shí)例代碼

來源:本站原創(chuàng)|時(shí)間:2020-01-10|欄目:C語言|點(diǎn)擊: 次

自定義一個(gè)鏈?zhǔn)綏#琧++語言實(shí)現(xiàn),不足之處,還望指正!

復(fù)制代碼 代碼如下:

// MyStack.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//自己構(gòu)造一個(gè)鏈?zhǔn)綏?,具有push(入棧),pop(出棧),top(取得棧頂元素),size(返回棧大?。?,empty(判斷是否為空)等功能
#include "stdafx.h"
#include <iostream>
using namespace std;
//構(gòu)造棧的節(jié)點(diǎn)
template <class T>
struct NODE
{
 NODE<T>* next;
 T data;
};
template <class T>
class MyStack
{
public:
 MyStack()
 {
  phead = new NODE<T>;
  if (phead == NULL)
  {
   cout << "Failed to malloc a new node. " << endl;
  }
  else
  {
   phead->data = NULL;
   phead->next = NULL;
  }
 }

 //入棧
 void push(T e)
 {
  NODE<T>* p = new NODE<T>;
  if (p == NULL)
  {
   cout << "Failed to malloc a new node. " << endl;
  }
  else
  {
   p->data = e;
   p->next = phead->next;
   phead->next = p;
  }
 }

 //出棧
 T pop()
 {
  T e;
  NODE<T>* p = phead->next;
  if(p != NULL)
  {
   phead->next = p->next;
   e = p->data;
   delete p;
   return e;
  }
  else
  {
   cout << "There is no elements in the stack." << endl;
   return NULL;
  }
 }

 //取得棧頂元素
 T top()
 {
  T e;
  NODE<T>* p = phead->next;
  if (p != NULL)
  {
   e = p->data;
   return e;
  }
  else
  {
   cout << "There is no elements in the stack." << endl;
   return NULL;
  }
 }
 //取得棧中元素個(gè)數(shù)
 int size()
 {
  int count(0);
  NODE<T>* p = phead->next;
  while (p != NULL)
  {
   p = p->next;
   count++;
  }
  return count;
 }
 //判斷stack是否為空
 bool empty()
 {
  NODE<T>* p = phead;
  if (p->next == NULL)
  {
   return true;
  }
  else
  {
   return false;
  }
 }
private:
 NODE<T>* phead;
};
int _tmain(int argc, _TCHAR* argv[])
{
 MyStack<int> sta;
 sta.push(1);
 sta.push(2);
 sta.push(3);
 cout << "The size of the stack now is " << sta.size() << endl;
 sta.pop();
 cout << "The top element is " << sta.top() << endl;
 cout << "The size of the stack now is" << sta.size() << endl;
 if (sta.empty())
 {
  cout << "This stack is empty." << endl;
 }
 else
 {
  cout << "This stack is not empty." << endl;
 }
 return 0;
}

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時(shí)內(nèi)進(jìn)行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有