请编写创建链表和输出链表的函数。对于以下数据结点的结构定义,
针对带头结点的链表,请编程完成以下功能。
struct LNode{
int data; //数据域
struct LNode *next; //指针域
};
struct LNode *head; //头指针
输入数据包含若干组命令和数据,一组数据中的第1个字符代表命令,
接下来的是该命令需要的数据。
(1)如果命令是I,功能为创建空链表,对应函数:void List_Init(head);
(2)如果命令是A,后跟一个整数data,功能为向链表尾部追加一个数据data,
对应函数:void List_Append(head,data);
(3)如果命令是C,后跟一个整数N,再跟N个整数,功能为向链表尾部追加N个
数据,可通过调用List_Append()函数实现;
(4)如果命令是P,功能遍历输出链表中所有数据,数据间用一个空格分隔,对应
函数:void List_print(head),如果链表未建立,输出“List not defined!”,
如果链表为空输出:“List is empty!”。
(5)如果命令是N,后跟一个整数n和d,功能为向链表的第n个位置插入数据d,
可通过调用List_Insert(head,n,d)函数实现;
(6)如果命令是F,后跟一个整数d,功能为在链表查找数据d,返回其位序,若
找不到返回-1。可通过调用List_Find(head,d)函数实现;
(7)如果命令是D,后跟一个整数n,功能为删除链表第n个位置的数据,可通过
调用List_Delete(head,n)函数实现。
输入格式:
若干组命令和数据,很多命令和数据写在一起请注意识别。
输出格式:
根据输入命令输出相应内容,详见输出样例。
输入样例:
I C 5 100 200 300 400 500 P
F 500 N 3 23 N 5 31 P
D 4 P F 99
A 6 P
输出样例:
100 200 300 400 500
index:5
100 200 23 300 31 400 500
100 200 23 31 400 500
Not Found!
100 200 23 31 400 500 6
输入样例:
I A 100 A 200 A 300 C 4 400 500 600 700 P
F 800 F 500
N 4 4 N 5 5 P
D 2 D 2 D 2 P
D 2 D 2 D 2 P
D 1 D 1 P
D 1 D 1 P
输出样例:
100 200 300 400 500 600 700
Not Found!
index:5
100 200 300 4 5 400 500 600 700
100 5 400 500 600 700
100 600 700
700
List is empty!
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
struct LNode
{
int data; //数据域
struct LNode *next; //指针域
};
struct LNode *head = NULL;
int len = sizeof(struct LNode);
struct LNode *List_Init(struct LNode *head) // 创建空链表
{
head = (struct LNode *) malloc(len);
head -> next = NULL;
head -> data = INT_MAX;
return head;
}
void List_Append(struct LNode *head, int data) // 向链表的尾部追加一个数据data
{
struct LNode *p1, *p2;
p1 = p2 = (struct LNode *) malloc(len);
p1 = head;
p2 -> data = data;
p2 -> next = NULL;
while (p1 -> next != NULL)
{
p1 = p1 -> next;
}
p1 -> next = p2;
}
void List_print(struct LNode *head) // 遍历输出链表中所有数据
{
struct LNode *p;
if(head == NULL)
{
printf("List not defined!\n");
return;
}
if(head -> next == NULL)
{
printf("List is empty!\n");
return;
}
p = head -> next;
while (p != NULL)
{
printf("%d", p -> data);
p = p -> next;
if (p != NULL)
printf(" ");
}
printf("\n");
}
void List_Insert(struct LNode *head, int n, int d) // 向链表的第n个位置插入数据d
{
struct LNode *p1, *p2;
p1 = p2 = (struct LNode *) malloc(len);
p1 = head;
p2 -> data = d;
p2 -> next = NULL;
for (int i = 1; i < n; i++)
{
p1 = p1 -> next;
}
p2 -> next = p1 -> next;
p1 -> next = p2;
}
int List_Find(struct LNode *head, int d) // 在链表查找数据d,返回其位序
{
int count = 1;
struct LNode *p;
p = head -> next;
while(p != NULL)
{
if(p -> data == d)
return count;
p = p -> next;
count++;
}
return -1;
}
void List_Delete(struct LNode *head, int n) // 删除链表第n个位置的数据
{
struct LNode *p1, *p2;
p1 = p2 = (struct LNode *) malloc(len);
p1 = head;
p2 = p1 -> next;
for (int i = 1; i < n; i++)
{
if(p1 != NULL)
p1 = p1 -> next;
}
if (p1 == NULL || p1 -> next == NULL)
return;
p2 = p1 -> next;
p1 -> next = p1 -> next -> next;
free(p2);
}
int main()
{
char ch;
int n, data, N, i;
while (scanf("%c", &ch) != EOF)
{
if (ch == 'I')
head = List_Init(head);
if (ch == 'A')
{
scanf("%d", &data);
List_Append(head, data);
}
if (ch == 'C')
{
scanf("%d",&N);
for(i = 0; i < N; i++)
{
scanf("%d", &data);
List_Append(head, data);
}
}
if (ch == 'P')
List_print(head);
if (ch == 'N')
{
scanf("%d %d", &n, &data);
List_Insert(head, n, data);
}
if (ch == 'F')
{
scanf("%d", &data);
if (List_Find(head, data) == -1)
printf("Not Found!\n");
else
printf("index:%d\n", List_Find(head, data));
}
if (ch == 'D')
{
scanf("%d%d",&n);
List_Delete(head,n);
}
}
return 0;
}