代码示例
#include "iostream"
using namespace std;
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
typedef int Elemtype;
typedef int Status;
typedef struct Node
{
Elemtype data;
struct Node *next;
} Node;
typedef struct Node *CLinkList;
CLinkList CLinkList_Init()
{
CLinkList head;
head = (CLinkList)
malloc(
sizeof(CLinkList));
head->next = head;
return head;
}
void CreateCLinkList(CLinkList *head)
{
CLinkList p, s;
p = *head;
int flag =
1;
double c;
while (flag)
{
cin >> c;
if (c != -
99999)
{
s = (CLinkList)
malloc(
sizeof(CLinkList));
s->data = c;
s->next = *head;
p->next = s;
p = s;
}
else
{
flag =
0;
}
}
}
int CLinkList_Length(CLinkList *head)
{
CLinkList p;
p = *head;
int count =
0;
while (p->next != *head)
{
count++;
p = p->next;
}
return count;
}
Status CLinkList_Insert(CLinkList *head,
int i, Elemtype e)
{
CLinkList pre, s;
pre = *head;
int k =
1;
while (pre && k < i)
{
pre = pre->next;
k++;
}
if (!pre || k > i)
{
cout <<
"插入位置错误!" << endl;
return ERROR;
}
if (i > CLinkList_Length(head) +
1)
{
cout <<
"插入位置错误!" << endl;
return ERROR;
}
else
{
s = (CLinkList)
malloc(
sizeof(CLinkList));
s->data = e;
s->next = pre->next;
pre->next = s;
}
return OK;
}
Status CLinkList_Delete(CLinkList *head,
int i, Elemtype *e)
{
CLinkList pre, r;
pre = *head;
int k =
1;
while (pre && k < i)
{
pre = pre->next;
k++;
}
if (!pre || k > i)
{
cout <<
"删除位置错误!" << endl;
return ERROR;
}
r = pre->next;
if (i > CLinkList_Length(head))
{
cout <<
"删除位置错误!" << endl;
return ERROR;
}
else
pre->next = pre->next->next;
*e = r->data;
return OK;
}
Status CLinkList_GetData(CLinkList *head,
int i, Elemtype *e)
{
CLinkList p;
p = *head;
int k =
0;
while (p && k < i)
{
p = p->next;
k++;
}
if (!p || k > i)
{
cout <<
"查找位置错误!" << endl;
return ERROR;
}
if (i > CLinkList_Length(head) || i <=
0)
{
cout <<
"查找位置错误!" << endl;
return ERROR;
}
else
{
*e = p->data;
}
return OK;
}
Status PrintList(CLinkList *head)
{
CLinkList p;
p = (*head)->next;
if (p != NULL)
{
while (p != *head)
{
cout << p->data <<
" ";
p = p->next;
}
}
else
{
cout <<
"没有元素!" << endl;
return ERROR;
}
cout << endl;
return OK;
}
void main()
{
CLinkList head;
Elemtype e;
cout <<
"开始初始化..............................................." << endl;
head = CLinkList_Init();
cout <<
"初始化操作完毕!" << endl;
cout <<
"开始建表(这里是尾插法建表,输入-99999结束建表)..........." << endl;
CreateCLinkList(&head);
cout <<
"建表操作完毕!" << endl;
cout <<
"打印线性表中的所有数据:";
PrintList(&head);
cout <<
"打印线性表的长度:";
int count = CLinkList_Length(&head);
cout << count << endl;
cout <<
"-------------------------------------------------" << endl;
cout <<
"开始插入(在第6个位置插入81)............................" << endl;
CLinkList_Insert(&head,
6,
81);
cout <<
"插入操作完毕!" << endl;
cout <<
"打印线性表中的所有数据:";
PrintList(&head);
cout <<
"打印线性表的长度:";
int count2 = CLinkList_Length(&head);
cout << count2 << endl;
cout <<
"-------------------------------------------------" << endl;
cout <<
"开始删除(这里删除第2个元素)............................" << endl;
CLinkList_Delete(&head,
2, &e);
cout <<
"删除操作完毕!" << endl;
cout <<
"删除后打印线性表中的所有数据:";
PrintList(&head);
cout <<
"-------------------------------------------------" << endl;
cout <<
"开始查找(这里查找第5个元素)............................." << endl;
if (CLinkList_GetData(&head,
5, &e))
{
cout <<
"查找操作完毕!" << endl;
cout <<
"打印查找到的数据:";
cout << e << endl;
}
else
{
cout <<
"查找位置错误!" << endl;
}
system(
"pause");
}
程序截图
说明:
程序中不管是插入,删除和查找函数,都做了判断输入的位置是否合法,比如查找第0号元素,会返回查找位置错误,或者是查找的位置大于链表长度,也会返回查找位置错误,删除和插入做了同样的处理,判断输入的位置是否合法。
转载请注明原文地址: https://ju.6miu.com/read-659191.html