----------------解决方案--------------------------------------------------------
学习成绩不好是不是不能学C啊?
----------------解决方案--------------------------------------------------------
#include <stdio.h> #define len sizeof(struct a) #define NULL 0
struct a {char lk; struct a *next ; };
main() {struct a *head,*p,*q; int i; char ch;
head=p=(struct a *) malloc(len);
for(i=1;i<=26;i++) {(p->lk)=('a'+i-1); p->next=(struct a *)malloc(len); p=p->next;
} p->next=NULL;
p=head; while (p->next != NULL)
{ printf("%c ",p->lk); p=p->next; } printf("\n");
p=head; printf("input deleted letter"); scanf("%c",&ch); while(p->lk != ch && p->next != NULL) p=p->next; q=p; p=q->next; free(q); while (p->next != NULL)
{ printf("%c ",p->lk); p=p->next; } printf("\n");
} 大家看看这段程序`````` 我想输入 b后 显示为 a,c,d,e,f,g````````````````z 可是,却是 c,d,e,f,g,h,````````````````z 该怎么改改啊```
----------------解决方案--------------------------------------------------------
#include <stdio.h> #define len sizeof(struct a) #define NULL 0
struct a {char lk; struct a *next ; };
main() {struct a *head,*p,*q; int i; char ch;
head=p=(struct a *) malloc(len);
for(i=1;i<=26;i++) {(p->lk)=('a'+i-1); p->next=(struct a *)malloc(len); p=p->next;
} p->next=NULL;
p=head; while (p->next != NULL)
{ printf("%c ",p->lk); p=p->next; } printf("\n");
q=p=head; printf("input deleted letter"); scanf("%c",&ch); while(p->lk!=ch&&p) {q=p;p=p->next;} if(p==head) head=head->next; else q->next=p->next; free(p); p=head;
while (p->next!=NULL)
{ printf("%c ",p->lk); p=p->next; } printf("\n"); getch();
}
[此贴子已经被作者于2005-4-9 17:15:10编辑过]
----------------解决方案--------------------------------------------------------
我写的程序没头接点``
----------------解决方案--------------------------------------------------------
晕 好复杂啊
----------------解决方案--------------------------------------------------------
//数组写的 不是本人写的。 是guitarliukai他同学写的。我和guitarliukai也写了链表的 在下一楼呢 //我认为他写的很好。程序的健壮性非常好。所以我在次帮他同学发表出来。希望他勿怪。 #include <stdio.h>
#include <string.h>
main()
{ char str[27]="abcdefghijklmnopqrstuvwxyz";
char ch[2];
int i,j;
printf("\n%s",str);
printf("\ndelete(if exit ,enter 0):");
scanf("%s",ch);
while(ch[0]!='0')
{ j=-1;
for(i=0;str[i]!='\0';i++)
if (ch[0]==str[i])
{j=i;break;}
if (j!=-1)
{ while(str[i+1]!='\0')
{ str[i]=str[i+1];
i++;
}
str[i]='\0';
printf("\n%s\n",str);
}
else
printf("\nno find %c",ch[0]);
printf("\ndelete(if exit,enter 0):");
scanf("%s",ch);
}
printf("\ninsert(if exit,enter 0):");
scanf("%s",ch);
while(ch[0]!='0')
{ if ((ch[0]<'a')||(ch[0]>'z')) printf("\nIt is not letter");
else
{
for(i=0;str[i]!='\0';i++)
{ if(ch[0]>str[i]) continue;
else
{ if(ch[0]==str[i]) {printf("have exist"); break;}
else
{ j=strlen(str);
str[j+1]='\0';
for(;j>i;j--)
str[j]=str[j-1];
str[j]=ch[0];
printf("\n%s",str);
break;
}
}
}
}
printf("\ninsert(if exit,enter 0):");
scanf("%s",ch);
}
}
----------------解决方案--------------------------------------------------------
//数组写的 不是本人写的。 是guitarliukai他同学写的。我和guitarliukai也写了链表的 在下一楼呢 //我认为他写的很好。程序的健壮性非常好。所以我在次帮他同学发表出来。希望他勿怪。 #include <stdio.h>
#include <string.h>
main()
{ char str[27]="abcdefghijklmnopqrstuvwxyz";
char ch[2];
int i,j;
printf("\n%s",str);
printf("\ndelete(if exit ,enter 0):");
scanf("%s",ch);
while(ch[0]!='0')
{ j=-1;
for(i=0;str[i]!='\0';i++)
if (ch[0]==str[i])
{j=i;break;}
if (j!=-1)
{ while(str[i+1]!='\0')
{ str[i]=str[i+1];
i++;
}
str[i]='\0';
printf("\n%s\n",str);
}
else
printf("\nno find %c",ch[0]);
printf("\ndelete(if exit,enter 0):");
scanf("%s",ch);
}
printf("\ninsert(if exit,enter 0):");
scanf("%s",ch);
while(ch[0]!='0')
{ if ((ch[0]<'a')||(ch[0]>'z')) printf("\nIt is not letter");
else
{
for(i=0;str[i]!='\0';i++)
{ if(ch[0]>str[i]) continue;
else
{ if(ch[0]==str[i]) {printf("have exist"); break;}
else
{ j=strlen(str);
str[j+1]='\0';
for(;j>i;j--)
str[j]=str[j-1];
str[j]=ch[0];
printf("\n%s",str);
break;
}
}
}
}
printf("\ninsert(if exit,enter 0):");
scanf("%s",ch);
}
}
----------------解决方案--------------------------------------------------------
//这个是本人和guitarliukai 一起写的链表 。请大家都多指教 程序的健壮性不如数组那位朋友写的好。
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct Node{
char data;
struct Node *next;
}SLNode,*Linklist;
void output(SLNode *head){
SLNode *p;
p=head;
while(p->next!=NULL){
printf("%c ",p->next->data);
p=p->next;
}
printf("\n");
}
Linklist Deletechar(SLNode *head)
{ SLNode *p,*q;
char ch;
p=head;
printf("please input delete char(The zero is over ! ):\n");
scanf("%c",&ch);
while(ch!='0')
{ p=head;
while(p->next!=NULL&&p->next->data<ch)
p=p->next;
if(p->next->data!=ch)
printf("No fine the delete char\n");
else
{
q=p->next;
p->next=q->next;
free(q);
q=NULL;
}
printf("deleted:\n");
output(head);
printf("plese input delete char:\n");
fflush(stdin);//在此加上清流函数
scanf("%c",&ch);
}
return head;
}
Linklist insert(SLNode *head)
{ SLNode *p,*q;
char ch;
printf("please input your char(The zero is over ! ):\n");
scanf("%c",&ch);
while(ch!='0')
{ p=head;
while(p->next!=NULL&&p->next->data<ch)
p=p->next;
if(p->next->data==ch)
printf("list have a char\n");
else
{
if((q=(SLNode *)malloc(sizeof(SLNode)))==NULL)exit(1);
q->data=ch;
q->next=p->next;
p->next=q;
}
printf("inserted:\n");
output(head);
printf("plese input insert char:\n");
fflush(stdin);//在此加上清流函数
scanf("%c",&ch);
}
return head;
}
main()
{SLNode *head,*p,*q;
int i;
if((head=(SLNode *)malloc(sizeof(SLNode)))==NULL) exit(1);
head->next=NULL;
p=head;
for(i=0;i<26;i++)
{
if((q=(SLNode *)malloc(sizeof(SLNode)))==NULL)exit(1);
(q->data)=('a'+i);
q->next=NULL;
p->next=q;
p=q;
q=NULL;
}
p=head;
printf("26 char:\n");
output(head);
printf("\n");
Deletechar(head);
insert(head);
}
----------------解决方案--------------------------------------------------------
数组不是链表 而且没有用上动态数组,在内存上会比链表吃点亏.
你们老师不是让你们用链表写吗?
----------------解决方案--------------------------------------------------------