Implementation of queue-type chained storage and stack-type chained storage in C + + code example _c language

Source: Internet
Author: User
Tags int size

Queue-type chained storage

Code:
Linkqueue.hpp

Queue class #pragma once #include "linklist.hpp" template <typename t> class Linkqueue {Public:linkqueu 
  E (); 
~linkqueue (); 
  Public:int Clear (); 
  int append (T &t); 
  int Retieve (T &t); 
  int header (T &t); 
int length (); 
Protected:linklist<t> *m_list; 
 
}; 
Template <typename t> linkqueue<t>::linkqueue () {m_list = new linklist < T >; 
  } template <typename t> Linkqueue<t>::~linkqueue () {clear (); 
  Delete m_list; 
M_list = NULL; 
  Template <typename t> int linkqueue<t>::clear () {T t; 
  while (M_list->getlen () > 0) {m_list->del (0, T); 
return 0; Template <typename t> int linkqueue<t>::append (t &t) {return M_list->insert (T, M_list->ge 
Tlen ()); Template <typename t> int linkqueue<t>::retieve (T &t) {return M_list->del ( 
)-1, T); } template <typename t> int Linkqueue<t>::header (t &t) {return m_list->get (0, T); 
Template <typename t> int linkqueue<t>::length () {return M_list->getlen (); 
 }

Main.cpp

Queue class Test program 
 
#include <iostream> 
#include <cstdio> 
#include "linkqueue.hpp" 
 
using namespace Std; 
 
struct Student 
{ 
  char name[32]; 
  int age; 
}; 
 
void Play () 
{ 
  Student s1, S2, S3; 
  S1.age =; 
  S2.age =; 
  S3.age = n; 
 
  Linkqueue<student> LQ; Create Queue 
  lq.append (S1);//Enter queue 
  lq.append (S2); 
  Lq.append (S3); 
 
  Student tmp; 
  Lq.header (TMP); 
  cout << "header of queue:" << tmp.age << Endl; 
  cout << "Length of queue:" << lq.length () << Endl; 
 
  while (Lq.length () > 0) { 
    lq.retieve (TMP); 
    cout << tmp.age << ""; 
  } 
  cout << Endl; 
 
  Lq.clear (); 
 
} 
 
int main () 
{play 
  (); 
 
  return 0; 
} 


Stack-type chained storage

Linkstack.hpp

Stack class #pragma once #include "linklist.hpp" template <typename t> class Linkstack {Public:linkstack 
  (); 
~linkstack (); 
  Public:int Clear (); 
  int push (T &t); 
  int pop (T &t); 
  int Top (T &t); 
int size (); 
Protected:linklist<t> *m_list; 
 
}; 
Template <typename t> Linkstack<t>::linkstack () {m_list = new linklist < T >; 
  } template <typename t> Linkstack<t>::~linkstack () {clear (); 
  Delete m_list; 
M_list = NULL; 
  Template <typename t> int linkstack<t>::clear () {T t; 
  while (M_list->getlen () > 0) {m_list->del (0, T); 
return 0; 
Template <typename t> int linkstack<t>::p ush (t &t) {return M_list->insert (t, 0); 
Template <typename t> int linkstack<t>::p op (t &t) {return M_list->del (0, T); Template <typename t> int linkstack<t>::top (T &t) {return m_list-&Gt;get (0, T); 
Template <typename t> int linkstack<t>::size () {return M_list->getlen (); 
 }

Main.cpp

Chain Storage Stack class test program 
 
#include <iostream> 
#include <cstdio> 
#include "linkstack.hpp" 
 
using namespace Std; 
 
struct Student 
{ 
  char name[32]; 
  int age; 
}; 
 
void Play () 
{ 
  Student s1, S2, S3; 
  S1.age =; 
  S2.age =; 
  S3.age = n; 
 
  Linkstack<student> ls; Create stack 
 
  //into Stack 
  ls.push (S1); 
  Ls.push (S2); 
  Ls.push (S3); 
 
  Gets the stack top element 
  Student tmp; 
  Ls.top (TMP); 
  cout << "Top of Stack:" << tmp.age << Endl; 
  cout << "Size of Stack:" << ls.size () << Endl; 
 
  Out Stack while 
  (ls.size () > 0) { 
    ls.pop (TMP); 
  } 
 
  Ls.clear (); 
 
} 
 
int main () 
{play 
  (); 
 
  return 0; 
} 

Linklist.h

List #pragma once #include <iostream> #include <cstdio> using namespace std; 
  Template <typename t> struct Node {T t; 
Node<t> *next; 
 
}; 
  Template <typename t> class Linklist {public:linklist (); 
 
~linklist (); 
  Public:int Clear (); 
  int Insert (T &t, int pos); 
  int get (int pos, T &t); 
  int del (int pos, T &t); 
 
int Getlen (); 
  Protected:node<t> *header; 
int length; 
 
}; 
  Template <typename t> linklist<t>::linklist () {header = new Node < T >; 
  Header->next = NULL; 
length = 0; 
 
  } template <typename t> linklist<t>::~linklist () {node<t> = NULL; 
    while (header) {tmp = header->next; 
    Delete header; 
  Header = tmp; 
  } template <typename t> int linklist<t>::clear () {~linklist (); 
  Linklist (); 
return 0; Template <typename t> int linklist<t>::insert (T &t, int pOS) {node<t> *cur = NULL; 
  Fault-tolerant processing of POS if (pos >= length) {pos = length; 
  } cur = header; 
  for (int i = 0; i < POS; ++i) {cur = cur->next; 
  ///cache top-Applied t-nodes in container node<t> *node = new Node < T >; 
  Node->next = NULL; Node->t = t; 
  Cache T in container Node->next = cur->next; 
 
  Cur->next = node; 
 
  ++length; 
return 0; 
 
  Template <typename t> int linklist<t>::get (int pos, T &t) {node<t> *cur = NULL; 
  if (pos >= length) {return-1; 
  } cur = header; 
  for (int i = 0; i < POS; ++i) {cur = cur->next; } t = cur->next->t; 
Assigning the node of POS position to T return 0; 
 
  Template <typename t> int linklist<t>::d el (int pos, T &t) {node<t> *cur = NULL; 
  if (pos >= length) {return-1; 
  } cur = header; 
  for (int i = 0; i < POS; ++i) {cur = cur->next; } node<t> *ret = NULL 
  RET = cur->next; t = ret->t; 
  Apply the cached node to the upper layer t//delete operation Cur->next = ret->next; 
  --length; Delete ret; 
Note free memory because the new node<t> return 0 when insert; 
Template <typename t> int Linklist<t>::getlen () {return length;  }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.