#include <stdio.h>
#include <stdlib.h>
#define DT char //可用int、unsigned、char
#define SDT " %c" //对应 "%d"、"%u"、" %c"
typedef struct _data {
DT x;
struct _data *next;
} DATA;
DATA* INPUT(int m);
void TEST(DATA *p);
DATA* DELLEN(DATA *p , int dellen);
int DELVAL(DATA *p , DT delval);
int main(int argc, char **argv){
DATA *now = NULL; //记录尾指针
int n , m , del ; //n最大长度、m数据长度、del删除的长度、val删除的值
DT val;
scanf("%d %d",&n,&m);
/*构建单向循环链表*/
if(n>=m){
now = INPUT(m);
TEST(now);
}
/*实现删除指定数目的数据*/
scanf("%d",&del);
now = DELLEN(now,del);
TEST(now);
/*实现删除指定值的数据*/
scanf(SDT,&val);
if(DELVAL(now,val)){
printf("del val ok!\n");
TEST(now);
}else{
printf("can't find the val!\n");
}
return 0;
}
DATA* INPUT(int m){
DATA *p , *head ;
if(m--){
head = (DATA*)malloc(sizeof(DATA));
head->next = head;
scanf(SDT,&head->x);
p = head;
}
while(m--){
p->next = (DATA*)malloc(sizeof(DATA));
p = p->next;
p->next = head;
scanf(SDT,&p->x);
}
return p;
}
DATA* DELLEN(DATA *p , int dellen){
DATA *q;
while(dellen--){
if(p->next == p){
p->next = NULL;
free(p);
p = NULL;
printf("error ! dellen > list \n");
break;
}
q = p->next;
p->next = p->next->next;
free(q);
}
(p != NULL)?(q = p):(q = p->next);
return q;
}
int DELVAL(DATA *p , DT delval){
DATA *pt = p;
int i;
do{
if(pt->next->x == delval){
p = pt->next;
pt->next = pt->next->next;
free(p);
p = NULL;
break;
}
pt = pt->next;
}while(pt != p);
(p == NULL)?(i=1):(i=0);
return i;
}
void TEST(DATA *p){
DATA *pt = p->next;
printf("head = %p , next = %p\n",p,p->next);
do{
printf("add = %p ",pt);
printf(SDT,pt->x);
printf(" next add = %p\n",pt->next);
pt = pt->next;
}while(pt != p->next);
printf("\n");
}
|