单链表代码
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define ERROR 1
#define MAX 100 //定义最大初始化长度
#define DELAY 2 //定义延时单位秒
typedef int ElemType;
typedef int STATUS;
typedef struct Linknode{ //单链表结构体
int data;
struct Linknode * next;
} Link;
/**********************
创建
输入:初始化数据个数
输出:链表指针
功能:返回一个带数据的链表的指针
**********************/
Link * createLink(int num){
if(num<=0||num>MAX){
printf("num is invalid,now num is set 3\n");
num = 3;
}
int i;
Link * head, *tmp, *new;
//初始化头结点
head = malloc(sizeof(Link));
printf("link[0]:");
scanf("%d",&head->data);
head->next = NULL;
//初始化链表
tmp = head;
for(i=1;i<num;i++){
new = malloc(sizeof(Link));
printf("link[i]:");
scanf("%d",&new->data);
new->next = NULL;
tmp->next = new;
tmp = tmp->next;
}
return head;
}
/**********************
查询值
输入:链表指针,查询位置
输出:查询结果
功能:返回指定位置的数据
**********************/
ElemType getValue(Link * head,int index){
index--;
if(index<0 || index>=MAX){
printf("index is invalid\n");
return ERROR;
}
Link * tmp = head;
int i=0;
while(tmp){
if(i == index){
printf("value is %d\n",tmp->data);
return 1;
}
tmp = tmp->next;
i++;
}
printf("sorry index is not found\n");
return ERROR;
}
/**********************
查询位置
输入:链表指针,查询值
输出:查询结果
功能:返回指定值的位置
**********************/
ElemType getIndex(Link * head,ElemType value){
int i = 0;
Link * tmp = head;
while(tmp){
if(tmp->data == value){
printf("Index is %d\n",i+1);
return 0;
}
tmp=tmp->next;
i++;
}
printf("Sorry value is not found!\n");
return 0;
}
/**********************
查询节点
输入:链表指针,查询值
输出:节点
功能:返回指定值的节点
**********************/
Link * findNode(Link * head,int value){
Link * tmp = head;
while(tmp){
if(tmp->data == value)return tmp;
tmp = tmp->next;
}
return NULL;
}
/**********************
向前插入
输入:链表指针,目标节点,插入值
输出:链表指针
功能:在指定位置节点的前面插入值
**********************/
Link * insertBefore(Link * head,Link * target,ElemType value){
Link * tmp = head;
Link * new = malloc(sizeof(Link));
new->data=value;
if(target == head){
new->next = head;
return head;
}
while(tmp->next != target)tmp=tmp->next;
new->next = target;
tmp->next = new;
return head;
}
/**********************
向后插入
输入:链表指针,目标节点,插入值
输出:状态码
功能:在指定位置节点的后面插入值
**********************/
STATUS insertAfter(Link * target,ElemType value){
Link * new = malloc(sizeof(Link));
new->data = value;
new->next = target->next;
target->next = new;
return OK;
}
/**********************
追加节点
输入:链表指针,追加值
输出:状态码
功能:在指定位置节点的后面插入值
**********************/
STATUS appendNode(Link * head,ElemType value){
Link * tmp = head;
Link * new = malloc(sizeof(Link));
new->data = value;
new->next = NULL;
while(tmp->next)tmp=tmp->next;
tmp->next = new;
return 0;
}
/**********************
替换数据
输入:链表指针,目标节点
输出:状态码
功能:在指定值的节点上更改数据
**********************/
STATUS replaceNode(Link *target,ElemType value){
target->data=value;
return 0;
}
/**********************
删除节点
输入:链表指针,目标节点
输出:链表指针
功能:将指定值的节点删除
**********************/
Link * deleteNode(Link * head,Link * target){
if(head == target)return head->next;
Link * tmp = head;
while(tmp->next != target)tmp=tmp->next;
tmp->next = target->next;
return head;
}
/**********************
释放链表
输入:链表指针
输出:无
功能:将链表内存空间释放
**********************/
void freeLink(Link *head){
Link * tmp = malloc(sizeof(Link));
while(head){
tmp = head;
head = head->next;
free(tmp);
}
}
/**********************
打印链表
输入:链表指针
输出:无
功能:将链表的值一一输出
**********************/
void printLink(Link * head){
Link * tmp = head;
while(tmp){
printf("[%d] ",tmp->data);
tmp = tmp->next;
}
printf("\n");
}
/**********************
打印信息
输入:无
输出:无
功能:打印链表的操作信息
**********************/
void printInfo(){
system("clear");
printf("\n");
printf("---------------------------\n");
printf(" a = append \n");
printf(" g = getValue \n");
printf(" G = getIndex \n");
printf(" i = insertBefore \n");
printf(" I = insertAfter \n");
printf(" r = replace \n");
printf(" d = delete \n");
printf(" e = exit \n");
printf("===========================\n");
}
int main(){
int num,index,value;
char select;
printf("how many num are you want:");
scanf("%d",&num);
Link * head = createLink(num);
Link * tmp = malloc(sizeof(Link));
while(1){
printInfo();
printLink(head);
printf("please enter your choose:");
select = getchar();
if(select == 'a'){
printf("please enter value:");
scanf("%d",&value);
appendNode(head,value);
}
else if(select == 'r'){
printf("please enter old value:");
scanf("%d",&value);
tmp = findNode(head,value);
printf("please enter new value:");
scanf("%d",&value);
replaceNode(tmp,value);
}
else if(select == 'g'){
printf("please enter index:");
scanf("%d",&index);
getValue(head,index);
sleep(DELAY);
}
else if(select == 'G'){
printf("please enter value:");
scanf("%d",&value);
getIndex(head,value);
sleep(DELAY);
}
else if(select == 'i'){
printf("enter the old value:");
scanf("%d",&value);
tmp = findNode(head,value);
if(!tmp){
printf("Sorry value is not found\n");
sleep(DELAY);
continue;
}
printf("enter the new value:");
scanf("%d",&value);
head = insertBefore(head,tmp,value);
}
else if(select == 'I'){
printf("enter the old value:");
scanf("%d",&value);
tmp = findNode(head,value);
if(!tmp){
printf("sorry value is not found\n");
sleep(DELAY);
continue;
}
printf("enter the new value:");
scanf("%d",&value);
insertAfter(tmp,value);
}
else if(select == 'd'){
printf("enter the old value:");
scanf("%d",&value);
tmp = findNode(head,value);
head = deleteNode(head,tmp);
}
else if(select == 'e'){
free(head);
exit(0);
}
}
}
程序员最高境界:静若瘫痪,动若癫痫