#include <iostream>
using namespace std;
typedef struct node {
int data;
node *next;
}node;
void Inite(node* listedsq) {
listedsq->data = 0;
listedsq->next = NULL;
}
bool Inserthead(node *head, int value) {
node *s = new node;
s->data = value;
s->next = head->next;
head->next = s;
return true;
}
bool Insertend(node *head, int value) {
node *temp = new node;
node *s = head;
temp->data = value;
temp->next = NULL;
while (s->next != NULL) {
s = s->next;
}
s->next = temp;
return true;
}
bool Delete(node *head, int pos) {
if (head->next == NULL) {
cout << "表为空,无法删除" << endl;
return false;
}
node *s = head;
node *temp;
for (int i = 1; i < pos;i++) {
if (s->next == NULL) {
cout << "超出链表长度,删除失败!" << endl;
return false;
}
s = s->next;
}
temp = s->next;
s->next = s->next->next;
delete temp;
return true;
}
int Getvalue(node *head, int pos) {
node *temp = head->next;
for (int i = 1; i < pos; i++) {
temp = temp->next;
if (temp == NULL) {
cout << "查找位置超过链表长度" << endl;
return NULL;
}
}
return temp->data;
}
void Output(node *head) {
node *temp;
temp = head->next;
while (temp != NULL) {
cout << temp->data << ",";
temp = temp->next;
}
cout << endl;
}
int main() {
cout << "链表" << endl;
node* l = new node;
Inite(l);
cout << endl;
cout << "插入10个数:" << endl;
for (int i = 1; i < 11; i++) {
Insertend(l, i);
}
Output(l);
cout << endl;
cout << "删除链表的第3、5个元素:" << endl;
Delete(l, 3);
Delete(l, 5);
Output(l);
cout << endl;
cout << "输出链表的第5个元素值:" << endl;
cout << Getvalue(l, 5) << endl;
cout << "输出链表的第10个元素值:" << endl;
cout << Getvalue(l, 10) << endl;
return 0;
}