我没明白你的意思
用链表实现线性表的功能!
#include
#include
#include
#include "malloc.h"
#define null 0
struct node
{
float data;
struct node *next;
};
void insert(float aa,struct node *vq)
{
struct node *newnode,*p;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->data=aa;
p=vq;
while ((p->next!=null)&&(p->next->data{
p = p->next;
}
newnode->next = p->next;
p->next = newnode;
++vq->data;
}
void dele(float aa,struct node *vq)
{
struct node *p,*q;
p=vq;
while ((p->next!=null)&&(p->next->data{
p = p->next;
}
if ((p->next==null)||(p->next->data!=aa))
printf("\n%5.1f is not in the link !",aa);
else if (p->next->data==aa)
{
q = p->next;
p->next = q->next;
free(q);//释放q元素
vq->data=vq->data-1;
}
}
void print(struct node *vq)
{
struct node *p;
printf("\nthe length of link is %4.0f",vq->data);
p=vq->next;
printf("\nthe link is:");
while (p!=0)//当链表中还有元素时
{
printf("%5.1f",p->data);
p=p->next;//用于显示下一个元素
}
}
void main()
{
int mark=1,op;
float aa;
struct node *vq;
vq=(struct node *)malloc(sizeof(struct node));
vq->data=0;
vq->next=null;
while (mark==1)
{
printf("\nWhich kind of operation will you select ? ");
printf("\ninsert---1, delete---2, print---3, exit---0 : ");
scanf("%d",&op);
switch (op)
{
case 0: mark=0;
break;
case 1: printf("\nPlease input the new element:");
scanf("%f",&aa);
insert(aa,vq);
print(vq);
break;
case 2: if (vq->data==0)
printf("\n the link is null !",aa);
else
{
printf("\nPlease input the deleted element:");
scanf("%f",&aa);
dele(aa,vq);
print(vq);
}
break;
case 3: print(vq);
break;
default:printf(" input error!\n");
}
}
}
参考这段代码哈,欢迎交流。