找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 5148|回复: 0

[原]第2章 线性表(4)

[复制链接]
发表于 2012-1-27 21:15:10 | 显示全部楼层 |阅读模式
C++实现一个单向链表://Node.h
  1. #pragma once
  2. #include "stdafx.h"
  3. namespace Alexsoft
  4. {
  5.         template<class T>
  6.         class Node
  7.         {
  8.         public:
  9.                 T data;
  10.                 Node<T> *next;
  11.                 Node(T val,Node<T> *p){
  12.                         data=val;
  13.                         next=p;
  14.                 }
  15.                 Node(T val){
  16.                         data=val;
  17.                         next=0;
  18.                 }
  19.                 Node(){
  20.                         data=0;
  21.                         next=0;
  22.                 }
  23.         };
  24. }//LinkNode.h
  25. #pragma once
  26. #include "stdafx.h"
  27. #include "Node.h"
  28. #include <iostream>
  29. using std::cout;
  30. namespace Alexsoft
  31. {
  32.         template<class T>
  33.         class LinkList{
  34.         public:
  35.                 Node<T> *head;
  36.                 LinkList(){
  37.                         head=0;
  38.                 }
  39.                 LinkList(Node<T> &node){
  40.                         head=&node;
  41.                 }
  42.                 int GetLength(){
  43.                         Node<T> *p=head;
  44.                         int len=0;
  45.                         while(p!=0){
  46.                                 ++len;
  47.                                 p=p->next;
  48.                         }
  49.                         return len;
  50.                 }
  51.                 void Clear(){
  52.                         head=0;
  53.                 }
  54.                 bool IsEmpty(){
  55.                         if(head==0)
  56.                                 return true;
  57.                         else
  58.                                 return false;
  59.                 }
  60.                 void Append(T item){
  61.                         Node<T> *q=new Node<T>(item);
  62.                         if(head==0)
  63.                                 head=q;
  64.                         else{
  65.                                 Node<T> *p=head;
  66.                                 while(p->next!=0){
  67.                                         p=p->next;
  68.                                 }
  69.                                 p->next=q;
  70.                         }
  71.                 }
  72.                 void Insert(T item, int i){
  73.                         if(IsEmpty()||i<0)
  74.                                 cout<<"List is empty or Position is error!";
  75.                         Node<T> *q=new Node<T>(item);
  76.                         if(i==0){
  77.                                 q->next=head;
  78.                                 head=q;
  79.                                 return;
  80.                         }
  81.                         else{
  82.                                 Node<T> *p=head;
  83.                                 Node<T> *post=p;
  84.                                 int index=0;
  85.                                 while(index<i&&p!=0){
  86.                                         post=p;
  87.                                         p=p->next;
  88.                                         ++index;
  89.                                 }
  90.                                 if(index==i){
  91.                                         q->next=post->next;
  92.                                         post->next=q;
  93.                                 }
  94.                         }
  95.                 }
  96.                 T Delete(int i){
  97.                         if(IsEmpty()||i<0){
  98.                                 cout<<"Link is empty or position is error!";
  99.                                 return T();
  100.                         }
  101.                        
  102.                         if(i==0){
  103.                                 Node<T> *q=head;
  104.                                 head=head->next;
  105.                                 return q->data;
  106.                         }
  107.                         else{
  108.                                 int index=0;
  109.                                 Node<T> *p=head;
  110.                                 Node<T> *post;
  111.                                 while(p->next!=0&&index<i){
  112.                                         ++index;
  113.                                         post=p;
  114.                                         p=p->next;
  115.                                 }
  116.                                 if(index==i){
  117.                                         post->next=p->next;
  118.                                         return p->data;
  119.                                 }
  120.                                 else{
  121.                                         cout<<"The 1th node is not exist!";
  122.                                         return T();
  123.                                 }
  124.                         }
  125.                 }
  126.                 T GetItem(int i){
  127.                         if(IsEmpty()){
  128.                                 cout<<"List is empty";
  129.                                 return T();
  130.                         }
  131.                         Node<T> *p=head;
  132.                         int j=0;
  133.                         while(p->next!=0&&j<i){
  134.                                 ++j;
  135.                                 p=p->next;
  136.                         }
  137.                         if(j==i){
  138.                                 return p->data;
  139.                         }
  140.                         else{
  141.                                 cout<<"The target node is not exist!";
  142.                                 return T();
  143.                         }
  144.                 }
  145.                 int Locate(T value){
  146.                         if(IsEmpty()){
  147.                                 cout<<"List is Empty!";
  148.                                 return -1;
  149.                         }
  150.                         Node<T> *p=head;
  151.                         int i=0;
  152.                         while(p->data!=value&&p->next!=0){
  153.                                 p=p->next;
  154.                                 ++i;
  155.                         }
  156.                         if(p->data==value){
  157.                                 return i;
  158.                         }
  159.                         else{
  160.                                 cout<<"Not found";
  161.                                 return -1;
  162.                         }
  163.                 }
  164.         };
  165. }
  166. Verify Codes:
  167. // CCA.cpp : Defines the entry point for the console application.
  168. //
  169. #include "stdafx.h"
  170. #include "Algri.h"
  171. #include <iostream>
  172. #include "LinkNode.h"
  173. using namespace std;
  174. using namespace Jeffery;
  175. using namespace Alexsoft;
  176. int _tmain(int argc, _TCHAR* argv[])
  177. {
  178.         LinkList<int> li= LinkList<int>();
  179.         for(int i=0;i<8;i++)
  180.         {
  181.                 li.Append(i);
  182.         }
  183.         Node<int> *node=li.head;
  184.         do
  185.         {
  186.                 cout<<node->data<<" ";
  187.                 node=node->next;
  188.         }while(node!=0);
  189.         cout<<endl;
  190.         li.Insert(100,5);
  191.         node=li.head;
  192.         do
  193.         {
  194.                 cout<<node->data<<" ";
  195.                 node=node->next;
  196.         }while(node!=0);
  197.         cout<<endl;
  198.         li.Insert(1000,9);
  199.         node=li.head;
  200.         do
  201.         {
  202.                 cout<<node->data<<" ";
  203.                 node=node->next;
  204.         }while(node!=0);
  205.         cout<<endl;
  206.         li.Delete(2);
  207.         node=li.head;
  208.         do
  209.         {
  210.                 cout<<node->data<<" ";
  211.                 node=node->next;
  212.         }while(node!=0);
  213.         cout<<endl;
  214.         cout<<li.GetItem(7)<<endl;
  215.         cout<<li.Locate(4)<<endl;
  216.         return 0;
  217. }
复制代码

作者:xufei96 发表于2012-1-17 15:45:01 原文链接


您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-5-2 09:42 , Processed in 0.017984 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表