单链表的实现(C++描述)

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <fstream>
#include <cassert>
#include <string>
using namespace std;
#define max_length_list									//链表长度
#ifdef max_length_list
#define N 10
#endif
typedef int mytype;										//value数据类型
typedef struct node										//链表结点数据结构
{
	mytype val;
	node *next;
}node;
void init_list(node *& L);								//初始化链表
void create_list_front(node *&L);						//头插法
void create_list_back(node *&L);						//尾插法
void display_list(node *L);								//打印链表
bool empty_list(node *L);								//判断链表空
int	 length_list(node *L);								//链表长度
int	 search_list(node *L,mytype val);					//查找value位置
bool getval_list(node *L,int key,mytype &val);			//获取指定位置value
bool insert_list(node *&L,int key,mytype val);			//在指定位置插入value
bool delete_val_list(node *&L,int key,mytype &val);		//删除指定位置并获得value
void delete_list(node *&L);								//删除链表
void reverse_list(node *&L);                            //逆序
int main()
{
	node *L;
	init_list(L);
	create_list_back(L);
//	create_list_front(L);
	display_list(L);
	mytype c;
	cout << delete_val_list(L,search_list(L,5),c) << endl;
	cout << c << endl;
	cout << endl;
	display_list(L);
	c = 5;
	insert_list(L,5,c);
	display_list(L);
	delete_list(L);
	return 0;
}
void init_list(node *& L)
{
	L = new node;
	L -> next = NULL;
}
void create_list_front(node *&L)
{
	for(int i = 0;i < N;i++)
	{
		node *p = new node;
		cin >> p -> val;
		p -> next = L -> next;
		L -> next = p;
	}
}
void create_list_back(node *&L)
{
	node *pre,*p;
	pre = L;
	for(int i = 0;i < N;i++)
	{
		p = new node;
		cin >> p -> val;
		pre -> next = p;
		pre = p;
	}
	pre -> next = NULL;
}
void display_list(node *L)
{
	node *p = L -> next;
	while(p != NULL)
	{
		cout << p -> val << endl;
		p = p -> next;
	}
	cout << endl;
}
bool empty_list(node *L)
{
	return (L -> next == NULL);
}
int length_list(node *L)
{
	int len = 0;
	node *p = L;
	while(p -> next != NULL)
	{
		len++;
		p = p -> next;
	}
	return len;
}
int search_list(node *L,mytype val)
{
	int key = 1;
	node *p = L -> next;
	while(p != NULL && p -> val != val)
	{
		p = p -> next;
		key++;
	}
	if(p == NULL)
		return 0;
	else return key;
}
bool getval_list(node *L,int key,mytype &val)
{
	if(key <= 0)return 0;
	node *p = L;
	for(int i = 0;i < key && p != NULL;i++)
		p = p -> next;
	if(p == NULL)return 0;
	else 
	{
		val = p -> val;
		return 1;
	}
}
bool insert_list(node *&L,int key,mytype val)
{
	if(key <= 0)return 0;
	node *pre = L;
	for(int i = 0;i < key - 1 && pre != NULL;i++)
		pre = pre -> next;
	if(pre == NULL)return 0;
	else 
	{
		node *p = new node;
		p -> val = val;
		p -> next = pre -> next;
		pre -> next = p;
		return 1;
	}
}
bool delete_val_list(node *&L,int key,mytype &val)
{
	if(key <= 0)return 0;
	node *pre = L;
	for(int i = 0;i < key - 1 && pre != NULL;i++)
		pre = pre -> next;
	if(pre == NULL)return 0;
	else 
	{
		node *p = new node;
		p = pre -> next;
		if(p == NULL)return 0;
		cout << p->val <<endl;
		val = p -> val;
		pre ->next = p -> next;
		delete p;
		return 1;
	}
}
void delete_list(node *&L)
{
	node *pre = L,*p = L -> next;
	while(p != NULL)
	{
		delete pre;
		pre = p;
		p = p->next;
	}
	delete pre;
}
void reverse_list(node *&L)
{
    node *p = L->next,*prev = NULL,*next;
    while(p != NULL)
    {
        next = p -> next;
		p -> next = prev;
		prev = p;
		p = next;
	}
    L->next = prev;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值