链表 头插 尾插 遍历 删除
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//map<string,char>vis;
struct node{
int key;
node *head,*next;
};
node *head;
void init(){
head=(node *)malloc(sizeof(node));
head->head=head;
head->next=head;
}
void add(int key){ //头插
node *now=(node *)malloc(sizeof(node));
now->key=key;//赋值
now->next=head->next; //指向开头指向的下一个
head->next=now; //开头指向这个now
now->head=head; //now father指向头
}
void wdd(int key){ //尾插
node *p,*t;
t=head->next;
p=(node *)malloc(sizeof(node));
while(t->next!=head)
t=t->next;
p->key=key;
t->next=p;
p->next=head;
p->head=t;
}
node *sousuo(int key){ //搜索返回
node *now=head->next;
while(now!=head){
if(now->key==key){
break;
}
now=now->next;
}
return now;
}
void shanchu(int value){ //删除链表中key为value的
node *p,*t;
t=head->next;
while(t->next!=head)
{
if(t->next->key==value)
{
p=t->next;
t->next=p->next;
p->next->head=t;
free(p);
}
else
t=t->next;
}
}
void pt(){ //打印
node *now=head->next;
while(1){
if(now==head) break;
cout<<now->key<<" ";
now=now->next;
}
}
int main(){
init();
for(int i=1;i<=10;i++) add(i);
pt();
cout<<endl;
for(int i=1;i<=10;i++) wdd(i);
pt();
cout<<endl;
shanchu(4);
pt();
return 0;
}