类实现链表
结点类的创建class ListNode//结点类
{
public:
int data;
ListNode* next;
ListNode()
{
next = NULL;
}
ListNode(int a)
{
data = a;
next = NULL;
}
};
链表类的创建
class linkList//链表类
{
public:
ListNode* head;
int len;
linkList()//链表初始化
{
head = new ListNode();//头节点
len = 0;//链表长度初始化为0
}
linkList(int n, int* arr)
{
head = new ListNode();
len = n;
ListNode* node = new ListNode [arr[0]];//申请第一个node结点的内存空间
head = node;
for (int i = 0; i < n; i++)
{
ListNode* newNode = new ListNode(arr[i]);
node->next = newNode;
node = node->next;
}
}
~linkList()
{
ListNode* p, * q;
p = head;
while (p != NULL)
{
q = p;
p = p->next;
delete q;
}
len = 0;
head = NULL;
}
查找第i个位置的元素
int LL_get(int i)
{
int j;
ListNode* p;
p = head->next;
j = 1;
while (p && j < i)
{
p = p->next;
++j;
}
if (!p || j > i)
return error;
else
return p->data;
}
插入元素
int LL_insert(int i, int item)
{
int j;
ListNode* p,*s;
p = head;
j = 1;
while (p && j < i)
{
p = p->next;
++j;
}
if (!p || j > i)
return error;
s = new ListNode(item);
s->data = item;
s->next = p->next;
p->next = s;
return ok;
}
删除元素
int LL_del(int i)
{
int j;
ListNode* p, * q;
p = head;
j = 1;
while (p && j < i)
{
p = p->next;
++j;
}
if (i<1 ||i>len)
return error;
q = p->next;
p->next = q->next;
return ok;
}
链表元素输出
void LL_display()
{
ListNode* p;
p = head->next;
while (p)
{
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
例题:
实现:
#includeusing namespace std; #define ok 0 #define error -1 class ListNode//结点类 { public: int data; ListNode* next; ListNode() { next = NULL; } ListNode(int a) { data = a; next = NULL; } }; class linkList//链表类 { public: ListNode* head; int len; linkList()//链表初始化 { head = new ListNode();//头节点 len = 0;//链表长度初始化为0 } linkList(int n, int* arr) { head = new ListNode(); len = n; ListNode* node = new ListNode [arr[0]];//申请第一个node结点的内存空间 head = node; for (int i = 0; i < n; i++) { ListNode* newNode = new ListNode(arr[i]); node->next = newNode; node = node->next; } } ~linkList() { ListNode* p, * q; p = head; while (p != NULL) { q = p; p = p->next; delete q; } len = 0; head = NULL; } int LL_get(int i) { int j; ListNode* p; p = head->next; j = 1; while (p && j < i) { p = p->next; ++j; } if (!p || j > i) return error; else return p->data; } int LL_insert(int i, int item) { int j; ListNode* p,*s; p = head; j = 1; while (p && j < i) { p = p->next; ++j; } if (!p || j > i) return error; s = new ListNode(item); s->data = item; s->next = p->next; p->next = s; return ok; } int LL_del(int i) { int j; ListNode* p, * q; p = head; j = 1; while (p && j < i) { p = p->next; ++j; } if (i<1 ||i>len) return error; q = p->next; p->next = q->next; return ok; } void LL_display() { ListNode* p; p = head->next; while (p) { cout << p->data << " "; p = p->next; } cout << endl; } }; int main() { int n; cin >> n; int* arr = new int[n]; for (int i = 0; i < n; ++i) { cin >> arr[i]; } int status; linkList* list = new linkList(n, arr); list->LL_display(); int i, item; cin >> i >> item; status=list->LL_insert(i, item); if (status == ok) list->LL_display(); else cout << "error" << endl; cin >> i >> item; status = list->LL_insert(i, item); if (status == ok) list->LL_display(); else cout << "error" << endl; cin >> i; status = list->LL_del(i); if (status == ok) list->LL_display(); else cout << "error" << endl; cin >> i; status = list->LL_del(i); if (status == ok) list->LL_display(); else cout << "error" << endl; cin >> i; status = list->LL_get(i); if (status == ok) list->LL_display(); else cout << "error" << endl; cin >> i; status = list->LL_get(i); if (status == error) cout << "error" << endl; else cout << status << endl; return 0; }



