单链表应用中,用户手动输入一串字符,查找字符’x’出现的索引
- 创建指针结构体
struct link {
char ch;
struct link *next;
};
- 创建用户输入算法
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;
};
- 提供一个搜索算法,用来搜索用户输入的内容.
void search(struct link *L) {
struct link *h;
int index = 1;
h = L;
while(h!=NULL) {
if(h->ch == 'x') {
printf("X的位置为:%d\n",index);
}
index++;
h = h->next;
}
}