主函式:
#include <iostream>
#include "Linklist.h"
int main()
{
int l;
link L;
Elemtype e;
cout << Init_Linklist(L) << endl;
for (int i = 0; i < 10; i++)
{
cin >> e;
Insert_Linklist(L, 1, e);
}
Traver_Linklist(L);
cout << endl;
int n1;
cout << "洗掉第幾個位置:" << " ";
cin >> n1;
Delete_Linklist(L, n1, e);
Traver_Linklist(L);
cout << endl;
int n2;
cin >> e;
n2 = Locate_Linklist(L, e);
Traver_Linklist(L);
cout << endl;
int n3;
Get_Linklist(L, n3, e);
Traver_Linklist(L);
cout << endl;
int n4, a;
cin >> e;
a = Seek_Linklist(L, n4, e);
Traver_Linklist(L);
cout << endl;
int n5;
cin >> e;
Change_Linklist(L, n5, e);
Traver_Linklist(L);
cout << endl;
return 0;
}
頭檔案:
#include <iostream>
#include <stdio.h>
using namespace std;
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define EOF -1
#define MAXSiZE 100
typedef int Elemtype;
typedef int Status;
//結點
typedef struct Lnode
{
Elemtype date;
struct Lnode* next;
}Lnode, * Link;
//控制結構
typedef struct link
{
Link head, tail;
int len;
int isnotInit;
}link;
//初始化
Status Init_Linklist(link& L)
{
L.head = L.tail = new Lnode[1];
if (!L.head) { return ERROR; }
//頭結點資料域不存資料
L.head->next = NULL;
L.len = 0;
L.isnotInit = 1;
return OK;
}
//插入
Status Insert_Linklist(link& L, int i, Elemtype e)
{
int j;
Link p, q;
q = p = L.head;
if (L.isnotInit != 1) { return ERROR; }
if (i<1 || i>L.len + 1) { return ERROR; }
if (i != L.len + 1)
{
for (j = 1; j < i; j++) { p = p->next; }//for(j=0;j<i-1;j++),第i-1個結點
q = new Lnode[1];
q->date = e;
q->next = p->next;
p->next = q;
}
if (i == L.len + 1) { L.tail = q; }//插入在鏈尾
L.len++;
return OK;
}
//洗掉
Status Delete_Linklist(link& L, int i, Elemtype& e)
{
int j;
Link p, q;
p = L.head;
if (L.isnotInit != 1) { return ERROR; }
if (i < 1 || i < L.len) { return ERROR; }
if (i != L.len)
{
for (j = 1; j < i; j++) { p = p->next; }
q = p->next;
e = q->date;//保存結點資料
p->next = q->next;
delete[] q;
}
if (i == L.len) { L.tail = p; }
L.len--;
return OK;
}
//按值查找
#if(1)
int Locate_Linklist(link L, Elemtype e)
{
int i = 1;
if (L.isnotInit != 1) { return ERROR; }
Lnode* p = L.head->next;//第一個結點
while (p != NULL && p->date != e)
{
p = p->next;
i++;
}
if (p->date == e){return i;}
else
{
cout << "The key is not fine!" << endl;
return NULL;
}
}
#endif
#if(0)
Lnode* Locate_Linklist(link L, Elemtype key)
{
Lnode* p = L.head->next;
while (p != NULL && p->date == key) { p = p->next; }
if (p->date == key) { return p; }
else
{
cout << "The key is not fine" << endl;
return NULL;
}
}
#endif
Status Get_Linklist(link& L, int a, Elemtype& key)
{
if (L.isnotInit != 1) { return ERROR; }
Lnode* p = L.head->next;
while (p != NULL && p->date == key) { p = p->next; }
if (p->date == key) { key = a; }
else
{
cout << "The key is not fine" << endl;
return NULL;
}
}
//位置查找
Status Seek_Linklist(link L, int i, Elemtype e)
{
if (L.isnotInit != 1) { return ERROR; }
Lnode* p = L.head->next;
for (int j = 0; j < i - 1; j++) {p = p->next;}
e = p->date;
return e;
}
//位置改值
Status Change_Linklist(link& L, int i, Elemtype& e)
{
int j;
if (L.isnotInit != 1) { return ERROR; }
Lnode* p = L.head;
if (i<1 || i>L.len) { return ERROR; }
for (j = 0; j < i - 1; j++) { p = p->next; }
e = p->date;
return OK;
}
void Traver_Linklist(link L)
{
Lnode* p;
p = L.head;
while (p)
{
cout << p->date << "\t";
p = p->next;
}
}
求大佬幫我改一下
uj5u.com熱心網友回復:
改什么呢?要改成什么樣子?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/271665.html
標籤:C語言
