|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
struct string
{
char ch;
struct string *nextptr;
};
int num = 0;
struct string *creat(struct string *h) {
struct string *p1, *p2;
p1 = p2 = (struct string*)malloc (sizeof (struct string));
if (p2 != NULL) {
scanf("%c", &p2->ch);
p2->nextptr= NULL;
}
while (p2->ch != '\n') {
num++;
if (h == NULL) {
h = p2;
}
else
p1->nextptr = p2;
p1 = p2;
p2 = (struct string*)malloc (sizeof (struct string));
if (p2 != NULL) {
scanf("%c", &p2->ch);
p2->nextptr=NULL;
}
}
return h;
}
struct string *del(struct string *h, char *str) {
struct string *p1, *p2;
p1 = h;
if(p1 == NULL) {
printf("the list is null \n");
return h;
}
p2=p1->nextptr;
if(!strcmp(p1->ch, str)) {
h=p2;
return h;
}
while(p2!=NULL) {
if (!strcmp(p2->ch, str)) {
p1->nextptr=p2->nextptr;
return h;
}
else
{
p1 = p2;
p2 = p2->nextptr;
}
}
return h;
}
void print(struct string *h) {
struct string *temp;
temp = h;
while (temp != NULL) {
printf("%c", temp->ch);
temp = temp->nextptr;
}
}
void main() {
struct string *head;
char *a;
head = NULL;
printf("请输入一行字符: \n");
head = creat(head);
print(head);
printf("%d", num);
printf("please enter del: \n");
scanf("%c", &a);
del(head, a);
print(head);
printf("%d", num);
}
|
|