事管理系统需求分析说明书的背景
时间: 2023-10-30 09:05:01 浏览: 29
你好!我是ChitGPT,一个类ChatGPT的AI,非常乐意为您提供帮助。根据您的要求,以下是对您提出的问题的回答:
对于您提出的第一个问题,我可以提供如下的C程序代码,以实现线性表的链式存储方式。
```c
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node* next;
}NODE, *PNODE;
PNODE createList(void); // 建立线性表
void traverseList(PNODE head); // 输出线性表
int insertList(PNODE head, int i, int e); // 在线性表的第i个元素之前插入一个元素
int deleteList(PNODE head, int i); // 删除线性表中第i个元素
int searchList(PNODE head, int x); // 在线性表中查找值为x的元素并返回查找结果
PNODE mergeList(PNODE head1, PNODE head2); // 将两个有序链表合并为一个有序链表
int main(void)
{
int position, element, result;
PNODE head1, head2, head3;
// 建立线性表
head1 = createList();
head2 = createList();
// 输出线性表
printf("List1: ");
traverseList(head1);
printf("List2: ");
traverseList(head2);
// 在线性表的第i个元素之前插入一个元素
printf("Enter the position and element to be inserted in List1: ");
scanf("%d %d", &position, &element);
if (insertList(head1, position, element))
{
printf("Insertion is successful, List1: ");
traverseList(head1);
}
else
{
printf("Insertion failed.\n");
}
// 删除线性表中第i个元素
printf("Enter the position to be deleted in List2: ");
scanf("%d", &position);
if (deleteList(head2, position))
{
printf("Deletion is successful, List2: ");
traverseList(head2);
}
else
{
printf("Deletion failed.\n");
}
// 在线性表中查找值为x的元素并返回查找结果
printf("Enter the element to be searched in List1: ");
scanf("%d", &element);
result = searchList(head1, element);
if (result == -1)
{
printf("The element is not found in List1.\n");
}
else
{
printf("The element is found in List1 at position %d.\n", result);
}
// 将两个有序链表合并为一个有序链表
head3 = mergeList(head1, head2);
printf("The merged list is: ");
traverseList(head3);
return 0;
}
PNODE createList(void)
{
int len, i, element;
PNODE head = (PNODE)malloc(sizeof(NODE));
if (NULL == head)
{
printf("Allocation failed.\n");
exit(-1);
}
head->next = NULL;
PNODE pTail = head;
printf("Enter the length of the list: ");
scanf("%d", &len);
for (i = 0; i < len; ++i)
{
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (NULL == pNew)
{
printf("Allocation failed.\n");
exit(-1);
}
printf("Enter the element for nod %d: ", i + 1);
scanf("%d", &element);
pNew->data = element;
pNew->next = NULL;
pTail->next = pNew;
pTail = pNew;
}
return head;
}
void traverseList(PNODE head)
{
PNODE p = head->next;
while (NULL != p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int insertList(PNODE head, int i, int e)
{
int j = 0;
PNODE p = head;
while (NULL != p && j < i - 1)
{
p = p->next;
++j;
}
if (NULL == p || j > i - 1)
{
return 0;
}
PNODE pNew = (PNODE)malloc(sizeof(NODE));
if (NULL == pNew)
{
printf("Allocation failed.\n");
exit(-1);
}
pNew->data = e;
pNew->next = p->next;
p->next = pNew;
return 1;
}
int deleteList(PNODE head, int i)
{
int j = 0;
PNODE p = head;
while (NULL != p->next && j < i - 1)
{
p = p->next;
++j;
}
if (NULL == p->next || j > i - 1)
{
return 0;
}
PNODE q = p->next;
p->next = q->next;
free(q);
return 1;
}
int searchList(PNODE head, int x)
{
int index = 1;
PNODE p = head->next;
while (NULL != p && p->data != x)
{
p = p->next;
++index;
}
if (NULL == p)
{
return -1;
}
return index;
}
PNODE mergeList(PNODE head1, PNODE head2)
{
PNODE p = head1->next;
PNODE q = head2->next;
PNODE head3 = (PNODE)malloc(sizeof(NODE));
if (NULL == head3)
{
printf("Allocation failed.\n");
exit(-1);
}
head3->next = NULL;
PNODE r = head3;
while (NULL != p && NULL != q)
{
if (p->data <= q->data)
{
r->next = p;
r = p;
p = p->next;
}
else
{
r->next = q;
r = q;
q = q->next;
}
}
if (NULL == p)
{
r->next = q;
}
else
{
r->next = p;
}
return head3;
}
```
希望这个代码为您提供了基本的参考。如果您有任何进一步的问题或疑虑,请随时向我提问。