如何C语言编写线性表的初始化,求线性表长度,插入,删除

2024-12-29 18:13:07
推荐回答(2个)
回答1:

#include
#include
struct node
{
int num;
struct node *next;
};
struct node *head=NULL,*point,*newNode;
int Count=0;//链表长度
//增加节点
void Add(int i)
{
newNode=(struct node*)malloc(sizeof(struct node));
if(newNode==NULL)
exit(0);
if(head==NULL)
{
head=newNode;
}
else
{
point=head;
while(point->next!=NULL)
{
point=point->next;
}
point->next=newNode;
}
point=newNode;
point->num=i;
point->next=NULL;
Count++;
}
//查询第i个结点
void Search(int i)
{
int n;
struct node *temp;
point=head;
for(n=0;n {
temp=point;
point=point->next;
}
printf("%d\n",point->num);
}
//删除第i个结点
void DeleteNode(int i)
{
int n=0;
struct node *temp;
point=head;
for(n=0;n {
temp=point;
point=point->next;
}
temp->next=point->next;
Count--;
}
//遍历打印链表
void Traversal()
{
point=head;
for(;point!=NULL;point=point->next)
{
printf("%d\t",point->num);
}
printf("\n");
}

回答2:

这个找找源程序看看吧,这个很基本很经典的东西