Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ C ++ implementation of the list of basic operations and test cases     - Installation under Linux to deploy Java (Linux)

- C language preprocessor command (Programming)

- GO five stages of language learning (Programming)

- Ubuntu Linux to create and increase the Swap partition tutorial (Linux)

- SSH security note (Linux)

- Linux and hardware (Linux)

- Linux configuration Samba server (Server)

- MySQL view (Database)

- Ubuntu install OpenMPI (Linux)

- Oracle archive log full cause abnormal slow database performance (Database)

- Difference Docker mirror and containers (Server)

- Security Knowledge: How to hide a backdoor PHP file tips (Linux)

- CentOS 6.6 install JDK7 (Linux)

- Security Knowledge: redirection command application security (Linux)

- Minimum period string problem (Programming)

- CentOS7 install and configure Nagios (Server)

- libreadline.so.6: can not open shared object file problem solution (Linux)

- Caffe install under Ubuntu 14.04 64bit (Linux)

- Configuring xdebug debugging environment in Ubuntu 14.04 under the PhpStorm (Linux)

- Java NIO in the world (Programming)

 
         
  C ++ implementation of the list of basic operations and test cases
     
  Add Date : 2018-11-21      
         
         
         
  Today, to achieve the basic operations under the list, including the creation of a node, the first plug tail plug, first delete the tail deleted, the first pass to find an intermediate node of the list, look for the list of the penultimate x node, delete headless list of non-end node, set against the list, as follows:

#include "SLinkList.h"
#include
#include
#include
void PrintList (SListNode * pHead) // print the list from the pointer position
{
while (pHead)
{
printf ( "->% d", pHead-> data);
pHead = pHead-> next;
}
printf ( " n");
}
static SListNode * _BuyNode (DataType x) // Create a new node
{
SListNode * ret = (SListNode *) malloc (sizeof (SListNode));
ret-> next = NULL;
ret-> data = x;
if (ret)
{
return ret;
}
printf ( "Failed to create node n"); // prompt creation failed
return NULL;
}
void PushBack (SListNode * & pHead, DataType x) // end plug
{
if (pHead == NULL)
{
pHead = _BuyNode (x);
}
else
{
SListNode * tail = pHead;
while (tail-> next)
{
tail = tail-> next;
}
tail-> next = _BuyNode (x);
}
}
void PopBack (SListNode * & pHead) // End deleted
{
if (pHead == NULL)
{
return;
}
else if (pHead-> next-> next == NULL)
{
free (pHead-> next);
pHead = NULL;
}
else
{
SListNode * tail = pHead;
while (tail-> next-> next)
{
tail = tail-> next;
}
free (tail-> next);
tail-> next = NULL;
}
}
void PushFront (SListNode * & pHead, DataType x) // plug head
{
if (pHead == NULL)
{
pHead = _BuyNode (x);
}
else
{
SListNode * tmp = _BuyNode (x);
tmp-> next = pHead;
pHead = tmp;
}
}
void PopFront (SListNode * & pHead) // t delete head
{
if (pHead == NULL)
{
return;
}
else
{
SListNode * tail = pHead;
pHead = pHead-> next;
tail-> next = NULL;
free (tail);
}
}
SListNode * Find (SListNode * pHead, DataType x) // x to find the node, if there is returned to the node, if it exists return empty
{
if (pHead == NULL)
{
return NULL;
}
SListNode * tail = pHead;
while (tail)
{
if (tail-> data == x)
{
return tail;
}
tail = tail-> next;
}
return NULL;
}
void Insert (SListNode * & pos, DataType x) // given a node is inserted after the position
{
SListNode * tmp = _BuyNode (x);
if (pos == NULL)
{
pos = tmp;
}
else
{
tmp-> next = pos-> next;
pos-> next = tmp;
}
}
void Erase (SListNode * & pos) // Delete the head of the list of non-end node
{
if (pos == NULL)
{
return;
}
else if (pos-> next == NULL)
{
printf ( "The node is tail node can not be deleted n");
}
else
{
SListNode * tmp = pos-> next;
pos-> data = pos-> next-> data;
pos-> next = pos-> next-> next;
free (tmp);
tmp = NULL;
}
}
void ReveseList (SListNode * & pHead) // the list of Retrograde
{
SListNode * tail = pHead;
while (tail-> next)
{
SListNode * tmp = NULL;
tmp = tail-> next;
tail-> next = tail-> next-> next;
tmp-> next = pHead;
pHead = tmp;
}
}
SListNode * FindminList (SListNode * pHead) // iteration of the loop to find the list of intermediate nodes
{
assert (pHead);
SListNode * quick = pHead;
SListNode * slow = pHead;
while (quick)
{
slow = slow-> next;
if (quick-> next)
quick = quick-> next-> next;
else
return slow;
}
return slow;
}
SListNode * FindListPosList (SListNode * pHead, int lastpos) // Find the list of the penultimate node lastpos
{
SListNode * quick = pHead;
SListNode * slow = pHead;
while (quick && lastpos--)
{
quick = quick-> next;
}
if (quick == NULL)
{
return slow;
}
while (quick)
{
quick = quick-> next;
slow = slow-> next;
}
return slow;
}

    Test case as follows:

