设为首页 加入收藏

TOP

栈的链表实现 (一)
2014-11-23 21:42:18 来源: 作者: 【 】 浏览:10
Tags:实现

1)list.h


/*
 * list_2.cpp
 *
 *  Created on: 2013年8月2日
 *      Author: 黄东东
 *      为了能有章泽天这样的女朋友而不断努力。。。。。。
 */ 
 
#include   
 
using namespace std; 
 
typedef int T; 
 
class List { 
    struct Node { 
 
        T data; 
        Node* next; 
        Node(const T& d = T()) : 
                data(d), next(0) { 
 
        } 
 
    }; 
 
    Node* head; 
    int len; 
 
public: 
    List() : 
            head(NULL), len(0) { 
 
    } 
 
    ~List() { 
        clear(); 
    } 
 
    void insert(const T& d, int pos) { 
 
        Node*& pn = getptr(pos); 
        Node* p = new Node(d); 
        p->next = pn; 
        pn = p; 
        ++len; 
    } 
 
    void push_front(const T& d) { 
        insert(d, 0); 
    } 
 
    List& push_back(const T& d) { 
        insert(d, size()); 
 
        return *this; 
    } 
 
    void clear() { 
        Node* p; 
        while (head != NULL) { 
            p = head->next; 
            delete head; 
            head = p; 
        } 
    } 
 
    void erase(int pos) { 
        if (pos < 0 || pos >= size()) { 
            return; 
        } 
 
        Node*& pn = getptr(pos); 
        Node* p = pn; 
        pn = pn->next; 
        delete p; 
        --len; 
    } 
 
    void remove(const T& d) { 
        int pos; 
        while ((pos = find(d)) != -1) { 
            erase(pos); 
        } 
    } 
 
    void set( int pos,const T& d) { 
        if (pos < 0 || pos >= size()) { 
            return; 
        } 
 
        getptr(pos)->data = d; 
    } 
 
    Node*& getptr(int pos) { 
 
        if (pos < 0 || pos > size()) { 
            pos = 0; 
        } 
 
        if (pos == 0) { 
            return head; 
        } 
 
        Node* p = head; 
 
        for (int i = 1; i < pos; ++i) { 
            p = p->next; 
        } 
 
        return (*p).next; 
 
    } 
 
     int find(const T& d) { 
        Node* p = head; 
        int pos = 0; 
        while (p) { 
            if (p->data == d) { 
                return pos; 
            } 
 
            p = p->next; 
            pos++; 
        } 
 
        return -1; 
    } 
 
    const int& front() { 
        if (empty()) { 
            throw "空"; 
        } 
        return head->data; 
    } 
 
    int back() { 
        if (empty()) { 
            throw "空"; 
        } 
 
        Node* p = head; 
 
        while (p->next != NULL) { 
            p = p->next; 
        } 
 
        return (*p).data; 
    } 
 
    bool empty() { 
        return head == NULL; 
    } 
 
    int size() { 
        return len; 
    } 
 
    void travel() { 
        Node* p = head; 
 
        while (p != NULL) { 
 
            cout << p->data << ' '; 
            p = p->next; 
        } 
 
        cout<

using namespace std;

typedef int T;

class List {
 struct Node {

  T data;
  Node* next;
  Node(const T& d = T()) :
    data(d), next(0) {

  }

 };

 Node* head;
 int len;

public:
 List() :
   head(NULL), len(0) {

 }

 ~List() {
  clear();
 }

 void insert(const T& d, int pos) {

  Node*& pn = getptr(pos);
  Node* p = new Node(d);
  p->next = pn;
  pn = p;
  ++len;
 }

 void push_front(const T& d) {
  insert(d, 0);
 }

 List& push_back(const T& d) {
  insert(d, size());

  return *this;
 }

 void clear() {
  Node* p;
  while (head != NULL) {
   p = head->next;
   delete head;
   head = p;
  }
 }

 void erase(int pos) {
  if (pos < 0 || pos >= size()) {
   return;
  }

  Node*& pn = getptr(pos);
  Node* p = pn;
  pn = pn->next;
  delete p;
  --len;
 }

 void remove(const T& d) {
  int pos;
  while ((pos = find(d)) != -1) {
   erase(pos);
  }
 }

 void set( int pos,const T& d) {
  if (pos < 0 || pos >= size()) {
   return;
  }

  getptr(pos)->data = d;
 }

 Node*& getptr(int pos) {

  if (pos < 0 || pos > size()) {
   pos = 0;
  }

  if (pos == 0) {
   return head;
  }

  Node* p = head;

  for (int i = 1; i < pos; ++i) {
   p = p->next;
  }

  return (*p).next;

 }

     int find(const T& d) {
  Node* p = head;
  int pos = 0;
  while (p) {
   if (p->data == d) {
    return pos;
   }

   p = p->next;
   pos++;
  }

  return -1;
 }

 const int& front() {
  if (empty()) {
   throw "空";
  }
  return head->data;
 }

 int back() {
  if (empty()) {
   throw "空";
  }

  Node* p = head;

  while (p->next != NULL) {
   p = p->next;
  }

  return (*p).data;
 }

 bool empty() {
  return head == NULL;
 }

 int size() {
  return len;
 }

 void travel() {
  Node* p = head;

  while (p != NULL) {

   cout << p->data << ' ';
   p = p->next;
  }

  cout
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu1072 Nightmare (BFS) 下一篇HDU 2196 树形DP Computer

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)
·使用华为开发者空间 (2025-12-27 04:19:24)
·Getting Started wit (2025-12-27 03:49:24)
·Ubuntu 上最好用的中 (2025-12-27 03:49:20)