链表的插入运算

来源:百度知道 编辑:UC知道 时间:2024/09/23 19:24:52
求高手帮我在此程序基础上做一个链表的插入计算。写出的程序要能正确运行的啊,写出程序运行结果。。。。。。回答可以的话再加分!重谢!
#include "stdio.h"
#include "malloc"
typedef int datatype;

typedef struct node
{ datatype data;
struct node *next;
}linklist;

void main()
{
int i;
char ch;
linklist *head, *p,*r;
head=NULL;
r=NULL;

p=malloc(sizeof(linklist));
head=p;
ch=getchar();
while(ch!='$')
{
p=malloc(sizeof(linklist));
p->data=ch;
if(head==NULL) head=p;
else
r->next=p;
r=p;
ch=getchar();

}

p=head;

printf("\n");
for(i=0;i<5;i++)
{
printf("%c, ",p->data);
p=p->next;
}

}

#include "stdio.h"
/*#include "malloc"*/
#include "stdlib.h"
typedef int datatype;

typedef struct node
{ datatype data;
struct node *next;
}linklist;

linklist * insert(linklist *head,linklist *p)
{
linklist *r=head;
if(head==NULL) head=p;
else{
while(r->next!=NULL)r=r->next;
r->next=p;
}
p->next=NULL;
return head;
}

void main()
{
int i;
char ch;
linklist *head, *p,*r;
head=NULL;
r=NULL;

/*p=malloc(sizeof(linklist));
r=head=p;*/
ch=getchar();
while(ch!='$')
{
p=malloc(sizeof(linklist));
p->data=ch;
p->next=NULL;
if(head==NULL) r=head=p;
else
r->next=p;
r=p;
/*head=insert(head,p); 用这个插入也可以*/
ch=getchar();
}
p=head;

printf("\n");
for(i=0;i<5;i++)