//线性表的链式存储实现(有头结点)
#include <stdio.h>
struct node{
int element;
struct node* next;
};
struct node* Insert(struct node* head,int i,int number)
{
int j=0;
struct node* p;
p=head;
while(p&&j<i-1){
p=p->next;
j++;
}
if(p==NULL){
printf("Error\n");
return;
}
struct node* t;
t=(struct node*)malloc(sizeof(struct node));
t->element=number;
t->next=p->next;
p->next=t;
return;
}
Delete(struct node* head,int i)
{
struct node* p;
p=head;
int location=0;
while(location<i-1&&p){
p=p->next;
location++;
}
if(location!=i-1||p->next==NULL){//元素不到i或者i位置为NULL
printf("Error\n");
return;
}
struct node* temp=p->next;
p->next=temp->next;
free(temp);
return;
};
struct node*Init(struct node* head)
{
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
return head;
};
int main()
{
struct node* head;
//初始化
head=Init(head);
//插入
Insert(head,1,10);//插到第i个位置
//删除
Delete(head,1);//删除第i个元素
return 0;
}
线性表的链式存储实现(有头结点)
最新推荐文章于 2021-08-11 11:32:54 发布