Fundamentals of Algorithms and Data Structures 3: implementation of a simple single-linked list class

Source: Internet
Author: User

Simple single-linked list implementation, the data type is defined as an int, if you want to be generic, you need to change to a template class, and then slightly modified to the next.

List.h

#include <cstdio> #include <cassert> #include <iostream>using namespace Std;class list{public://* * * * * Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor/* * * * * List (); List (const list &list); list& operator= (const List &list); ~list ();//************************************************************* Additions and deletions//**************************************************************************void PushFront ( const int& data); int popfront (); void pushback (const int& data); int popback (); void reverse (); unsigned short size ( ), bool IsEmpty (), void Printlist ();p rivate:void free ();p rivate:typedef struct node{node (int d):d ata (d), next (NULL) {} int data;struct node* Next;} Node; node* M_head; node* m_tail;/* This variable adds a bit of chicken, originally wanted to use to manipulate the end of the chain, found that only when the insertion can be used, delete the time is not used, because there is no previous pointer */unsigned short m_size;};/ / *****************************************************************Private method//**************************************************************************void List::Free () {if (M_head) {node* tmp = Null;while (m_head) {tmp = M_head;m_head = M_head->next;delete tmp;}} M_head = Null;m_tail = Null;m_size = 0;} Four functions of a class: constructor, copy constructor, overloaded assignment operator, destructor/ /**************************************************************************list::list () {m_head = NULL;m_tail = null;m_size = 0;} List::list (const List &list) {m_head = Null;m_tail = Null;m_size = List.m_size;if (list.m_head) {node* tmp = List.m_head ; node* q = null;while (tmp) {node* node = new node (tmp->data), if (!m_head) {m_head = Node;q = M_head;} Else{q->next = node;q = node;} TMP = Tmp->next;} M_tail = q; }else{m_head = Null;m_tail = NULL;}} list& list::operator= (const List &list) {free (); m_size = List.m_size;if (list.m_head) {node* tmp = List.m_head; node* q = null;while (tmp) {node* node = new node (tmp->data), if (!m_head) {m_Head = Node;q = M_head;} Else{q->next = node;q = node;} TMP = Tmp->next;} M_tail = q; }else{m_head = Null;m_tail = NULL;} return *this;} List::~list () {if (m_head) {node* TMP = M_head;while (tmp) {m_head = M_head->next;delete tmp;tmp = M_head;}} M_head = Null;m_tail = NULL;} Additions/deletions//*************************** void List::P ushfront (const int& data) {node* TMP = new Node (data); if (!m_head) {m_head = Tmp;m_tail = tmp;} Else{tmp->next = M_head;m_head = tmp;} ++m_size;} int List::P opfront () {assert (M_size > 0); node* tmp = M_head;m_head = M_head->next;int val = Tmp->data;delete tmp;--m_size;return val;} void List::P ushback (const int& data) {node* TMP = new Node (data); M_head? (M_tail->next = tmp): (m_head = tmp); m_tail = Tmp;++m_size;} int List::P opback () {assert (M_size > 0); int val = 0; Node *cur, *pri;if (!m_head->next) {val = M_head->data;delete M_head;m_head = Null;m_tail = Null;m_size = 0;} Else{cur = M_head;while (cur->next->next) {cur = cur->next;} pri = Cur;m_tail = Cur;cur = Cur->next;pri->next = Null;val = Cur->data;delete cur;cur = NULL;--m_size;} return Val;} void List::reverse () {//list is empty or only one element, return directly if (!m_head | |!m_head->next) {return;} Node *pri, *cur, *tmp;pri = m_head;cur = M_head->next;m_head->next = Null;while (cur) {tmp = Cur->next;cur->nex t = Pri;pri = Cur;cur = tmp;} M_head = pri;} unsigned short list::size () {return m_size;} BOOL List::isempty () {return (m_size = = 0);} void List::p rintlist () {cout << "size:" << m_size << ", content:"; if (m_head) {node* tmp = M_head;whi LE (TMP) {cout << tmp->data << ", tmp = Tmp->next;}} cout << "NULL" << Endl;}

Main.cpp

#include "List.h" #include <cstdlib> #include <iostream> #include <list>using namespace Std;int main () {List list;for (int i = 1; i < 5; ++i) {list. Pushfront (i);} List.printlist (); list. Pushback (0); List.printlist (); List listcopy (list); Listcopy.printlist (); List Listassig;listassig = List;listassig.printlist (); int popfront = list. Popfront (); cout << "Popfront:" << popfront << endl;list.printlist (); int popback = list. Popback (); cout << "Popback:" << popback << endl;list.printlist (); List.reverse (); cout << "Do Reverse "<< endl;list.printlist (); System (" pause "); return 0;}

Output:



There are some features not implemented, such as the insertion of elements in the specified location, linked list search, etc., because there is no iterator, with index do not be too good, simply do not write forget, later use template, write a generic.

Based on a single linked list, you can implement the queue, stack, and so on, a little extension can be implemented double-linked list, will be implemented in this one, post it.




Fundamentals of Algorithms and Data Structures 3: implementation of a simple single-linked list class

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.