数据结构之单链表
来源:互联网 发布:mac eclipse 编辑:程序博客网 时间:2024/04/30 07:12
#include <iostream>
#include <windows.h>
#include <cstdlib>
using namespace std;
#define OK 1
#define ERROR 0
typedef struct LNode
{
int date;
struct LNode *next;
}LNode, *LinkList;
LinkList initList_L(LinkList&L, int n)//链表初始化
{
LNode *p, *q;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
p = (Lode*)malloc(sizeof(LNode));
p = L;
q = (Lode*)malloc(sizeof(LNode));
L->next = q;
q->next = NULL;
int i;
for (i = 1; i <= n; i++)
{
p = p->next; //指针后移
q = (Lode*)malloc(sizeof(LNode));
cin >> p->date;//输入数据
q->next = p->next; //接入结点
p->next = q;
q = p->next;
q->next = NULL;
}
return L;
}
int ListInsert_L(LinkList&L, int i, int E)//链表插入
{
LNode *p, *q;
p = (LNode*)malloc(sizeof(LNode));
p = L;
int j = 0;
while (p&&j < i - 1)
{
p = p->next;
j++;
}
if (!p || j>i - 1) return ERROR;
q = (LinkList)malloc(sizeof(LNode));
q->date = E;
q->next = p->next;
p->next = q;
return OK;
}
int ListDelete_L(LinkList&L, int i, int E)//链表删除
{
LNode *p, *q;
p = (LinkList)malloc(sizeof(LNode));
p = L;
int j = 0;
while (p&&j < i - 1)
{
p = p->next;
j++;
}
if (!p->next || j >i - 1) return ERROR;
q = (LinkList)malloc(sizeof(LNode));
q = p->next;
p->next = q->next;
E = q->date;
free(q);
return OK;
}
void Listdisplay_L(LinkList&L)//显示
{
LinkList p;
p = L;
p = p->next;
while (p->next!= NULL)
{
cout << p->date << " ";
p = p->next;
}
}
int main()
{
LinkList L;
int M;
cout << "请按键选择————————(0)插入————————(1)删除" << endl;
cin >> M;
system("cls");
switch (M)
{
case 0:
{int N, J, E, k;
cout << "请输入链表中元素的个数:" << endl;
cin >> N;
cout << "请输入链表中的元素:" << endl;
initList_L(L, N);
cout << "请输入插入元素的位置:" << endl;
cin >> J;
cout << "要插入的元素:" << endl;
cin >> E;
ListInsert_L(L, J, E);
Listdisplay_L(L);
cin >> k;
break; }
case 1:
{int a, b, c;
cout << "请输入链表中元素的个数:" << endl;
cin >> a;
cout << "请输入链表中的元素:" << endl;
initList_L(L, a);
cout << "请输入要删除元素的位置:" << endl;
cin >> b;
cout << "要删除的元素:" << endl;
cin >> c;
ListDelete_L(L, b, c);
Listdisplay_L(L);
break; }
}
system("pause");
return 0;
}
#include <windows.h>
#include <cstdlib>
using namespace std;
#define OK 1
#define ERROR 0
typedef struct LNode
{
int date;
struct LNode *next;
}LNode, *LinkList;
LinkList initList_L(LinkList&L, int n)//链表初始化
{
LNode *p, *q;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
p = (Lode*)malloc(sizeof(LNode));
p = L;
q = (Lode*)malloc(sizeof(LNode));
L->next = q;
q->next = NULL;
int i;
for (i = 1; i <= n; i++)
{
p = p->next; //指针后移
q = (Lode*)malloc(sizeof(LNode));
cin >> p->date;//输入数据
q->next = p->next; //接入结点
p->next = q;
q = p->next;
q->next = NULL;
}
return L;
}
int ListInsert_L(LinkList&L, int i, int E)//链表插入
{
LNode *p, *q;
p = (LNode*)malloc(sizeof(LNode));
p = L;
int j = 0;
while (p&&j < i - 1)
{
p = p->next;
j++;
}
if (!p || j>i - 1) return ERROR;
q = (LinkList)malloc(sizeof(LNode));
q->date = E;
q->next = p->next;
p->next = q;
return OK;
}
int ListDelete_L(LinkList&L, int i, int E)//链表删除
{
LNode *p, *q;
p = (LinkList)malloc(sizeof(LNode));
p = L;
int j = 0;
while (p&&j < i - 1)
{
p = p->next;
j++;
}
if (!p->next || j >i - 1) return ERROR;
q = (LinkList)malloc(sizeof(LNode));
q = p->next;
p->next = q->next;
E = q->date;
free(q);
return OK;
}
void Listdisplay_L(LinkList&L)//显示
{
LinkList p;
p = L;
p = p->next;
while (p->next!= NULL)
{
cout << p->date << " ";
p = p->next;
}
}
int main()
{
LinkList L;
int M;
cout << "请按键选择————————(0)插入————————(1)删除" << endl;
cin >> M;
system("cls");
switch (M)
{
case 0:
{int N, J, E, k;
cout << "请输入链表中元素的个数:" << endl;
cin >> N;
cout << "请输入链表中的元素:" << endl;
initList_L(L, N);
cout << "请输入插入元素的位置:" << endl;
cin >> J;
cout << "要插入的元素:" << endl;
cin >> E;
ListInsert_L(L, J, E);
Listdisplay_L(L);
cin >> k;
break; }
case 1:
{int a, b, c;
cout << "请输入链表中元素的个数:" << endl;
cin >> a;
cout << "请输入链表中的元素:" << endl;
initList_L(L, a);
cout << "请输入要删除元素的位置:" << endl;
cin >> b;
cout << "要删除的元素:" << endl;
cin >> c;
ListDelete_L(L, b, c);
Listdisplay_L(L);
break; }
}
system("pause");
return 0;
}
0 0
- 数据结构学习之单链表
- java数据结构之单链表
- 数据结构之单链表
- 数据结构学习之单链表
- 数据结构之单链表反转
- 数据结构之单链表
- 数据结构之, 单链表
- 数据结构之单链表
- 数据结构之单链表
- C++数据结构之单链表
- C++数据结构之单链表
- 数据结构之单链表
- 数据结构之单链表
- 大话数据结构之单链表
- C数据结构之单链表
- 数据结构基础之单链表
- javascript 数据结构 之 单链表
- 数据结构之单链表操作
- 数据结构之顺序表的操作
- Mantle JSONkeyPath辅助插件
- C语言输入输出函数详解
- 诚实人和说谎者 -- truth teller and liar
- 校友会攻略
- 数据结构之单链表
- swift控件代码
- 【IOS 开发学习总结-OC-34】★★文件 I/O——NSFileManager 管理文件和目录
- 原子性和可见性
- 数据结构之静态链表
- UI高级第五课 网络通信——iOS学习连载33
- Ios66详解之地图定位失败错误信息处理
- 省市区3个tableView联动实现
- 手机应用推荐