#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct chain{
char data;
struct chain *next;
};
struct chain *create(void);
struct chain *insert(struct chain *head,char ch,int num);
void print(struct chain *head);
void main()
{
struct chain *head,*p,*q,*d;
char ch;
int num,i=0;
head=create();
scanf("%c %d",&ch,&num);
if ((head->data)<=num)
{
printf("error");
}
head=insert(head,ch,num);
print(head);
printf("\n");
printf("谢谢!\n");
}
struct chain *create(void)
{
struct chain *head,*tail,*p,*h;
head=tail=NULL;
char dat;
int count=0;
h=(struct chain *)malloc (sizeof(struct chain));
printf("输入链表元素:");
while ((dat=getchar())!='\n')
{
p=(struct chain *)malloc (sizeof(struct chain));
p->data=dat;
p->next=NULL;
if (head==NULL)
{
head=tail=p;
}
else
{
tail=tail->next=p;
}
count++;
}
h->data=count;
h->next=head;
return h;
}
void print(struct chain *head)
{
struct chain *r;
r=head->next;
while (r!=NULL)
{
printf("%c",r->data);
head=r->next;
free(r);
r=head;
}
}
struct chain *insert(struct chain *head,char ch,int num)
{
struct chain *p,*q,*insert;
int j=0;
insert=(struct chain *)malloc (sizeof(struct chain));
insert->data=ch;
insert->next=NULL;
p=head;
while ((j<num)&&(p->next!=NULL))
{
q=p;
p=p->next;
j++;
}
insert->next=p;
q->next=insert;
return(head);
}
没写注释,不好意思
[此贴子已经被作者于2006-2-17 21:33:30编辑过]