过不去,过不去,忘各位帮帮看看怎么回事,谢谢
提示: 作者被禁止或删除 内容自动屏蔽
2010-10-19 14:16
2010-10-19 15:13
2010-10-19 15:22
程序代码:#include<stdio.h>
#include<malloc.h>
struct linknode
{
int data;
struct linknode *next;
};
struct linknode *create()//创建单链表
{
int d;
int i=1;
struct linknode *head,*s,*t;
head=NULL;
printf("建立一个单链表,data域数据已0结束:\n");
while(1)
{
printf("%d:",i);
scanf("%d",&d);
if(d==0)
break;
if(i==1)//建立第一个结点
{
head=(struct linknode *)malloc(sizeof(struct linknode));
head->data=d;
head->next=NULL;
t=head;
}
else//建立其余结点
{
s=(struct linknode *)malloc(sizeof(struct linknode));
s->data=d;
s->next=NULL;
t->next=s;
t=s;
}
i++;
}
return head;
}
void disp(struct linknode *head)//输出结点数据
{
struct linknode *p=head;
printf("输出一个单链表:\n");
if(p==NULL)
printf("空");
while(p!=NULL)
{
printf("%4d",p->data);
p=p->next;
}
printf("\n");
}
int len(struct linknode *head)//返回单链表的长度
{
int pcount=0;//结点计数器
struct linknode *p=head;
while(p!=NULL)
{
p=p->next;
pcount++;
}
return pcount;
}
struct linknode *find(struct linknode *head,int i)//返回第i个结点的指针
{
struct linknode *p=head;
int j=1;
if(i>len(head)||i<0)
return NULL;
else
{
while(p!=NULL&&j<i)
{
j++;
p=p->next;
}
return p;
}
}
/*在单链表第i个结点i>0之后插入一个data域为x的结点*/
struct linknode *insert(struct linknode *head,int i,int x)
{
struct linknode *p,*s;
s=(struct linknode *)malloc(sizeof(struct linknode));
s->data=x;
s->next=NULL;
if(i==0)//插入头结点之前
{
s->next=head;
head=s;
}
else
{
p=find(head,i);//查找第i 个结点并由p指向该结点
if(p!=NULL)
{
s->next=p->next;
p->next=s;
}
else
printf("输入的i值不正确\n");
}
return head;
}
struct linknode *del(struct linknode *head,int i)//删除第i个结点
{
struct linknode *p=head,*s;
int j=i;
if(i==1)
{
head=head->next;
free(p);
}
else
{
p=find(head,i-1);//查找第i-1个结点并由p指向该结点
if(p!=NULL&&p->next!=NULL)
{
s=p->next;//s指向要删除的结点
p->next=s->next;
free(s);
}
else
printf("输入的i值不正确\n");
}
return head;
}
void dispose(struct linknode *head)
{
struct linknode *pa=head,*pb;
if(pa!=NULL)
{
pb=pa->next;
if(pb==NULL)//只有一个结点的情况
free(pa);
else
{
while(pb!=NULL)
{
free(pa);
pa=pb;
pb=pb->next;
}
free(pa);
}
}
}
参考一下我的~
2010-10-19 15:24
程序代码:#include<stdio.h>
#include<malloc.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student *next;
};
int n;
struct student*creat(void)
{
struct student*head;
struct student*p1,*p2;
n=0;
p1=p2=(struct student*)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student*)malloc(LEN);
scanf("%d,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
/*输出链表*/
void print(struct student*head)
{
struct student*p;
printf("NOW,These %d records are:\n",n);
p=head;
if(head!=NULL)
do
{
printf("%d%5.1f\n",p->num,p->score);
p=p->next;
}
while(p!=NULL);
}
/*删除链表*/
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL) {printf("List null!\n");
goto end;}
p1=head;
while(num!=p1->num && p1->next!=NULL)//!=打成!==
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head) head=p1->next;
else p2->next=p1->next;
printf("delete :%d\n",num);
n=n-1;
}
else printf("%d not been found!\n",num);
end://改成冒号
return(head);
}
/*插入链表*/
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{
while(p0->num>p1->num && p1->next!=NULL)//!=打成!==
{
p2=p1;
p1=p1->next;//少逗号
}
if(p0->num<p1->num)
{
if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
return (head);
}
void main()
{
struct student *head,*stu;
long del_num;
printf("input records:\n");
head=creat(); /*建立链表返回头指针*/
print(head); //少逗号 /*输出全部节点*/
printf("\ninput the delete number:");
scanf("%ld",&del_num); /*输出要删除的学号*/
head=del(head,del_num); /*输出后链表的头地址*/
print(head); /*输出全部节点*/
printf("\ninput the insert number:") ; /*输出要插入的节点*/
scanf("%d,%f",&stu->num,&stu->score);
head=insert(head,stu);
print(head);
} 大致改了下~不抱错了~,具体没来得及调试
2010-10-19 15:32
看不懂
2010-10-19 15:34