[C++] c++ 链表的建立 插入 删除 →→→→→进入此内容的聊天室

来自 , 2019-06-23, 写在 C++, 查看 105 次.
URL http://www.code666.cn/view/1f1baa5b
  1. #include "stdafx.h"
  2. #include <iostream>
  3. using namespace std;
  4. //C++动态链表的建立
  5. class Book
  6. {
  7. public:
  8.         int num;
  9.         int price;
  10.         Book *next;
  11. };
  12. Book *p;
  13. //创建头结点返回头结点地址给程序调用
  14. Book* CreateHead()
  15. {
  16.         Book *head,*p1,*p2;//定义三个指向Book的类,返回值给head
  17.         p1=new Book;
  18.         head=p1;
  19.         p2=p1;//将三个结点一起在椎中创建
  20.         cout<<"请输入图书编号,以0结束"<<endl;
  21.         cin>>p1->num;
  22.         if ( p1->num!=0 )
  23.         {
  24.                 cout<<"请输入图书价格"<<endl;
  25.                 cin>>p1->price;
  26.         }
  27.         else
  28.         {
  29.                 delete p1;
  30.                 p2=NULL;
  31.                 return head;
  32.         }
  33.         while ( p1->num!=0 )
  34.         {
  35.                 p2=p1;//这步很重要,意思为将P2设为当前结点,然后用p1继续创建,用培p2->next指向p1
  36.                 p1=new Book;
  37.                 cout<<"请输入图书编号,以0结束"<<endl;
  38.                 cin>>p1->num;
  39.                 if ( p1->num!=0 )
  40.                 {
  41.                         cout<<"请输入图书价格"<<endl;
  42.                         cin>>p1->price;
  43.                 }
  44.                 p2->next=p1;
  45.         }
  46.         delete p1;
  47.         p2->next=NULL;
  48.         return head;
  49. }
  50. void Show ( Book *head )
  51. {
  52.         while ( head!=NULL )
  53.         {
  54.                 cout<<"图书编号为:"<<head->num<<",价格为:"<<head->price<<endl;
  55.                 head=head->next;
  56.         }
  57. }
  58.  
  59. //删除链表某个结点
  60. void Delete ( Book *p,int num )
  61. {
  62.         Book *temp;
  63.         if ( p->num==num )
  64.         {
  65.                 temp=p;
  66.                 p=p->next;
  67.                 ::p=p;
  68.                 delete temp;
  69.                 return;
  70.         }
  71.         while ( p )
  72.         {
  73.                 if ( p->next==NULL )
  74.                 {
  75.                         cout<<"null"<<endl;
  76.                         return;
  77.                 }
  78.                 if ( p->next->num==num )
  79.                 {
  80.                         temp=p->next;
  81.                         p->next=temp->next;
  82.                         delete temp;
  83.                         cout<<"ok"<<endl;
  84.                         return;
  85.                 }
  86.                 p=p->next;
  87.         }
  88.         cout<<"null"<<endl;
  89. }
  90.  
  91. //链表的添加(这里假设直接添加到最尾段)
  92. void Insert ( Book *p,int num,int price )
  93. {
  94.         Book *temp=new Book;//这个结点用于添加编号和价格
  95.         Book *tem=new Book;//这个结点用于存放尾结点地址
  96.         while ( p )
  97.         {
  98.                 tem=p;
  99.                 p=p->next;
  100.         }
  101.         temp->num=num;
  102.         temp->price=price;
  103.         tem->next=temp;
  104.         temp->next=NULL;
  105.  
  106. }
  107. int _tmain ( int argc, _TCHAR* argv[] )
  108. {
  109.         p=CreateHead();
  110.         Show ( p );
  111.         cout<<endl;
  112.         cout<<"请输入要删除的编号"<<endl;
  113.         int num;
  114.         cin>>num;
  115.         Delete ( p,num );
  116.         Show ( p );
  117.         cout<<"请输入编号"<<endl;
  118.         cin>>num;
  119.         int price;
  120.         cout<<"请输入价格"<<endl;
  121.         cin>>price;
  122.         Insert ( p,num,price );
  123.         Show ( p );
  124.         system ( "pause" );
  125.         return 0;
  126. }

回复 "c++ 链表的建立 插入 删除"

这儿你可以回复上面这条便签

captcha