国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ 構造雙向鏈表的實現代碼

2020-01-26 16:03:21
字體:
來源:轉載
供稿:網友
構造雙向鏈表,不足之處,還望指正! 
復制代碼 代碼如下:

// DoubleLinkedList.cpp : 定義控制臺應用程序的入口點。
//構造雙向鏈表,實現從控制臺輸入,插入,刪除,求大小size等操作
#include "stdafx.h"
#include <iostream>
using namespace std;
//定義雙向鏈表的節點
template<class T>
struct NODE
{
 NODE<T>* pre;
 T data;
 NODE<T>* next;
};
//定義雙向鏈表容器
template<class T>
class DoubleLinkedList
{
public:
 DoubleLinkedList()
 {
  NODE<T>* q = new NODE<T>;
  if (q == NULL)
  {
   cout << "Fail to malloc the head node." << endl;
   return;
  }
  phead = q;
  phead->pre = NULL;
  phead->data = NULL;
  phead->next = NULL;

  T i;
  cout << "Please input several integer number, input ctrl+z to the end: " << endl;
  while (cin >> i)
  {
   NODE<T>* p = new NODE<T>;
   if (p == NULL)
   {
    cout << "Fail to malloc a new node." << endl;
    return;
   }
   p->data = i;
   q->next = p;
   p->pre = q;
   p->next = NULL;
   q = q->next;
  }
 }

 //容器大小方法
 int size()
 {
  NODE<T>* p = phead->next;
  int count(0);
  while (p != NULL)
  {
   count++;
   p = p->next;
  }
  return count;
 }

 //輸出DoubleLinkedLIst中的元素
 void print_elements()
 {
  NODE<T>* p = phead->next;
  while (p != NULL)
  {
   cout << p->data << " ";
   p = p->next;
  }
  cout << endl;
 }
 //插入一個元素到DoubleLinkedList中,任意合法位置插入
 void insert_element(int i, T e)
 {
   if (i <= this->size())
  {
   NODE<T>* m = phead;
   for (int j = 1; j < i; j ++)
   {
    m = m->next;
   }
   NODE<T>* n = m->next;
   NODE<T>* p = new NODE<T>;
   if (p == NULL)
   {
    cout << "Failed to malloc the node." << endl;
   }
   m->next = p;
   p->pre = m;
   p->data = e;
   p->next = n;
   n->pre = p;
  }
  else if (i == (this->size()+1))
  {
   NODE<T>* m = phead;
   for (int j = 1; j < i; j++)
   {
    m = m->next;
   }
   NODE<T>* p = new NODE<T>;
   if (p == NULL)
   {
    cout << "Failed to malloc the node." << endl;
   }
   m->next = p;
   p->pre = m;
   p->data = e;
   p->next = NULL;
  }
  else
  {
   cout << "Please input the position number equals or smaller than " << size()+1 << endl;
  }
 }

 //插入一個元素到DoubleLinkedList中,只在末尾插入
 void insert_element(T e)
 {
  NODE<T>* m = phead;
  for (int j = 1; j <= size(); j++)
  {
   m = m->next;
  }
  NODE<T>* p = new NODE<T>;
  if (p == NULL)
  {
   cout << "Failed to malloc the node." << endl;
  }
  m->next = p;
  p->pre = m;
  p->data = e;
  p->next = NULL;
 }

 //刪除DoubleLinkedList中的一個元素
 void delete_element(int i)
 {
  NODE<T>* p = phead;
  for (int j = 0; j < i; j ++)
  {
   p = p->next;
   if (p == NULL)
   {//所要刪除的元素超過list的范圍
    cout << "The size of the list is " << size() << " ,Please input the right number." << endl;
    return;
   }
  }
  if(p->next != NULL)
  {//刪除除最后一個以外的元素
   NODE<T>* m = p->pre;
   NODE<T>* n = p->next;
   m->next = n;
   n->pre = m;
   delete p;
  }
  else
  {//刪除元素為最后一個
   NODE<T>* m = p->pre;
   m->next = NULL;
   delete p;
  }
 }
private:
 NODE<T>* phead;
};
int _tmain(int argc, _TCHAR* argv[])
{
 //測試代碼
  DoubleLinkedList<int> mylist;
 mylist.print_elements();
 cout << "The size of the double linked list is : " << mylist.size() << endl;
 mylist.insert_element(1, 50);
 mylist.print_elements();
 mylist.insert_element(6, 80);
 mylist.print_elements();
 mylist.insert_element(250);
 mylist.print_elements();
 mylist.delete_element(7);
 mylist.print_elements();
 return 0;

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 怀安县| 磐安县| 崇州市| 邯郸市| 丰都县| 余江县| 克什克腾旗| 突泉县| 凯里市| 融水| 兴安盟| 洪洞县| 龙游县| 游戏| 彩票| 旬邑县| 信宜市| 龙岩市| 黄冈市| 丁青县| 利津县| 邮箱| 任丘市| 泸定县| 瓦房店市| 德化县| 谢通门县| 徐水县| 孙吴县| 重庆市| 景泰县| 锦州市| 平乡县| 永登县| 阳谷县| 沂源县| 桑植县| 左权县| 友谊县| 衡阳县| 广平县|