没啥说的,最重要的是在操作链表时要画图
//linkList.h
#ifndef _LINKLIST_H_
#define _LINKLIST_H_
#include<stdio.h>
#include<stdlib.h>
//LLNode是struct linkListNode类型,pNode是struct linkListNode*类型
typedef struct linkListNode
{
int data;
struct linkListNode *next;
}LLNode,*pNode,*linkList;
//初始化节点函数
linkList linkListInit();
//头插法插入结点
linkList headInsertNode(linkList head,int value);
//尾插法插入结点
linkList endInsertNode(linkList head,int value);
//销毁链表
int deleteLinklist(linkList head);
//遍历链表
int linkListTraverse(linkList head);
//查找第pos个结点
int finePos(linkList head,int pos);
//查找值为value的所有结点并修改之
int modifyNodeByValue(linkList head,int ModifiedValue,int value);
//修改第pos个结点
int modifyNodeByPos(linkList head,int pos,int value);
//删除第i个结点
int deleteLinkListNode(linkList,int i);
#endif
//linkList.c
#include "linkList.h"
//初始化链表头结点
linkList linkListInit()
{
pNode head=(pNode)malloc(sizeof(LLNode));
if(head==NULL)
{
printf("链表初始化失败\n");
return NULL;
}
head->next=NULL;//初始化时一定要将头结点的next域指向NULL,不然遍历的时候会出现死循环
return head;
}
//头插法为链表插入结点
linkList headInsertNode(linkList head,int value)
{
//创建一个新结点
pNode p=(pNode)malloc(sizeof(LLNode));
if(p==NULL)
return NULL;
p->data=value;
p->next=NULL;
p->next=head->next;//等同于p->next=NULL
head->next=p;
return head;
}
//尾插法为链表插入结点
linkList endInsertNode(linkList head,int value)
{
//pEnd为链表的尾节点
pNode pEnd=NULL;
//创建一个新结点
pNode p=(pNode)malloc(sizeof(LLNode));
if(p==NULL)
return NULL;
p->data=value;
p->next=NULL;
pEnd=head;
//循环是为了遍历出尾结点
while(pEnd->next!=NULL)
{
pEnd=pEnd->next;
}
pEnd->next=p;
pEnd=p;
pEnd->next=NULL;
return head;
}
//链表遍历
int linkListTraverse(linkList head)
{
int lenth=0;
pNode p=NULL;
if(head==NULL||head->next==NULL)
return -1;
p=head->next;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
lenth++;
}
printf("\n");
return lenth;
}
/*
*功能:删除链表的i个结点
*参数1:链表的头结点
*参数2:指定第i个结点
*/
int deleteLinkListNode(linkList head,int i)
{
int j=0;
pNode prePnode=head;
pNode pnode=head->next;
while(pnode!=NULL&&j<i-1)
{
prePnode=pnode;
pnode=pnode->next;
j++;
}
if(pnode==NULL)
{
printf("!!!!!!!!删除结点时位置已经超出了链表!!!!!!!!\n");
return -1;
}
prePnode->next=pnode->next;
pnode->next=NULL;
free(pnode);
return 0;
}
//删除链表函数调用删除结点函数和遍历链表函数
int deleteLinklist(linkList head)
{
int i=0;
int lenth=linkListTraverse(head);
for(i=lenth;i>0;i--)
{
deleteLinkListNode(head,i);
}
return 0;
}
int finePos(linkList head,int value)
{
pNode p=NULL;
int res=0;
int pos=1;
if(head==NULL||head->next==NULL)
return -1;
p=head->next;
while(p!=NULL)
{
if(value==p->data)
{
printf("在%d位置找到值为%d的结点\n",pos,value);
res=1;
}
pos++;
p=p->next;
}
if(res==0)
{
printf("该链表不存在值为%d的结点\n",value);
}
return 0;
}
int modifyNodeByValue(linkList head,int ModifiedValue,int value)
{
pNode p=NULL;
int cur=1;
int res=0;
int pos=0;
if(head==NULL||head->next==NULL)
return -1;
p=head->next;
while(p!=NULL)
{
if(ModifiedValue==p->data)
{
p->data=value;
res=1;
pos=cur;
}
cur++;
p=p->next;
}
if(res==1)
{
printf("已经成功修改链表中值为%d的结点\n",ModifiedValue);
}
else
{
printf("该链表不存在值为%d的结点\n",ModifiedValue);
}
return 0;
}
int modifyNodeByPos(linkList head,int pos,int value)
{
pNode current=head->next;
int i=0;
while(current!=NULL&&i<pos-1)
{
current=current->next;
i++;
}
if(current!=NULL)
current->data=value;
else
printf("!!!!!!!!位置已经超出了链表!!!!!!!!\n");
return 0;
}
int destroylinkList(linkList head)
{
return 0;
}
//main.c
#include"linkList.h"
main()
{
int i=0,ret;
//初始化链表头结点,以创建一个链表
linkList head=linkListInit();
//项链表插入10个元素
printf("头插法插入12个元素\n");
for(i=0;i<12;i++)
{
headInsertNode(head,i+1);
}
printf("尾插法插入12个元素\n");
for(i=0;i<12;i++)
{
endInsertNode(head,i+1);
}
ret=linkListTraverse(head);
if(ret==-1)
{
printf("链表为空!\n");
}
printf("修改值为6个结点修改为100\n");
modifyNodeByValue(head,6,100);
printf("将第23个结点的值修改为99\n");
ret=modifyNodeByPos(head,29,99);
if(ret==-1)
finePos(head,5);
printf("删除第3个结点\n");
deleteLinkListNode(head,3);
printf("删除链表\n");
deleteLinklist(head);
}
源码下载: 点击打开链接