//***************************intSLList.h********************************/
#ifndef INTSLLIST_H_
#define INTSLLIST_H_
class IntSLLNode
{
public:
IntSLLNode():next(0){};
IntSLLNode(int el, IntSLLNode* ptr = 0) :info(el), next(ptr){};
public:
int info;
IntSLLNode *next;
// friend class IntSLLList;
};
class IntSLLList
{
public:
IntSLLList():head(0), tail(0){};
void addToHead(int el);
void addToTail(int el);
int deleteFromHead(); //delete node and return its values
int deleteFromTail();
void deleteNode(int el);
bool isInList(int el); //judge whether the node is in the linked list
private:
IntSLLNode *head,*tail;
};
#endif
//********************************initSLList.cpp***********************************//
#include "intSLList.h"
void IntSLLList::addToHead(int el)
{
head = new IntSLLNode(el,head); //头部指针移位
if (tail == 0)
{
tail = head;
}
}
void IntSLLList::addToTail(int el)
{
if (tail == 0)
{
tail = new IntSLLNode(el);
head = tail;
}
tail->next = new IntSLLNode(el);
tail = tail->next; //尾部指针移位
}
int IntSLLList::deleteFromHead()
{
int el = head->info;
IntSLLNode *tmp = head->next; //create a temporary variable to save second node
if (head == tail)
{
head = tail = 0;
}
else
{
delete head; //here just delete data, the point name still exists;
}
head = tmp;
return el;
}
int IntSLLList::deleteFromTail()
{
int el = tail->info;
if (tail == head)
{
tail = head = 0;
}
else
{
for (IntSLLNode *i = head; i != tail;i=i->next)
{
if (i->next == tail)
{
IntSLLNode *tmp = tail;
tail = i; //tail points to second last node
delete tmp; //delete last node
}
}
}
return el;
}
void IntSLLList::deleteNode(int el)
{
if (head!=0)
{
if (tail == head && head->info == el) //linked list only has one node and is be selected
{
delete head;
head = tail = 0;
}
else
{
IntSLLNode *pred;
for (pred = head; pred != tail;pred=pred->next)
{
if (pred->info == el && pred == head) //first node
{
IntSLLNode *tmp = head->next;
delete head;
head = tmp;
}
else if (pred->next->info==el && pred->next == tail) //last node
{
tail = pred;
delete tail->next;
tail->next = 0;
}
else
{
if (pred->next->info == el) //node among the list
{
IntSLLNode *tmp = pred->next->next;
delete pred->next;
pred->next = tmp;
}
}
}
}
}
}
bool IntSLLList::isInList(int el)
{
IntSLLNode *tmp = head;
for (tmp; tmp != tail->next; tmp = tmp->next)
{
if (tmp->info == el)
{
return true;
}
}
return false;
}
//******************************main.cpp*****************************************//
#include <iostream>
#include "intSLList.h"
using namespace std;
int main()
{
//primary realization single linked list
IntSLLNode *head = new IntSLLNode(10);
head->next = new IntSLLNode(20);
head->next->next = new IntSLLNode(30);
head->next->next->next = new IntSLLNode(40);
//create a new class to operate single linked list
IntSLLList my_list;
my_list.addToHead(20);
my_list.addToHead(10);
my_list.addToTail(30);
my_list.addToTail(40);
my_list.deleteNode(30);
if (my_list.isInList(40))
{
cout << "数据存在" << endl;
}
else
{
cout << "数据不存在" << endl;
}
return 0;
}