链表基本操作,创建,遍历,删除,插入,重排。
struct Node
{
int a; //数据域
struct Node* next; //指针域(指向节点的指针)
};节点的创建。
void AddListTill(int a )
{
struct Node* temp=(struct Node*)malloc(sizeof(struct Node));
temp->a=a;
temp->next=NULL;
if(NULL==head)
{
head=temp;
// end=temp;
}
else
{
end->next=temp;
// end=temp;
}
end=temp;
}链表的创建
struct Node* FindNode(int a )
{
struct Node *temp =head;
while(temp !=NULL)
{
if(a == temp->a)
{
return temp;
}
temp = temp->next;
}
return NULL;
} 链表的查找。
void DeleteList(int a)
{
if(NULL==head)
{
printf("链表没东西\n");
return;
}
struct Node* temp =FindNode(a);
if(NULL==temp)
{
printf("查无此点\n");
return;
}
if(head==end)
{
free(head);
head=NULL;
end=NULL;
}
else if(head->next==end)
{
if(end==temp)
{ DeleteListTail(); }
else if(temp==head)
{ DeleteListHead(); }
}
else
{
if(end==temp)
DeleteListTail();
else if(temp==head)
DeleteListHead();
else
{
struct Node*pt =head;
while(pt->next!=temp)
{
pt=pt->next;
}
pt->next=temp->next;
free(temp);
}
}
}链表删除
void AddList(int index,int a)
{
if (NULL==head)
{
printf("链表没有节点\n");
return;
}
struct Node* pt =FindNode(index);
if(NULL==pt)
{
return;
}
struct Node* temp =(struct Node *)malloc(sizeof(struct Node));
temp->a=a;
temp->next=NULL;
if (pt == end)
{
end->next=temp;
end=temp;
}else
{
temp->next=pt->next;
pt->next=temp;
}
}插入
参加新星杯,但收获并不大,觉得自己还需要努力,才·可以追上别人步伐,不能原地踏步了。