----------------解决方案--------------------------------------------------------
#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编辑过]
----------------解决方案--------------------------------------------------------
线形链表这东西用起来真麻烦:(
----------------解决方案--------------------------------------------------------
是我做麻烦了吗??
----------------解决方案--------------------------------------------------------
不是你做的麻烦
是链表这东西麻烦,但处理大量数据还是很有用
----------------解决方案--------------------------------------------------------
#include"stdio.h"
#include "ctype.h"
main()
{
char *c,d;
int n,i,j;
i=0;
do /*输入字符串*/
{
*(c+i)=getch();
printf("%c",*(c+i));
i++;
}
while(*(c+i-1)!=13);i--;printf("\n");
d=getch(); /*输入要插入的字符*/
printf("%c\n",d);
do
{ /*输入要插入字符的位置*/
scanf("%d",&n);
if((n>0)&&(n<i+2))break;
if(!isascii(n))
{printf("input errno!");getch();exit(1);}
}
while(1);
for(j=i;j>n-2;j--) /*插入字符运算*/
{
*(c+j+1)=*(c+j);
}
*(c+n-1)=d;
for(j=0;j<i+1;j++) /*输出字符*/
{
printf("%c",*(c+j));
}
getch();
}
[此贴子已经被作者于2006-2-17 22:46:58编辑过]
----------------解决方案--------------------------------------------------------
数组的优点就是随机存取,缺点就是在作删除和插入操作时需要移动大量元素.
----------------解决方案--------------------------------------------------------
我是菜鸟啊
不知道该怎么办
----------------解决方案--------------------------------------------------------
#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);
}
没写注释,不好意思
我是学物理专业的。
我以前也学过数据结构,我当时不明白的就是上述我标记的。开辟内存,为什么是
insert=(struct chain *)malloc (sizeof(struct chain));而不是
insert=(struct chain *)malloc (sizeof(struct chain)*);
能不能说明一下,
----------------解决方案--------------------------------------------------------
malloc前面是类型,后面是长度.
----------------解决方案--------------------------------------------------------