输入链表:
int creat(LinkList &L)//输入链表
{
L = new LNode;
L -> next = NULL;
int n, i;
cout << "请输入元素个数:" <<endl;
cin >> n;
for( i = 1; i <= n; i++)
{
LNode *p;
p = new LNode;
cout << " 请输入第" << i <<"的元素。" <<endl;
cin >> p -> data;
p -> next = L -> next;
L -> next = p;
}
}
输出链表 :
int print(LinkList &L)//输出链表
{
LNode *p;
p = L -> next;
while (p != NULL)
{
cout << p -> data << "-";
p = p -> next;
}
return ok;
}
删除元素 :
int del(LinkList &L)//删除元素
{
int i , j = 0;
cout << "请删除元素的插入位置:"<<endl;
cin >> i;
LNode *p = L, *q;
while (p -> next && j < i - 1)
{
p = p -> next;
++j;
}
q = p -> next;
p -> next = p -> next -> next;
return ok;
}
插入元素 :
int insert(LinkList &L)//插入元素
{
int i , j = 0;
LNode *e;
e = new LNode;
cout << "请输入所插入的元素:"<<endl;
cin >> e -> data;
cout << "请输入元素的插入位置:"<<endl;
cin >> i;
LNode *p = L;
while (p && j < i - 1)
{
p = p -> next;
++j;
}
e -> next = p -> next;
p -> next = e;
return ok;
}
查找元素和地址:
int get(LinkList &L)//查找元素
{
int i , j = 1;
LNode *p = L -> next;
cout << "输入要查找的位置: " <<endl;
cin >> i;
while ( p && j < i)
{
p = p -> next;
j++;
}
cout << p -> data << endl;
return ok;
}
int locate(LinkList &L)//查找地址
{
int e , i = 1;
LNode *p = L -> next;
cout << "输入要查找的位置: " <<endl;
cin >> e;
while (p && p -> data != e)
{
p = p -> next;
}
cout << "元素所在的地址为" <<p -> next << endl;
return ok;
}
综合:
#include <bits/stdc++.h>
using namespace std;
#define ok 1
#define error 0
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode*next;
}LNode,*LinkList;
int creat(LinkList &L)//输入链表
{
L = new LNode;
L -> next = NULL;
int n, i;
cout << "请输入元素个数:" <<endl;
cin >> n;
for( i = 1; i <= n; i++)
{
LNode *p;
p = new LNode;
cout << " 请输入第" << i <<"的元素。" <<endl;
cin >> p -> data;
p -> next = L -> next;
L -> next = p;
}
}
int get(LinkList &L)//查找元素
{
int i , j = 1;
LNode *p = L -> next;
cout << "输入要查找的位置: " <<endl;
cin >> i;
while ( p && j < i)
{
p = p -> next;
j++;
}
cout << p -> data << endl;
return ok;
}
int locate(LinkList &L)//查找地址
{
int e , i = 1;
LNode *p = L -> next;
cout << "输入要查找的位置: " <<endl;
cin >> e;
while (p && p -> data != e)
{
p = p -> next;
}
cout << "元素所在的地址为" <<p -> next << endl;
return ok;
}
int insert(LinkList &L)//插入元素
{
int i , j = 0;
LNode *e;
e = new LNode;
cout << "请输入所插入的元素:"<<endl;
cin >> e -> data;
cout << "请输入元素的插入位置:"<<endl;
cin >> i;
LNode *p = L;
while (p && j < i - 1)
{
p = p -> next;
++j;
}
e -> next = p -> next;
p -> next = e;
return ok;
}
int del(LinkList &L)//删除元素
{
int i , j = 0;
cout << "请删除元素的插入位置:"<<endl;
cin >> i;
LNode *p = L, *q;
while (p -> next && j < i - 1)
{
p = p -> next;
++j;
}
q = p -> next;
p -> next = p -> next -> next;
return ok;
}
int print(LinkList &L)//输出链表
{
LNode *p;
p = L -> next;
while (p != NULL)
{
cout << p -> data << "-";
p = p -> next;
}
return ok;
}
int main()
{
LinkList p;
int out = 1,choice;
while(out)
{
cout << endl << "1-------------批量链表";
cout << endl << "2-------------查找元素";
cout << endl << "3-------------查找地址";
cout << endl << "4-------------插入元素";
cout << endl << "5-------------删除元素";
cout << endl << "6-------------显示全表";
cout << endl << "0-------------退出系统";
cout << endl << "请选择";
cin >> choice;
switch (choice)
{
case 1: creat(p); break;
case 2: get(p); break;
case 3: locate(p); break;
case 4: insert(p); break;
case 5: del(p); break;
case 6: print(p); break;
case 0: out = 0;
}
}
}
|