#include<bits/stdc++.h>
using namespace std;
typedef struct List{
int value;
List* next;
}*Link;
Link head = NULL;
Link tail = NULL;
void InsertHead(int value){
Link tmp = new List;
tmp->value = value;
tmp->next = head;
head = tmp;
if(NULL==tail){
tail = head;
}
}
void InsertTail(int value){
Link tmp = new List;
tmp->value = value;
tmp->next = NULL;
if(NULL==tail){
tail = tmp;
head = tmp;
}else{
tail->next = tmp;
tail = tmp;
}
}
int Search(int value){
Link p = head;
int index = 0;
while(p){
if(p->value==value){
return index;
}
p = p->next;
index++;
}
return -1;
}
void RemoveHead(){
if(NULL==head){
return ;
}
Link tmp = head;
head = head->next;
delete tmp;
return ;
}
void RemoveTail(){
if(NULL==head){
return;
}
Link prev = NULL;
Link tmp;
if(NULL==head->next){
prev = head;
head = tail = NULL;
delete prev;
return ;
}
prev = head;
tmp = prev->next;
while(tmp->next!=NULL){
prev = tmp;
tmp = tmp->next;
}
prev->next = NULL;
tail = prev;
delete tmp;
return;
}
void print(){
Link p = head;
while(p){
cout<<p->value<<' ';
p = p->next;
}
cout<<endl;
}
int main(){
InsertHead(5);
print();
InsertTail(8);
cout<<Search(8)<<endl;
print();
RemoveHead();
print();
}
Linked List->single linked list
最新推荐文章于 2023-11-25 00:36:38 发布