C语言——单链表(增删改查)
一·链表一
#include
#include
#include
typedef struct Test
{
int data;
struct Test *next;
}Link;
Link *head=NULL;
Link* creatHead(Link* head);
void AddLinkNode(Link* head,Link newnode);
void printLinkNode(Link *head);
void deleteLinkNode(Link* head,int data);
void searchLink(Link* head,int data);
void searchLink(Link* head,int data);
void modifiLink(Link* head,int data,int newdata);
int main(int argc,char** argv)
{
int i=0;
Link tmp={0};
int data=0;
head=creatHead(head);
for(i=0;i<5;i++)
{
printf("输入第%d个节点的数据\n",i+1);
scanf("%d",&tmp.data);
AddLinkNode(head,tmp);
}
printLinkNode(head);
printf("输入要删除的数据\n");
scanf("%d",&data);
deleteLinkNode(head,data);
printLinkNode(head);
searchLink(head,3);
modifiLink(head,3,100);
printLinkNode(head);
return 0;
}
Link* creatHead(Link* head)
{
if(head==NULL)
{
head=(Link*)malloc(sizeof(Link));
head->next=NULL;
}
return head;
}
void AddLinkNode(Link* head,Link newnode)
{
Link *p=head;
Link *new=(Link*)malloc(sizeof(Link));
while(p->next!=NULL)
{
p=p->next;
}
memcpy(new,&newnode,sizeof(Link));
new->next=NULL;
p->next=new;
}
void printLinkNode(Link *head)
{
int cnt=0;
Link* p=head;
while(p->next!=NULL)
{
cnt++;
p=p->next;
printf("%d ",p->data);
}
putchar('\n');
}
void deleteLinkNode(Link* head,int data)
{
Link* p=head;
Link* tmp=NULL;
while(p->next!=NULL)
{
tmp=p;
p=p->next;
if(p->data==data)
{
tmp->next=p->next;
free(p);
p=head;
}
}
}
void searchLink(Link* head,int data)
{
Link* p=head;
while(p->next!=NULL)
{
if(p->data==data)
{
printf("找到对应的节点:%d\n",p->data);
}
p=p->next;
}
if(p->next!=NULL)
{
printf("未找到对应的节点\n");
}
}
void modifiLink(Link* head,int data,int newdata)
{
Link* p=head;
while(p->next!=NULL)
{
if(p->data==data)
{
p->data=newdata;
printf("把对应的节点修改为:%d\n",p->data);
}
p=p->next;
}
if(p->next!=NULL)
{
printf("未找到对应的节点\n");
}
}
二·链表二
#include
#include
#include
typedef struct Test
{
char name[32];
int number;
struct Test *next;
}Link;
Link* head=NULL;
Link*creatLinkNode(Link* head);
void AddLinkNode(Link* head,Link data);
void printLinkNode(Link* head);
void deleteLinkNode(Link* head,int num);
int main(int argc,char** argv)
{
Link data ={"xie",100};
Link data1={"wen",200};
Link data2={"hui",300};
head=creatLinkNode(head);
AddLinkNode(head,data);
AddLinkNode(head,data1);
AddLinkNode(head,data2);
printLinkNode(head);
deleteLinkNode(head,200);
printLinkNode(head);
return 0;
}
Link* creatLinkNode(Link* head)
{
if(head==NULL)
{
head=(Link*)malloc(sizeof(Link));
head->next=NULL;
}
return head;
}
void AddLinkNode(Link* head,Link data)
{
Link* p=head;
Link* new=(Link*)malloc(sizeof(Link));
while(p->next!=NULL)
{
p=p->next;
}
memcpy(new,&data,sizeof(Link));
p->next=new;
new->next=NULL;
}
void printLinkNode(Link* head)
{
Link* p=head->next;
while(p!=NULL)
{
printf("%s,%d\n",p->name,p->number);
p=p->next;
}
}
void deleteLinkNode(Link* head,int num)
{
Link* p=head;
Link* tmp=NULL;
while(p->next!=NULL)
{
tmp=p;
p=p->next;
if(p->number==num)
{
tmp->next=p->next;
free(p);
p=head;
}
}
}
三·链表三
#include
#include
#include
typedef struct Test
{
char name[32];
int number;
struct Test *next;
}Link;
Link* head=NULL;
Link* creatLink(Link* head);
void printLink(Link* head);
void AddLink(Link* head,Link data);
void deleteLink(Link* head,int num);
void saveLink(Link* head);
void getLink(Link* head);
#if 1
int main(int argc,char** argv)
{
Link data={"xie",100};
Link data1={"wen",200};
Link data2={"hui",300};
head=creatLink(head);
AddLink(head,data);
AddLink(head,data1);
AddLink(head,data2);
printLink(head);
deleteLink(head,100);
printLink(head);
saveLink(head);
getLink(head);
printLink(head);
return 0;
}
#endif
#if 0
int main(int argc,char** argv)
{
head=creatLink(head);
getLink(head);
printLink(head);
return 0;
}
#endif
Link* creatLink(Link* head)
{
if(head==NULL)
{
head=(Link*)malloc(sizeof(Link));
head->next=NULL;
}
return head;
}
void printLink(Link* head)
{
Link* p=head->next;
while(p!=NULL)
{
printf("%s,%d\n",p->name,p->number);
p=p->next;
}
}
void AddLink(Link* head,Link data)
{
Link* p=head;
Link* new=(Link*)malloc(sizeof(Link));
while(p->next!=NULL)
{
p=p->next;
}
memcpy(new,&data,sizeof(Link));
p->next=new;
new->next=NULL;
}
void deleteLink(Link* head,int num)
{
Link* p=head;
Link* tmp=NULL;
while(p->next!=NULL)
{
tmp=p;
p=p->next;
if(p->number==num)
{
tmp->next=p->next;
free(p);
p=head;
}
}
}
void saveLink(Link* head)
{
Link* p=head;
FILE *fp=NULL;
fp=fopen("./text.txt","w+");
if(fp==NULL)
{
printf("文件打开失败\n");
return;
}
while(p->next!=NULL)
{
fwrite(p,1,sizeof(Link),fp);
p=p->next;
}
fclose(fp);
}
void getLink(Link* head)
{
Link* p=head;
FILE* fp=NULL;
Link data={0};
fp=fopen("./text.txt","r+");
if(fp==NULL)
{
printf("文件打开失败\n");
return;
}
while(!feof(fp))
{
fread(&data,1,sizeof(Link),fp);
AddLink(head,data);
}
fclose(fp);
}
四·链表四
#include
#include
typedef struct ListNode
{
int data;
struct ListNode* next;
}ListNode;
ListNode* createNode(int data)
{
ListNode *node=(ListNode*)malloc(sizeof(ListNode));
node->data=data;
node->next=NULL;
return node;
}
ListNode* insertNodeHead(ListNode *head,int data)
{
ListNode *node=createNode(data);
node->next=head;
return node;
}
ListNode *insertNodeTail(ListNode *head,int data)
{
ListNode* node=createNode(data);
ListNode* point=head;
if(head==NULL)
{
return NULL;
}
while(point->next!=NULL)
{
point=point->next;
}
point->next=node;
return head;
}
ListNode* deleteNode(ListNode* head,int data)
{
ListNode* point=head;
if(head==NULL)
{
return NULL;
}
if(head->data==data)
{
head=head->next;
free(point);
return head;
}
while(point->next!=NULL&&point->next->data!=data)
{
point=point->next;
}
if(point->next!=NULL)
{
ListNode* node=point->next;
point->next=node->next;
free(node);
}
return head;
}
void updateNode(ListNode* head,int data,int newdata)
{
ListNode* point=head;
while(point!=NULL)
{
if(point->data==data)
{
point->data=newdata;
}
point=point->next;
}
}
void printNode(ListNode* head)
{
ListNode* point=head;
while(point!=NULL)
{
printf("%d ",point->data);
point=point->next;
}
putchar('\n');
}
void clearNode(ListNode* head)
{
while(head!=NULL)
{
ListNode* point=head;
head=head->next;
free(point);
}
}
int main(int argc,char** argv)
{
ListNode* head=NULL;
head=insertNodeHead(head,1);
head=insertNodeHead(head,2);
head=insertNodeTail(head,3);
printNode(head);
head=deleteNode(head,2);
printNode(head);
updateNode(head,1,4);
printNode(head);
clearNode(head);
return 0;
}