单向链表基础例子

这篇博客介绍了单向链表的基础知识,包括增删改查等操作的实现,支持按值或位置进行操作,并提供了注释功能。文章还讨论了初始化头节点和完整链表的方法。
摘要由CSDN通过智能技术生成

单链表基础例子,实现增删改查读销,可选目标值或目标位置读、插、删,另有其他小功能如注释,未增加malloc后的检查NULL

更新,同时支持初始化1个头节点和初始化一个完整链表

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

/*
链表是一个由指针指向的结构体内存序列

功能ok,实现生成指定长度链表,增删改读插销毁

*/

typedef struct Node{
    struct Node * pnext;
    int val;

} node,*pnode;

pnode gen_node(int num);//生成链表
void show_node(pnode p_f);//打印链表
void add_elem(pnode phead_f,int val_f);//在链表尾加一个元素
void change_point(int val_f, int val_d, pnode p_head_f);//改变某个元素的值
void addin(int val_f, int val_d, pnode p_head_f);//在某元素后面插入一个元素
void delete(int val_f, pnode p_head_f);//删除某个元素
void destory_node(pnode p_head_f);//销毁一个链表
void add_before(pnode p_node, int val_d, int val_f);//add one element before val_d;
int pos_to_val(pnode p_node, int pos );//input position,return value of that position,an additional api
pnode init_stal_node(void);// upgrade:init a single head to meet some condition
void apd_node(pnode p_node, int val_f);//append one elememt

int main(void){

    int v = 0;
    int n = 0;
    int val_f;
    int	val_d;
    pnode NODE; 
    printf("input node num:\n");
    scanf("%d",&v);
    //NODE = init_stal_node();
    NODE = gen_node(v);
    show_node(NODE);//生成链表
    apd_node(NODE, 99);
   	show_node(NODE); 
    
    printf("input an int to put in node\n");
    scanf("%d",&n);
    add_elem(NODE, n);
    show_node(NODE);//在尾部增加
    
    printf("input point you want to change:\n");
    scanf("%d",&val_f);
    printf("input new point value:\n");
    scanf("%d",&val_d);
    change_point(val_f,val_d,NODE);
    show_node(NODE);//更改某个链表元素

    printf("input target val to add a val next to it:\n");
    scanf("%d",&val_f);
    printf("input the val you want to addin:\n");
    scanf("%d",&val_d);
    addin(val_f, val_d, NODE);
    show_node(NODE);//在某元素后插入新元素

    printf("input the value you want to delete:\n");
    scanf("%d",&val_f);
    delete(val_f, NODE);
    show_node(NODE);
    show_node(NODE);//删除1个元素


	printf("input a target val to add before:\n");
	scanf("%d",&val_d);
	printf("input a val to add before:\n");
	scanf("%d",&val_f);
	add_before(NODE,val_d,val_f);
	show_node(NODE);


	printf("input a pos\n");
	int pos;
	scanf("%d",&pos);
	printf("%d\n",pos_to_val(NODE,  pos ));


    destory_node(NODE);//销毁链表

	return 0;
}

pnode gen_node(int num){
    int i = 0;
    int val_f = 0;
    pnode phead;
    phead = (pnode)malloc(sizeof(node));
	if(phead == NULL){
		printf("failed to creat node\n");
		return NULL;
	}
    pnode ptemp;
    ptemp = phead;
    phead->val = 0;
    for(i = 0; i<num; i++){
		pnode pnew = (pnode)malloc(sizeof(node));
		
		if(pnew == NULL){
			printf("failed to creat new list\n");
			return NULL;
		}


		printf("input an int :\n");
		scanf("%d",&val_f);

		pnew->val = val_f;
		ptemp->pnext = pnew;
		pnew->pnext = NULL;
		ptemp = pnew;
    }
    return phead;
}


pnode init_stal_node(void){
	pnode p_head  = (pnode)malloc(sizeof(node));
	p_head->val = -99999;
	p_head->pnext = NULL;

	return p_head;

}

