#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;
}
单链表的实现(C++描述)
最新推荐文章于 2023-04-06 13:34:21 发布