单/双向链表相关的操作

单链表/双向链表的操作

单链表

typedef struct node{
	int data;
	struct node* next;
}Lnode;

结构体如上 需要定义一个


◼ 带头节点(不能直接l=list !)

操作如下:
① 初始化

void init(Lnode *list){
	list=(Lnode*)malloc(sizeof(Lnode));
	if(*list==NULL){ 
		printf("error");
		exit(0);
	}
	list->next==NULL;
}

②尾操作

尾插

void tail_add(Lnode *list,int i){
	Lnode *p=(Lnode*)malloc(sizeof(Lnode));
	Lnode *q=list;
	p->data=i;
	p->next=NULL;
	if(list==NULL) return p;
	else {
		while(q->next!=NULL) q=q->next;
		q->next=p;
		q=p;
	}
	return q;
}

尾删

void tail_dele(Lnode* list){
	if(list==NULL) {
		printf("error");
		return;
	}
	else if(list->next==NULL){
		free(list);
		return;
	}
	else{
		Lnode* p;
		while(list!=NULL) list=list->next;
		p=list;
		p->next=NULL;
		free(list);
	}
}

③头操作

头插

void head_add(Lnode* list,int i){
	Lnode *p=(Lnode*)malloc(sizeof(Lnode));
	p->data=i;
	p->next=NULL;
	if(list==NULL) return p;
	else{
		p->next=list->next;
		list->next=p;
	}
	return list;

头删

void head_dele(Lnode* list){
	Lnode* p_head=list->next;
	if(list==NULL) {
		printf("error");
		return;
	}
	else{
		list=list->next;
		free(p_head)'
	}
}

④ 中间某处插入数据

void add(Lnode* list,int w,int da_ta){
	if(list==NULL) printf("error");
	int tmp=0;
	Lnode* p=list;
	while(p){
		tmp++;
		p=p->next;
	}
	if(list==NULL && w>tmp && w<0){
		printf("error“);
	}
	else{
			int w1=0
			while(w1<w){
				w1++
				list=list->next;
			}
			Lnode* new=(Lnode*)malloc(sizeof(Lnode));
			new->next=list->next;
			list->next=new;
		}
}

⑤ 打印链表

//遍历链表 然后printf所有节点值
void print_list(Lnode *list){
	Lnode *l=list->next;
	while(l){
		printf("%d ",l->data);
		l=l->next;
	)
	printf("\n");
}

⑥ 查找链表中的某个数据

//遍历然后对比查找
Lnode* find(Lnode *list,int i){
	Lnode *l=list->next;
	while(l){
		if(l->data==i) return l;
		else l=l->next;
	}
	printf("error");
	return NULL;
}

⑦ 删除中间某节点 / 整个链表

void dele(Lnode *list,int i){
	Lnode *l=list;
	Lnode *p;
	if(l==NULL){
		printf("error");
		return ;
	}
	else{
		while(l){
			if(l->data==i){
				p=l;
				int *e=p->data;
				l=p->next;
				free(p);
			}
			list=list->next;
		}
}		
void dele_list(Lnode* list){
	Lnode *p;
	while(list){
		p=list->next;
		free(list);
		list=p;
	}
}

双向链表

◼带头节点
结构体

typedef struct Lnode{
	struct Lnode *prior;
	struct Lnode *next;
	int data;
}DLnode;

①初始化

void init(DLnode* list){
	DLnode *L=(DLnode*)malloc(sizeof(DLnode));
	L->next=L->prior=NULL;
}

②尾插

void tail_add(DLnode* list,int i){
	if(list==NULL) return L;
	else {
		while(list->next!=NULL) list=list->next;
		DLnode* L=(DLnode*)malloc(sizeof(DLnode));
		L->data=i;
		L->next=NULL;
		list->next=L;
		L->prior=list;
		list=L;
	}
}

③头插法

//传入 *list,数据i
{
	new=malloc;
	new->data=i
	new->next=list->next;
	new->next->prior=new;
	new->prior=list;
	list->next=new;
}

④中间插入某数据

//传入 *list
DLnode* new=(DLnode*)malloc(sizeof(DLnode));
if(只有头节点) {
	list->next=new;
	new->prior=list;
	new->next=NULL;
}
else{//移到目标位置的前一处
	new->next=list->next;
	list->next->prior=new;
	list->next=new;
	new->prior=list;
}

⑤删除某数据

{
	//while循环找到要删除的节点 找不到则退出
	//令要删除的节点为p
	p->prior->next=p->next;
	p->next->prior=p->prior;
	free(p);
}

◼循环链表就需要将最后一个节点的指针指向头节点 判断表尾条件为node->next=head

你可能感兴趣的:(c基础学习,c语言)