The basic operation of a single list c ++

#include<iostream>
using namespace std;

List class
{
public:
List () {create_list ();}
~ List () {Clear ();}
void create_list ();
// Insert a node list from the tail
void the Add (const int & D);
// insert a scratch node
void iNSERT (const int & D);
// the insertion node at the specified location
void insert_pos (const int & n-, const int & D);
// delete the specified data node
void ERASE (const int & D);
// delete the node specified position
void erase_pos (const int & n-);
// change specified data
void UPDATA (const int & D, const int & D1);
// modify data in the specified location
void updata_pos (const int & n-, const int & D);
// inverted list function
void Reverse ();
// print
void Print ();
Private:
struct the Node
{
int Data;
* Next the Node;
the Node (const int &
Data (D), Next (NULL) {}
};
the Node * head; // Create head node
// clear the entire list
void Clear ()
{
the Node * P = head;
the while (P )
{
the node Q * = p-> Next;
Delete P;
P = Q;
}
}

// returns the specified node on the need to find a node, and returns
the node * Find (const int & D)
{
the node * P = head;
for (; P; P = p-> Next)
{
IF (p-> next-> Data == D)
BREAK;
}
return P;
}

};

// Create a list
void List :: create_list ()
{
head the Node new new = (0);
}

//从尾部插入
void List::add(const int &d)
{
Node *p=head;
Node *q=new Node(d);
while(p->next)
{
p=p->next;
}
p->next=q;
}

// inserted from the head
void List :: INSERT (const int & D)
{
the Node the Node new new = P * (D);
p-> Next = head-> Next;
head-> Next = P;
}

// inserted in the specified location node
void List :: insert_pos (const int & n-, const int & D)
{
the Node * P = head;
the Node * Q = new new the Node (D);
for (int I =. 1; I <n-; I ++ )
{
P = p-> Next;
}
Q-> = p-Next> Next;
p-> Next Q =;
}

// delete the specified data node
void List :: ERASE (const int & D)
{
the Node = P * Find (D);
the Node Q * = p-> Next;
p-> Next = p-> next-> Next;
Delete Q;
}

// delete the specified node position
void List :: erase_pos (n-const int &)
{
the Node * P = head;
for (int I =. 1; I <n-; I ++)
{
P = p-> Next;
}
the Node * Q = p-> Next;
p-> Next = p-> next-> Next;
Delete Q;
}

// change specified data
void List :: UPDATA (const int & D, D1 const int &)
{
the Node = P * Find (D);
p-> next-> Data = D1;
}

void List::updata_pos(const int &n,const int &d)
{
Node *p=head;
for(int i=1;i<n;i++)
{
p=p->next;
}
p->next->data=d;
}
//打印链表
void List::print()
{

for(Node *p=head->next;p;p=p->next)
{
cout << p->data <<" ";
}
cout<<endl;
}
int main(int argc, const char * argv[])
{
List list;
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
list.add(7);
list.add(8);
list.print();
cout<<endl;
cout<<".......10 is inserted into the third node as a linked list .......... ";
list.insert_pos (3,10);
COUT << endl;
list.print ();
COUT << "........ delete the list of the second node ........... .... ";
list.erase_pos (2);
cout << endl;
list.print ();
cout <<" ........ the list to delete the data as specified node 6 ..... .......... ";
list.erase (. 6);
COUT << endl;
list.print ();
COUT <<" the linked list data .......... 8 .............. to node 88 ";
list.updata (8,88);
COUT << endl;
list.print ();
COUT <<" ..... ..... in the data list to the fourth node .............. 14 ";
list.updata (4,14);
COUT << endl;
list.print ();
return 0;
}

Guess you like

Origin www.cnblogs.com/xiangrikuidemaimai/p/11442100.html