1234567891011121314151617181920212223242526272829303132333435363738
3940414243444546474849505152535455565758596061626364656667686970717
2737475767778798081828384858687888990919293949596979899100101102103
104105 #include
#include
#include "SLinkList.h"
void Test1 () // PushBack, PrintList, PopBack
{
SListNode * pHead = NULL;
PushBack (pHead, 1);
PushBack (pHead, 2);
PushBack (pHead, 3);
PushBack (pHead, 4);
PushBack (pHead, 5);
PrintList (pHead);
PopBack (pHead);
PopBack (pHead);
PrintList (pHead);
PopBack (pHead);
PopBack (pHead);
PopBack (pHead);
PrintList (pHead);
}
void Test2 () // PushFront, Popfront
{
SListNode * pHead = NULL;
PushFront (pHead, 5);
PushFront (pHead, 4);
PushFront (pHead, 3);
PushFront (pHead, 2);
PushFront (pHead, 1);
PrintList (pHead);
PopFront (pHead);
PrintList (pHead);
PopFront (pHead);
PrintList (pHead);
PopFront (pHead);
PrintList (pHead);
PopFront (pHead);
PrintList (pHead);
PopFront (pHead);
PrintList (pHead);
PopFront (pHead);
PrintList (pHead);
}
void Test3 () // Find, Insert
{
SListNode * pHead = NULL;
PushFront (pHead, 5);
PushFront (pHead, 4);
PushFront (pHead, 3);
PushFront (pHead, 2);
PushFront (pHead, 1);
Insert (pHead, 0);
pHead = Find (pHead, 3);
PrintList (pHead);
Insert (pHead, 4);
PrintList (pHead);
}
void Test4 () // Erase
{
SListNode * pHead = NULL;
SListNode * tmp = NULL;
PushFront (pHead, 5);
PushFront (pHead, 4);
PushFront (pHead, 3);
PushFront (pHead, 2);
PushFront (pHead, 1);
tmp = Find (pHead, 5);
Erase (tmp);
PrintList (pHead);
}
void Test5 () // ReveseList
{
SListNode * pHead = NULL;
PushBack (pHead, 1);
PushBack (pHead, 2);
PushBack (pHead, 3);
PushBack (pHead, 4);
PushBack (pHead, 5);
    ReveseList (pHead);
PrintList (pHead);
}
void Test6 () // FindLastposList
{
SListNode * pHead = NULL;
SListNode * ret = NULL;
PushBack (pHead, 1);
PushBack (pHead, 2);
PushBack (pHead, 3);
PushBack (pHead, 4);
PushBack (pHead, 5);
    ret = FindListPosList (pHead, 2);
printf ( "% d n", ret-> data);
ret = FindListPosList (pHead, 6);
printf ( "% d n", ret-> data);
}
int main ()
{
Test1 ();
Test2 ();
Test3 ();
Test4 ();
Test5 ();
Test6 ();
system ( "pause");
return 0;
}
     
         
         
         
  More:      
 
- Based on a complete solution RMI service to transfer large files (Programming)
- Linux file compression and file system packaged with instructions (Linux)
- Applications in Objective-C runtime mechanism (Programming)
- C ++ free store and heap (Programming)
- RHEL / CentOS / Fedora Install Nagios 4.0.1 (Linux)
- To solve the Mac in question invalid BASH under configuration environment variable (Linux)
- JDK comes with tools JPS (Linux)
- Linux firewall anti-hacker disguise malicious attacks (Linux)
- GCC library link order problems (Programming)
- The virtual memory (Linux)
- Linux network security strategy (Linux)
- Simple to use multi-threaded programming under Linux mutex and condition variable (Programming)
- The source code compiler installation Nginx 1.8.0 under Ubuntu 14.10 (Server)
- Get the Linux device PCI ID method (Linux)
- How to install CentOS CentOS web page (Server)
- How to Upgrade Ubuntu GNOME 14.10 to GNOME 3.16 Desktop (Linux)
- Linux environment password security settings (Linux)
- Ceph Source Analysis: Network Module (Server)
- Single-node Hadoop installation notes distributed pseudo & (Server)
- Linux process or thread is bound to a CPU (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.