void show_node(pnode p_f){
    if(p_f->pnext == NULL){
    	printf("empty node\n");
    	return;
    }
    pnode p_temp;
    p_temp = p_f->pnext;
    printf("value in node is :\n");
    while(p_temp->pnext != NULL){
		printf("%d\n",(p_temp->val));
		p_temp = p_temp->pnext; 
    }
    printf("%d\n",(p_temp->val));
}

void add_elem(pnode phead_f,int val_f){
	pnode p_temp_a;
	p_temp_a = phead_f;
	pnode pnew;
	while(p_temp_a->pnext != NULL){
	    p_temp_a = p_temp_a->pnext;
	}
	pnew = (pnode)malloc(sizeof(node));
	if(p_new == NULL){
		printf("out of memory when add elemrnt\n")
		return;
	}
	pnew->val = val_f;
	p_temp_a->pnext = pnew;
	pnew->pnext = NULL;
	printf("add one elem after desired val done\n");
}

void change_point(int val_f, int val_d, pnode p_head_f){
    pnode p_temp;
    p_temp = p_head_f;

    while(p_temp->val != val_f){

		p_temp = p_temp->pnext;
		if (p_temp == NULL){
			printf("wrong val.no such num\n");
return;
		}
    }
    p_temp->val = val_d;

}

void addin(int val_f, int val_d, pnode p_head_f){
    pnode p_temp;
    p_temp = p_head_f;
    pnode p_new;
    while(p_temp->val != val_f){
		p_temp = p_temp->pnext;
		if( p_temp == NULL){
	
			printf("no such value !");
			return;
		}

    }

    p_new = (pnode)malloc(sizeof(node));
    p_new->val = val_d;
    p_new->pnext = p_temp->pnext;
    p_temp->pnext = p_new;

   
}

void delete(int val_f, pnode p_head_f){
    pnode p_temp;
    pnode p_temp_p;
    p_temp = p_head_f;
    while(p_temp->val != val_f){
		p_temp_p = p_temp;
		p_temp = p_temp->pnext;	

		if( p_temp == NULL){
	
			printf("no such value !\n");
			return;
		}

		
    }
    p_temp_p->pnext = p_temp->pnext;
    free(p_temp);
}

void destory_node(pnode p_head_f){
    pnode p_temp;
    p_temp = p_head_f;
    pnode p_temp_p;
    int i = 1;
    while(p_temp->pnext != NULL){
		p_temp_p = p_temp;
		p_temp = p_temp->pnext;
		p_temp_p->val = 0;
		free(p_temp_p);
		printf("%d element destoryed\n",i);
		i++;
    }
	if(p_temp != NULL)
		free(p_temp);
    printf("last element destoryed\n"); 
}

void add_before(pnode p_node, int val_d, int val_f){
	pnode p_temp;
	pnode p_new;
	pnode p_temp_p;
	p_new = (pnode)malloc(sizeof(node));
	p_temp = p_node;
	
	while(p_temp->val != val_d){
		p_temp_p = p_temp;
		p_temp = p_temp->pnext;
		if( p_temp == NULL){
	
			printf("no such value !\n");
			return;
		}
				
	}
	p_new->val = val_f;
	p_new->pnext = p_temp;
	p_temp_p->pnext = p_new;
	printf("add one element before desired val done\n");	
}

int pos_to_val(pnode p_node, int pos ){

	pnode p_temp = p_node->pnext;
	int i = 0;
	for (i = 0; i<pos; i++){
		p_temp = p_temp->pnext;
		if(p_temp->pnext == NULL){
			printf("no such position ,try smaller pos\n");
			return -99999;
		}
	}
	printf("val:%d\n",p_temp->val);
	return p_temp->val;


}

void apd_node(pnode p_node, int val_f){
	pnode p_temp = p_node;
	pnode p_new = (pnode)malloc(sizeof(node));
	while(p_temp->pnext != NULL){
		p_temp = p_temp->pnext;
	}
	p_new->val = val_f;

	p_new->val = val_f;
	p_new->pnext = NULL;
	p_temp->pnext = p_new;
	printf("append one element done \n");



}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值