# 单链表操作（纯代码）

## 单链表操作（附带菜单）

#include<iostream>
#include <stdio.h>
using namespace std;

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

typedef int ElemType;//单链表结点date成员类型
typedef int Status;//函数返回值类型

typedef struct LNode{
ElemType data;
struct LNode *next;

Status GetElem_L(LinkList L, int i, ElemType &e){
int j = 1;
while(p && j<i){
p = p->next;
j++;
}
if(!p||j>i) return ERROR;
e = p->data;
return OK;
}//获取第i个结点

Status ListInsert_L(LinkList &L, int i, ElemType e){
int j = 0;
while(p && j<i-1){
p = p->next;
j++;
}
if(!p||j>i-1) return ERROR;
s->data = e;
s->next = p->next;
p->next = s;
return OK;
}//插入结点到第i个位置

int j = 0;
while(p && j<i-1){
p = p->next;
j++;
}
if(!p || j>i-1) return ERROR;
p->next = p->next->next;
return q->data;
free(q);
}//删除第i个结点

void ListUpdate_L(LinkList &L, int i, ElemType e){
int j = 0;
while(p && j<i){
p = p->next;
j++;
}
if(!p || j>i) return;
p->data = e;
}//修改第i个结点

while(p){
p = p->next;
t->next = q;
q = t;
}
L->next = q;
}//翻转单链表

L->next = NULL;
for(int i=0;i<n;i++){
scanf("%d",&p->data);
p->next = L->next;
L->next = p;
}
}//头插法创建单链表

if(!q) return;
while(q){
while(p){
if(p->data < q->data){
int t = p->data;
p->data = q->data;
q->data = t;
}
p = p->next;
}
q = q->next;
}
}//链表排序

C->next = NULL;
while(p && q){
if(q->data >= p->data){
s->data = p->data;
s->next = r->next;
r->next = s;
p = p->next;
}else{
s->data = q->data;
s->next = r->next;
r->next = s;
q = q->next;
}
r = r->next;
}
while(p){
s->data = p->data;
s->next = r->next;
r->next = s;
p = p->next;
r = r->next;
}
while(q){
s->data = q->data;
s->next = r->next;
r->next = s;
q = q->next;
r = r->next;
}
}//合并两个单链表并按降序排序

p = p->next;
while(p){
printf("%d->",p->data);
p = p->next;
}
printf("NULL\n");
}//打印单链表

typedef struct{
ElemType length;
}Repository;//单链表仓库数据结构，顺序表

if(R.length<50){
R.list[R.length]=L;
R.length++;
}else {
printf("仓库已满！！！");
}
}//向仓库添加单链表

void PrintRepository(Repository R){
for(int i=0;i<R.length;i++){
printf("%d:",i+1);
PrintList_L(R.list[i]);
}
}//打印仓库中的单链表

PrintRepository(R);
printf("   请选择要删除的链表");
int j;
scanf("%d",&j);
if(R.length==0) return;
if(j<1||j>R.length) return;
for(int i = j;i<R.length;i++){
R.list[i-1] = R.list[i];
}
R.length--;
}//删除仓库中的单链表

if(i<1||i>R.length) return NULL;
else return R.list[i-1];
}//获取单链表

while(1){
system("cls");
int i,n;
int w,e;
printf("   单链表操作\n");
printf(" == == == == == == \n");
printf("   1.创建单链表\n");
printf("   2.查看单链表仓库\n");
printf("   3.插入结点\n");
printf("   4.删除结点\n");
printf("   5.修改结点\n");
printf("   6.查找结点\n");
printf("   7.合并链表\n");
printf("   8.删除链表\n");
printf("   9.链表排序\n");
printf("   10.翻转链表\n");
printf(" == == == == == == \n");
printf("   请选择操作:");
scanf("%d",&i);
switch(i){
case 1:
printf("   输入初始大小:");
scanf("%d",&n);
CreateList_L(l,n);
break;
case 2:
PrintRepository(R);
break;
case 3:
PrintRepository(R);
printf("   选择链表:");
scanf("%d",&i);
printf("   插入位置和元素值:");
scanf("%d%d",&w,&e);
ListInsert_L(l,w,e);
break;
case 4:
PrintRepository(R);
printf("   选择链表:");
scanf("%d",&i);
printf("   删除位置:");
scanf("%d",&w);
ListDelete_L(l,w);
break;
case 5:
PrintRepository(R);
printf("   选择链表:");
scanf("%d",&i);
printf("   修改位置和修改值:");
scanf("%d%d",&w,&e);
printf("修改前:");
PrintList_L(l);
ListUpdate_L(l,w,e);
printf("修改后:");
PrintList_L(l);
break;
case 6:
PrintRepository(R);
printf("   选择链表:");
scanf("%d",&i);
printf("   查找位置:");
scanf("%d",&w);
GetElem_L(l,w,e);
printf("第%d个结点是%d\n",w,e);
break;
case 7:
PrintRepository(R);
printf("   选择两个链表:");
int j;
scanf("%d%d",&i,&j);
MergeList_L(l,p,q);
break;
case 8:
break;
case 9:
PrintRepository(R);
printf("   选择链表:");
scanf("%d",&i);
SortList_L(l);
break;
case 10:
PrintRepository(R);
printf("   选择链表:");
scanf("%d",&i);
ListReverse_L(l);
break;
}
system("pause");
}
}//菜单函数
void main(){
Repository repository;
repository.length = 0;
}


09-23 63

02-11 178
12-18 407
09-20 2万+
02-09 1283
04-03 74
02-09 230
07-23 6573
10-30 1783