输入线性表数据,编写完成下列功能的程序:
- (1)先建立单链表结构(包括初始化,销毁链表结构;节点的插入、删除和查找功能);
- (2)找到表中数据最大的结点,并输出其数据与序号。
效果:
c++代码:
#include<iostream>
using namespace std;
typedef struct LNode{
int data;
struct LNode* next;
}LNode,*LinkList;
bool InitList(LinkList &L);
void CreateList_R(LinkList &L, int n);
bool GetElem(LinkList L, int i,int &e);
int LocateElem(LinkList L, int e);
bool ListInsert(LinkList &L, int i, int e);
int Max(LinkList &L);
bool ListDelete(LinkList &L, int i);
void ClearList(LinkList &L);
void print(LinkList &L);
void MaxData(LinkList &L);
int main()
{
LinkList L;
int n;
cout << "====================" << endl << "(1).(Succeed)空链表初始化成功!:" << endl << endl;
cout << endl<<"====================" << endl << "(2).单链表的尾插入(在空链表上操作):" << endl << endl;
cout << "请输入要插入的数据个数:";
cin >> n;
cout << "请输入要插入的数据(以空格分隔):";;
CreateList_R(L, n);
cout << "当前链表长度为:" << L->data << endl;
cout << "打印当前链表:";
print(L);
cout<< endl<<"====================" << endl<<"(3)查找(根据数据值得到序号):" << endl;
int k;
cout << "请输入数据的值:" <<endl;
cin >> k;
int num = LocateElem(L, k);
if (num){
cout <<"数据"<<k<< "的序号为" << num << endl;
}else{
cout << "没有找到该数据!" << endl;
}
cout << endl<<"====================" << endl<< "(4)插入:" << endl;
cout << "请输入要插入的序号:" << endl;
int j1, k1;
cin >> j1;
cout << "请输入要插入的数据:" << endl;
cin>> k1;
if (ListInsert(L, j1, k1)){
cout << "插入成功"<<endl;
}else{
cout << "插入位置不合法" << endl;
}
cout << "打印当前链表:";
print(L);
cout<<endl<<"=============================="<<endl<<"(5)得到表中最大结点的数据与序号:"<<endl;
MaxData(L);
cout << endl<<endl<<"====================" << endl<< "(6):请输入要删除数据的序号:" << endl;
int j2;
cin >> j2;
if (ListDelete(L, j2)){
cout << "删除成功!" << endl;
}else{
cout << "删除位置不合法!" << endl;
}
cout << "打印当前链表:";
print(L);
cout << endl<<"====================" << endl<< "(7):链表清空:" << endl;
ClearList(L);
cout << "链表已清空,当前为空表!" << endl << "程序结束!"<< endl;
system("pause");
return 0;
}
void print(LinkList &L)
{
for (int i = 1;i <= L->data;i++)
{
int e;
GetElem(L, i, e);
cout << e << " ";
}
cout << endl;
}
bool InitList(LinkList &L)
{
L = new LNode;
L->next = NULL;
return true;
}
void CreateList_R(LinkList &L, int n)
{
L = new LNode;
L->data = 0;
L->next = NULL;
LNode* r = L;
for (int i = 0;i < n;i++)
{
int m;
LNode* p = new LNode;
cin >> m;
p->data=m;
p->next = NULL;
r->next = p;
r = p;
L->data++;
}
}
bool GetElem(LinkList L, int i,int &e)
{
LNode*p = L->next;
int j = 1;
while (p&&j < i)
{
p = p->next;
j++;
}
if (!p || j > i)
{
return false;
}
e = p->data;
return true;
}
int LocateElem(LinkList L, int e)
{
LNode* p = L->next;
int j = 1;
while (p&&p->data != e)
{
p = p->next;
j++;
}
if (j > L->data)
{
return false;
}
return j;
}
bool ListInsert(LinkList &L, int i, int e)
{
LNode* p= L;
int j = 0;
while (p&&j < i - 1)
{
p = p->next;
j++;
}
if (!p || j > i - 1)
{
return false;
}
LNode* s = new LNode;
s->data = e;
s->next = p->next;
p->next = s;
L->data++;
return true;
}
void MaxData(LinkList &L)
{
int j = 1;
if(L->next == NULL){
cout<<"空链表无最大值";
}
LNode*pmax = L->next;
LNode*p = L->next->next;
while (p)
{
if (p->data > pmax->data){
pmax = p;
j++;
}
p = p->next;
}
cout<<"最大值为"<<pmax->data<<",其下标为"<<j;
}
bool ListDelete(LinkList &L, int i)
{
LNode* p = L;
int j = 0;
while (p->next && j < i - 1)
{
p = p->next;
j++;
}
if (!(p->next) || (j > i - 1))
{
return false;
}
LNode* q=p->next;
p->next = q->next;
L->data--;
delete q;
return true;
}
void ClearList(LinkList &L)
{
LNode *p, *q;
p = L->next;
while (p)
{
q = p->next;
delete p;
p = q;
}
L->next = NULL;
}
c语言代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct LNode {
int data;
struct LNode *next;
}LNode;
typedef struct LNode LNode;
LNode * initLink();
LNode * insertElem(LNode * p, int data, int add);
int selectElem(LNode * p, int data);
LNode * delElem(LNode * p, int add);
void MaxData(LNode *L);
void ClearList(LNode *L);
void print(LNode *p);
int main() {
int address;
int i,n;
LNode *p;
printf("==========================================\n(1)[初始化]链表中:\n");
p = initLink();
printf("请输入单链表的结点个数n:");
printf("打印当前单链表:");
print(p);
printf("==========================================\n(2)把n[插入]在序号为i的地方:\n");
printf("请输入n和i(用空格隔开):");
scanf("%d %d",&i,&n);
p = insertElem(p, i, n);
printf("打印当前单链表:");
print(p);
printf("==========================================\n(3)[删除]序号为i的数据:\n");
printf("请输入i:");
scanf("%d",&i);
p = delElem(p, i);
printf("打印当前单链表:");
print(p);
printf("==========================================\n(4)[查找]数据值为n的序号:\n");
printf("打印当前单链表:");
print(p);
printf("请输入要查找的数据n:");
scanf("%d",&n);
address = selectElem(p, n);
if (address == -1) {
printf("没有该数据");
}
else {
printf("元素%d的序号为:%d\n",n,address);
}
printf("==========================================\n(5)[求最大结点]:\n");
printf("打印当前单链表:");
print(p);
MaxData(p);
ClearList(p);
printf("==========================================\n(6)[销毁]单链表销毁完成!\n");
return 0;
}
LNode * initLink()
{
int n;
int v;
LNode * p ;
LNode * temp ;
int i;
printf("请输入单链表的结点个数n:");
scanf("%d",&n);
p = (LNode*)malloc(sizeof(LNode));
temp= p;
printf("请依次输入n个数字:");
for (i = 1; i <= n; i++) {
LNode *a ;
a = (LNode*)malloc(sizeof(LNode));
scanf("%d",&v);
a->data = v;
a->next = NULL;
temp->next = a;
temp = temp->next;
}
return p;
}
LNode * insertElem(LNode * p, int data, int add)
{
LNode * temp;
int i;
LNode * c ;
temp = p;
c = (LNode*)malloc(sizeof(LNode));
for (i = 1; i < add; i++) {
temp = temp->next;
if (temp == NULL) {
printf("插入位置无效\n");
return p;
}
}
c->data = data;
c->next = temp->next;
temp->next = c;
return p;
}
LNode * delElem(LNode * p, int add)
{
LNode * temp;
int i;
LNode * del;
temp = p;
for (i = 1; i < add; i++) {
temp = temp->next;
if (temp->next == NULL) {
printf("没有该结点\n");
return p;
}
}
del = temp->next;
temp->next = temp->next->next;
free(del);
return p;
}
int selectElem(LNode * p, int data)
{
LNode * t;
int i = 1;
t = p;
while (t->next) {
t = t->next;
if (t->data == data) {
return i;
}
i++;
}
return -1;
}
void print(LNode *p)
{
LNode* temp;
temp = p;
while (temp->next) {
temp = temp->next;
printf("%d ", temp->data);
}
printf("\n");
}
void MaxData(LNode *L)
{
int j = 1;
LNode*pmax;
LNode*p;
pmax= L->next;
p = L->next->next;
if(L->next == NULL){
printf("空链表无最大值");
}
while (p)
{
if (p->data > pmax->data){
pmax = p;
j++;
}
p = p->next;
}
printf("最大值为%d,其序号为%d\n",pmax->data,j);
}
void ClearList(LNode *L)
{
LNode *p, *q;
p = L->next;
while (p)
{
q = p->next;
free(p);
p = q;
}
L->next = NULL;
}