//线性表的链式存储实现(无头结点)
#include <stdio.h>
struct node{
int element;
struct node* next;
};
void Find2(struct node* head,int k)
{
struct node* p;
p=head;
int location=1;
while(p&&location<k){
p=p->next;
location++;
}
if(p&&location==k)
printf("%d\n",p->element);
else
printf("Not Found\n");
return;
}
struct node* Insert(struct node* head,int i,int number)
{
struct node* t;
t=(struct node*)malloc(sizeof(struct node));
t->element=number;
t->next=NULL;
if(i==1){
head=t;
return head;
}
int j=1;
struct node* p;
p=head;
while(p&&j<i-1){
p=p->next;
j++;
}
if(p==NULL){
printf("Error\n");
free(t);
return head;
}
t->next=p->next;
p->next=t;
return head;
}
struct node* Delete(struct node* head,int i)
{
if(i==1){
struct node* temp;
temp=head;
head=head->next;
free(temp);
return head;
}
struct node* p;
int location=1;
while(location<i-1&&p){
p=p->next;
location++;
}
if(location!=i-1||p->next==NULL){//元素不到i或者i位置为NULL
printf("Error\n");
return head;
}
struct node* temp=p->next;
p->next=temp->next;
free(temp);
return head;
};
struct node*Init(struct node* head)
{
return NULL;
};
int main()
{
struct node* head;
//初始化
head=Init(head);
//按值查找
// Find1(head,number);//略
//序号查找
Find2(head,1);
//插入
head=Insert(head,1,10);//插到第i个位置
//删除
head=Delete(head,1);//删除第i个元素
return 0;
}
线性表的链式存储实现(无头结点)(陈越数据结构)
最新推荐文章于 2019-05-10 13:34:32 发布