asp.net中鏈表c++執行個體程式

來源:互聯網
上載者:User
 代碼如下 複製代碼

#include <iostream.h>
//   #define NULL 0
class Node //節點類
{
public:
 Node(){}
 Node(int n){ num=n; next=NULL; }
 Node(int n, Node *p){ num=n; next=p;}
 void setNum(int n) {num =n;}
 void setNext(Node *p){next =p;}
 
 int getNum(){return num;}
 Node *getNext(){ return next;}
private:
 int num;
 Node *next;
};
class Linklist //鏈表類
{
public:
 Linklist(){head=NULL;}
 Linklist(int n){ head = new Node(n); }
 void addAtEnd(int n);
 void addAtEnd(Node *p);
 void visitAllNode();
 void addBeforeNum(Node *p, int num); //在節點值為num之前添加新節點p
 void deleteNum(int num);//刪除值為num的節點
private:
 Node *head;
};
void Linklist::addAtEnd(int n)  //往鏈表尾添加節點
{
 Node *p=head;
 if(head==NULL){ head = new Node(n);}
 else
 {
  while(p->getNext()!=NULL)
  { p= p->getNext(); }//用p來指向最後一個節點
  p->setNext(new Node(n));//插入節點
 }
 
}
void Linklist::addAtEnd(Node *p) //往鏈表尾添加節點
{
 Node *pn=head;
 if(head==NULL) { head=p; }
 else
 {
  while(pn->getNext()!=NULL)
  { pn=pn->getNext(); }//用pn來指向最後一個節點
  pn->setNext(p);//插入節點
 }
 
}
void Linklist::visitAllNode()//遍曆鏈表
{
 Node *p;
 p=head;
 if(p==NULL){cout<<"空鏈表!"<<endl;}
 else
 {
  cout<<"表頭: ";
  while(p->getNext()!=NULL)
  {
   cout<<p->getNum()<<" --> ";
   p=p->getNext();
  }
  cout<<p->getNum()<<" 表尾"<<endl;
 }
}
void Linklist::addBeforeNum(Node *p, int num) //在節點值為num之前添加新節點p
{
 Node *pn=head,*follow=pn;
 if(pn==NULL)
 {
  cout<<"空鏈表!插入節點。";
  head= p;
  return;
 }
 while(pn->getNext() != NULL && pn->getNum() != num)
 // 尋找指向節點值為num的節點
 {
  follow = pn; //follow一直指向pn前的一個節點
  pn = pn->getNext();
 }
 if(pn==head && pn->getNum() == num)//找到節點,且為頭指標
 {
  p->setNext(pn);
  head=p;
 }
 else if(pn->getNum() ==  num) //找到此節點,註:follow指向其上一節點
 {
  p->setNext(pn);
  follow->setNext(p);
 }
 else //沒有找到節點,pn指向最後一個節點,將新節點插入鏈表尾
 {
  cout<<"沒有找到,插入到鏈表尾。"<<endl;
  pn->setNext(p);
 }
 
}
void Linklist::deleteNum(int num) //刪除節點
{
 Node *p=head,*follow=p;
 if(p->getNum() == num)//若刪除的節點為第1個節點
 {
  head = p->getNext();
  delete p;
 }
 else
 {
  while(p->getNext()!=NULL && p->getNum() != num)
  {
   follow = p;//follow一直指向p的前一個節點
   p = p->getNext();
  }
  if(p->getNum() == num)
  {
   follow->setNext(p->getNext());
   delete p;
  }
 
 }
}
void main()
{
 Linklist *pl=NULL;
 int num, n;
 Node *p;
 char option;
 while(1)
 {
  cout<<"\n鏈表操作練習:"<<endl;
  cout<<"1: 建立鏈表"<<endl;
  cout<<"2: 在鏈表尾插入新節點"<<endl;
  cout<<"3: 在節點值為num的節點前插入新節點"<<endl;
  cout<<"4: 刪除節點"<<endl;
  cout<<"5: 遍曆節點"<<endl;
  cout<<"0: 退出"<<endl;
 
  cout<<"\n請輸入你的選擇:";
  cin>>option;
 
  switch(option)
  {
  case '0':
   break;
  case '1':
   cout<<"請輸入第一個節點值"<<endl;
   cin>>num;
   pl = new Linklist(num);
   pl->visitAllNode();
   break;
  case '2':
   cout<<"請輸入節點值:";
   cin>>num;
   if(pl==NULL){ pl = new Linklist(num);}
   else
   {
    pl->addAtEnd(num); //pl->addAtEnd(new Node(num));
   }
   pl->visitAllNode();
   break;
  case '3':
   cout<<"請輸入插入的節點值:";
   cin>>num;
   cout<<"請輸入要插入到哪個節點前,以節點的值表示:";
   cin>>n;
   pl->addBeforeNum(new Node(num), n);
   pl->visitAllNode();
   break;
  case '4':
   cout<<"請輸入要刪除的節點的值:";
   cin>>num;
   pl->deleteNum(num);
   pl->visitAllNode();
   break;
  case '5':
   pl->visitAllNode();
   break;
  default:
   cout<<"你輸入錯誤,請重新輸入!"<<endl;
  }
  if(option == '0' )
   break;
 }
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.