⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 例2_5.cpp

📁 李根强的数据结构(C++描述)这本书源代码 很有用的 呵呵
💻 CPP
字号:
//单链表的逆置
#include<iostream.h>
struct link
{   int  data; //元素类型
 link     *next;   //指针类型,存放下一个元素地址
};

void swaplink(link *head)//逆置单链表
{
  link *p,*s;
  p=head->next;
  head->next=NULL;
  while(p!=NULL)
  {
s=p->next;
p->next=head->next;
head->next=p;
p=s;
      }
   }

  
//头插法建立带头结点的单链表
link *hcreat( )
  { link *s,*p;
   int i;
   cout<<"输入结点数值,为0时算法结束";
   cin>>i;       
   p=new link;
   p->next=NULL;
   while(i)
   { s=new link;
     s->data=i;
     s->next=p->next;
     p->next=s;
     cin>>i;     }
return p;
}

//输出单链表
 void print(link *head)
 {  
 link *p;
    p=head->next;
    while(p->next!=NULL)
  { 
 cout<<p->data<<"->"; //输出表中非最后一个元素
p=p->next;
  }
  cout<<p->data;   //输出表中最后一个元素
 cout<<endl;
}

 void main( )
 { link *p;
 p=hcreat();
 cout<<"输出逆置前的结果"<<endl;
 print(p);
 swaplink(p);
 cout<<"输出逆置后的结果"<<endl;
 print(p);
 }
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -