head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum passworld{SUCCESE,FLASE=-1};
typedef int datatype;
//单链表的创建
typedef struct node//node不可省略,因为定义指针域要用到
{
//数据域(可以和指针域交换位置)
datatype data;
//指针域,节点之间的关系,指向下一个节点
struct node *next;
}*node_t;
node_t create_node();
void output(node_t head);
int lence(node_t head);
node_t delete_head(node_t head);
node_t del_last(node_t head);
node_t inter_last(node_t head,datatype element);
node_t newnode(node_t head,datatype element,int pos);
//单链表的头插
node_t insert_head(datatype element,node_t head);
node_t del_pos(node_t head,int pos);
node_t fine(node_t head,int pos);
void fine_ele(node_t head,datatype element);
node_t reverse(node_t head);
node_t change_ele(node_t head,datatype element,int pos);
node_t del_ele(node_t head,datatype element);
void count_back(node_t head,int pos);
node_t freenode(node_t head);
node_t change(int pos,datatype element,node_t head);
#endif
aa.c
#include "head.h"
//创建新节点 create_node()
node_t create_node()
{
node_t s=(node_t)malloc(sizeof(struct node));
if(NULL==s)
return NULL;
s->data=0;
s->next=NULL;
return s;
}
//计算长度
int lence(node_t head)
{
int i=0;
node_t p=head;
while(p)
{
p=p->next;
i++;
}
return i;
}
//输出
void output(node_t head)
{
if(head==NULL)//判断列表是否为空
{
return;
}
node_t p=head;
while(p!=NULL)//或者while(p)
{
printf("%d\t",p->data);
p=p->next;
}
}
//单链表的头插 insert_head()
node_t insert_head(datatype element,node_t head)
{
node_t s=create_node();
s->data=element;
//判断列表是否空,也就是插入的是第一个值
if(NULL==head)
{
head=s;
return head;
}
else
{
s->next=head;
head=s;
}
return head;
}
//头删
node_t delete_head(node_t head)
{
if(head==NULL)
return NULL;
else
{
node_t p=head;
head=head->next;
free(p);
p=NULL;
return head;
}
}
//尾插
node_t inter_last(node_t head,datatype element)
{
node_t s=create_node();
s->data=element;
if(head==NULL)
{
head=s;
return head;
}
else
{
node_t p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=s;
return head;
}
}
//尾删
node_t del_last(node_t head)
{
if(head==NULL)
{
return head;
}
else if(head->next==NULL)
{
free(head);
head=NULL;
}
else
{
node_t del=head;
while(del->next->next!=NULL)
{
del=del->next;
}
free(del->next);
del->next=NULL;
}
}
//插入节点
node_t newnode(node_t head,datatype element,int pos)
{
int len=lence(head);
if(pos==1)
{
head=insert_head(element,head);
return head;
}
else if(pos<1||pos>len)
{
printf("不合法");
return head;
}
else
{
node_t p=head;
int count=2;
while(count<pos)
{
p=p->next;
count++;
}
node_t s=create_node();
s->data=element;
s->next=p->next;
p->next=s;
return head;
}
}
//按位置删
node_t del_pos(node_t head,int pos)
{
int len=lence(head);
node_t p=head;
if(head==NULL||pos>len)
{
printf("数据非法");
return head;
}
else if(pos==1)
{
head=delete_head(head);
return head;
}
else
{
int count=2;
while(count<pos)
{
count++;
p=p->next;
}
node_t s=p->next;
p->next=p->next->next;
free(s);
s=NULL;
return head;
}
}
//按位置修改
node_t change(int pos,datatype element,node_t head)
{
int len=lence(head);
if(head==NULL)
{
head->data=element;
return head;
}
else if(pos<1||pos>len)
{
printf("数据非法");
return head;
}
else
{
int count;
node_t s=head;
for(count=1;count<pos;count++)
{
s=s->next;
}
s->data=element;
return head;
}
}
//按位置查找
node_t fine(node_t head,int pos)
{
node_t p=head;
int i=1;
if(head==NULL)
{
printf("无数据");
return head;
}
else
{
while(i<pos)
{
p=p->next;
i++;
}
printf("要查找的值为:%d\n",p->data);
return head;
}
}
//按元素查找
void fine_ele(node_t head,datatype element)
{
node_t p=head;
int len=lence(head);
if(head==NULL)
{
printf("数据空");
}
else
{
for(int i=1;i<=len;i++)
{
if(p->data==element)
{
printf("在%d位有该元素",i);
}
p=p->next;
}
}
}
//按元素删除
node_t del_ele(node_t head,datatype element)
{
int len=lence(head);
node_t p=head;
int i=1;
while(p!=NULL)
{
if(p->data==element)
{
del_pos(head,i);
i--;
}
i++;
p=p->next;
}
return head;
}
//按元素修改
node_t change_ele(node_t head,datatype element,int pos)
{
if(head==NULL)//判断是否为空
{
printf("数据为空");
return head;
}
else
{
node_t s;
s=head;
while(s!=NULL)
{
if(s->data==element)//是否有值与要修改的元素相等
{
s->data=pos;
s=s->next;
}
else
{
s=s->next;
}
}
return head;
}
}
//逆置
node_t reverse(node_t head)
{
if(head==NULL||head->next==NULL)//排除空和仅有一个元素
{
return head;
}
else
{
node_t p=head->next;//现在p是从head->next开始的链式表
node_t s=head;//s是从head开始的链式表
head->next=NULL;//断开head与head->next的链接,现在有独立的s与p
while(p!=NULL)
{
//对s进行头插
s=insert_head(p->data,s);//传的p的值和s的链表
p=p->next;
}
return s;
}
}
//找倒数第n个节点
void count_back(node_t head,int pos)
{
int len=lence(head);
if(head==NULL)
{
printf("无数据");
}
else if(pos>len)
{
printf("数据非法");
}
else
{
node_t s=head;
node_t p=head;
int count=0;
while(count<pos)
{
p=p->next;
count++;
}
while(p!=NULL)
{
s=s->next;
p=p->next;
}
printf("倒数第%d个节点是:%d",pos,s->data);
}
}
//单链表释放内存
node_t freenode(node_t head)
{
if(head==NULL)
{
return head;
}
else
{
node_t p=head;
while(p!=NULL)
{
//循环头删
delete_head(head);
return head;
}
}
}
主函数main->a.c
#include "head.h"
int main(int argc, const char *argv[])
{
//单链表头插入
node_t head=NULL;
int n;
int pos;
//设置链表长度
printf("enter n:");
scanf("%d",&n);
datatype element;
//进行头插
for(int i=0;i<n;i++)
{
printf("enter %d element:",i+1);
scanf("%d",&element);
head=insert_head(element,head);
}
output(head);
printf("\n");
//头删(一个)
/*
head=delete_head(head);
output(head);
printf("\n");
*/
/*
printf("inter last element:");
scanf("%d",&element);
inter_last(head,element);
output(head);
printf("\n");
*/
//尾插
/* printf("在尾部插入element:");
scanf("%d",&element);
inter_last(head,element);
output(head);
printf("\n");
*/
//尾删
/* printf("删除尾部:\n");
del_last(head);
output(head);
printf("\n");
*/
//选择性插入
/*
int pos;
printf("选择一个位置进行插入:");
scanf("%d",&pos);
printf("输入要插入的数:");
scanf("%d",&element);
printf("插入后的结果:");
head=newnode(head,element,pos);
output(head);
printf("\n");
*/
//按位置删
/* printf("选择删除的位置:");
scanf("%d",&pos);
del_pos(head,pos);
output(head);
printf("\n");
*/
//按位置修改
/*
printf("请输入要修改的位置:");
scanf("%d",&pos);
printf("请输入要修改的值:");
scanf("%d",&element);
change(pos,element,head);
output(head);
printf("\n");
*/
//按位查抄
/*
printf("请输入要查找的位置:");
scanf("%d",&pos);
fine(head,pos);
*/
//元素查
/*
printf("输入需要查找的元素:");
scanf("%d",&element);
fine_ele(head,element);
printf("\n");
*/
//元素删除
/*
printf("输入需要删除的元素:");
scanf("%d",&element);
del_ele(head,element);
output(head);
printf("\n");
*/
//元素修改
/*
printf("输入要修改的元素:");
scanf("%d",&element);
printf("要修改为::");
scanf("%d",&pos);
head=change_ele(head,element,pos);
output(head);
printf("\n");
*/
//进行倒序
/*
printf("以下为倒序结果:");
head=reverse(head);
output(head);
printf("\n");
*/
//找倒数第n个节点
printf("输入要找的倒数的节点:");
scanf("%d",&pos);
count_back(head,pos);
printf("\n");
//释放
freenode(head);
printf("%d",head->data);
return 0;
}
单链表按位置修改

单链表按位置查找

单链表按元素查找

单链表按元素删除

单链表按元素修改

倒序

找倒数第n个节点

释放

623

被折叠的 条评论
为什么被折叠?



