双向链表按位置删除
//双向链表按位置删除
int delete_pos(linklist *L,int pos){
if(L==NULL || L->next==NULL || pos<1 || pos>L->len)
return -1;
linklist *p=L;
for(int i=0;i<pos-1;i++){
p=p->next;
}
linklist *q=p->next;
p->next=q->next;
if(q->next !=NULL){
q->next->prev=p;
}
free(q);
q=NULL;
L->len--;
return 0;
}
双向链表按位置修改
//双向链表位置修改
int modify_pos(linklist *L,int pos, datatyp data){
if(L==NULL || L->next==NULL || pos<1 || pos>L->len)
return -1;
linklist *p=L;
for(int i=0;i<pos;i++){
p=p->next;
}
p->data=data;
return 0;
}
双向链表的按位置查找
//双向链表按位置查找
void search_pos(linklist *L,int pos){
if(L==NULL || L->next==NULL || pos<1 || pos>L->len)
return;
linklist *p=L;
for(int i=0;i<pos;i++){
p=p->next;
}
printf("当前位置的值为:%c\n",p->data);
}
双向循环链表的头插
linklist *insert_head(linklist *L,datatyp data){
if(L==NULL){
return NULL;
}
linklist *newnode=create_node(1);
if(newnode==NULL){
return NULL;
}
newnode->data=data;
newnode->next=L->next;
newnode->prev=L;
if(L->next!=NULL){
L->next->prev=newnode;
}
L->next=newnode;
L->len++;
}
双向循环链表的头删
int delete_head(linklist *L){
//if..
if(L==NULL || L->next==L){
return -1;
}
linklist *q=L->next;
L->next=q->next;
if(q->next!=L){
q->next->prev=L;
}
free(q);
q=NULL;
L->len--;
return 0;
}
双向循环链表的尾删
//尾删
int delete_rear(linklist *L){
if(L==NULL || L->next==L) return -1;
linklist *p=L->prev;
L->prev=p->prev;
p->prev->next=p->next;
free(p);
p=NULL;
L->len--;
}