#ifndef SCNODE_H
#define SCNODE_H
typedef struct Node {
int data;
Node* next;
}Node,*LinkList;
int initialize(LinkList& list);
int cleanup(LinkList& list);
int insertData(LinkList& list, int data);
int deleteData(LinkList& list, int data);
int searchData(LinkList& list, int data); // return the index that first find
#endif
#include "scNode.h"
#include <stdlib.h>
#include <iostream>
using namespace std;
int initialize(LinkList& list) {
int retval = 1;
int headNumber = 0;
Node* head = new Node;
if (NULL != head) {
head->data = headNumber;
head->next = head;
list = head;
retval = 1;
} else {
retval = 0;
}
return retval;
}
int cleanup(LinkList& list){
Node* head = list;
Node* pNode = head;
Node* deleteNode = NULL;
int retval = 1;
if (NULL == head) {
retval = 1;
} else {
deleteNode = pNode->next;
while (deleteNode != head) {
pNode = deleteNode->next;
delete deleteNode;
deleteNode = pNode;
}
delete head;
retval = 1;
}
return retval;
}
int insertData(LinkList& list, int data) {
static int nodeNumber = 0;
Node* head = list;
Node* pNode = head;
Node* insertNode = new Node;
int retval = 1;
if (NULL != insertNode) {
if (NULL == pNode) {
retval = 0;
} else {
while (pNode->next != head) {
pNode = pNode->next;
}
pNode->next = insertNode;
insertNode->next = head;
insertNode->data = data;
nodeNumber++;
head->data = nodeNumber;
retval = 1;
}
} else {
retval = 0;
}
return retval;
}
int searchData(LinkList& list, int data) {
Node* head = list;
Node* pNode = head;
Node* searchNode = NULL;
int index = 1;
if (NULL == head) {
index = -1;
} else {
searchNode = pNode->next;
while ((searchNode != head) && (searchNode->data != data)) {
pNode = searchNode;
searchNode = pNode->next;
index++;
}
if (searchNode == head ) {
index = 0;
}
}
return index;
}
int deleteData(LinkList& list, int data) {
Node* head = list;
Node* pNode = head;
Node* deleteNode = head;
int index = 0;
int retval = 0;
index = searchData(list, data);
if ((-1 == index) || (0 == index)) {
retval = 0;
} else {
for (int i = 0; i< index; i++) {
pNode = deleteNode;
deleteNode = pNode->next;
}
pNode->next = deleteNode->next;
delete deleteNode;
deleteNode = NULL;
(head->data)--;
retval = 1;
}
return retval;
}
本文地址:http://com.8s8s.com/it/it26156.htm