#include<iostream> #include<vector> using namespace std; //结构定义 #define maxSize 50 typedef struct LNode{ int data; LNode* next; }LNode,*LinkList; //头插法建立单链表 LinkList List_HeadInsert(LinkList &L){ LNode *s; int elem; //建立头结点 L = new LNode; L->next = nullptr; cin>>elem; while(elem != -1){ s = new LNode; s->data = elem; s->next = L->next; L->next = s; cin>>elem; } return L; } //尾插法建立单链表 LinkList List_TailInsert(LinkList &L){ L = new LNode; L->next = nullptr; LNode *s, *r = L; int elem; cin>>elem; while(elem != -1){ s = new LNode; s->data = elem; r->next = s; r = s; cin>>elem; } r->next = nullptr; return L; } //按序号查找某个节点 LNode *GetElem(LinkList &L,int i){ if(i == 0){ return L; } if(i < 0){ return nullptr; } LNode* p = L; while(p != nullptr && i--){ p = p->next; } cout<<p->data; return p; } //按值查找表结点 LNode *LocateElem(LinkList &L,int e){ if(L->next == nullptr){ return nullptr; } LNode * p = L->next; while(p != nullptr && p->data != e){ p =p->next; } return p; }