单链表应用中,删除指定元素,并且打印链表元素内容
struct link {
char ch;
struct link *next;
};
- 创建用户输入元素内容算法(CreateByUser)
struct link *create() {
char ch;
struct link *next , *R , *L = NULL;
printf("printf char in this scope:\n");
while((ch = getchar()) != '\n') {
next = (struct link *)malloc(sizeof(struct link));
next -> ch = ch;
if(L == NULL) {
L = R = next;
} else {
R -> next = next;
R = next;
}
}
R -> next = NULL;
return L;
};
struct link *Delete(struct link *L , char ch){
struct link *q , *r , *p ;
p = q = L;
while(p != NULL){
if(p->ch == ch){
if(p == L){
r = p;
p = p->next;
q = p;
}else{
r = q->next;
q->next = p->next;
p = p->next;
}
r->next = NULL;
free(r);
}else{
q = p;
p = p->next;
}
}
return L;
}
void search(struct link *L , char ch) {
struct link *h;
int index = 1 ;
bool flag = false;
h = L;
while(h!=NULL) {
if(h->ch == ch) {
printf("X的位置为:%d\n",index);
flag = true;
}
index++;
h = h->next;
}
if(!flag){
printf("不存在指定字符\n");
}
}
void DispLink(link *L){
struct link *H;
H = L;
while(H!=NULL){
printf("%3c",H->ch);
H = H->next;
}
}
- 展示结果如下:(不带x字符)
- 带x字符
|