是要题目的翻译么?
实现一个链表及以下操作
从文件中读取操作并构建链表。文件中每一行代表了一组操作。你可以认为文件中不包含重复的key值,并且key值总是正数。
lab2_input.txt是输入文件
后面就是写个p2.c的文件,在里面实现这些功能。
数据结构和函数的定义,如下
typedef struct Node *PtrToNode;
typedef PtrToNode List;
...
List MakeEmpty(List L);
题目大概意思就是这个...代码自己写呗~~
exxucao@exxucao-VirtualBox:~/cProject/testList$ vim testList.c
exxucao@exxucao-VirtualBox:~/cProject/testList$ gcc -g -o p2 testList.c
exxucao@exxucao-VirtualBox:~/cProject/testList$ ./p2 lab2_input.txt
Insertion(5) Failed : element 8 is not in the list
Deletion failed : element 9 is not in the list
Could not find 3 in the list
Key of the previous node of 7 is header.
Key of the previous node of 2 is 7.
Key:7 Key:2 Key:4
exxucao@exxucao-VirtualBox:~/cProject/testList$ vim testList.c
exxucao@exxucao-VirtualBox:~/cProject/testList$ gcc -g -o p2 testList.c
exxucao@exxucao-VirtualBox:~/cProject/testList$ ./p2 lab2_input.txt
Insertion(5) Failed : element 8 is not in the list
Deletion failed : element 9 is not in the list
Could not find 3 in the list
Key of the previous node of 7 is header.
Key of the previous node of 2 is 7.
Key:7 Key:2 Key:4
exxucao@exxucao-VirtualBox:~/cProject/testList$ vim testList.c
exxucao@exxucao-VirtualBox:~/cProject/testList$ gcc -g -o p2 testList.c
exxucao@exxucao-VirtualBox:~/cProject/testList$ ./p2 lab2_input.txt
Insertion(5) Failed : element 8 is not in the list
Deletion failed : element 9 is not in the list
Could not find 3 in the list
Key of the previous node of 7 is header.
Key of the previous node of 2 is 7.
Key:7 Key:2 Key:4
exxucao@exxucao-VirtualBox:~/cProject/testList$ vim testList.c
exxucao@exxucao-VirtualBox:~/cProject/testList$ gcc -g -o p2 testList.c
exxucao@exxucao-VirtualBox:~/cProject/testList$ ./p2 lab2_input.txt
Insertion(5) Failed : element 8 is not in the list
Deletion failed : element 9 is not in the list
Could not find 3 in the list
Key of the previous node of 7 is header.
Key of the previous node of 2 is 7.
Key:7 Key:2 Key:4
exxucao@exxucao-VirtualBox:~/cProject/testList$ cat testList.c
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define HEADER -1
/* type definition */
typedef int ElementType;
typedef struct Node
{
ElementType element;
struct Node* next;
} Node;
typedef struct Node* PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;
/* function declare */
List Create();
List MakeEmpty(List L);
int IsEmpty(List L);
int IsLast(Position P, List L);
void Delete(ElementType X, List L);
Position FindPrev(ElementType X, List L);
void Insert(List L, ElementType X, ElementType Y);
void DeleteList(List L);
void PrintList(List L);
List Create()
{
Node* head = (Node*) malloc(sizeof(Node));
head->element = HEADER;
head->next = NULL;
return head;
}
List MakeEmpty(List L)
{
/* free node except head */
PtrToNode head = L;
PtrToNode tmp_p = head->next;
while(NULL != tmp_p) {
head->next = tmp_p->next;
free(tmp_p);
tmp_p = head->next;
}
return head;
}
int IsEmpty(List L)
{
if (L->next == NULL) {
return TRUE;
}
return FALSE;
}
int IsLast(Position P, List L)
{
PtrToNode last_p = L;
while(NULL != last_p->next) {
last_p = last_p->next;
}
if(last_p == P) {
return TRUE;
}
else {
return FALSE;
}
}
void Delete(ElementType X, List L)
{
PtrToNode curr_p = L;
PtrToNode next_p = curr_p->next;
while(NULL != next_p) {
/* find the node with X and delete */
if(next_p->element == X) {
curr_p->next = next_p->next;
free(next_p);
next_p = curr_p->next;
return;
}
curr_p=next_p;
next_p=next_p->next;
}
printf("Deletion failed : element %d is not in the list\n", X);
}
Position FindPrev(ElementType X, List L)
{
PtrToNode curr_p = L;
while(NULL !=curr_p->next) {
if(curr_p->next->element == X) {
printf("Key of the previous node of %d is ", X);
if(curr_p->element == HEADER) {
printf("header.\n");
}
else {
printf("%d.\n",curr_p->element);
}
return curr_p;
}
curr_p = curr_p->next;
}
printf("Could not find %d in the list\n", X);
}
void Insert(List L, ElementType X, ElementType Y)
{
PtrToNode curr_p = L;
while(NULL != curr_p) {
if(curr_p->element == Y) {
PtrToNode new_p = (PtrToNode) malloc (sizeof (Node));
new_p->element = X;
new_p->next = curr_p->next;
curr_p->next = new_p;
return;
}
curr_p = curr_p->next;
}
printf("Insertion(%d) Failed : element %d is not in the list\n",X,Y);
return;
}
void DeleteList(List L)
{
PtrToNode head_p = MakeEmpty(L);
free(head_p);
}
void PrintList(List L)
{
PtrToNode curr_p = L;
while(NULL != curr_p->next) {
curr_p = curr_p->next;
printf("Key:%d\t",curr_p->element);
}
printf("\n");
}
int main(int argc, char** argv)
{
if(argc != 2) {
printf("usage:\n ./p2 lab2_input.txt\n");
exit(1);
}
/* create empty list*/
List list = Create();
/* read file */
char* filename = argv[1];
FILE* fp;
if((fp=fopen(filename,"r"))==NULL){
printf("error when opening %s\n",filename);
}
char op; /* 'i','d','p' and etc*/
int x,y;
while(!feof(fp)) {
fscanf(fp, "%c", &op);
switch(op) {
case 'p':
PrintList(list);
break;
case 'i':
fscanf(fp,"%d%d",&x,&y);
Insert(list,x,y);
break;
case 'd':
fscanf(fp,"%d",&x);
Delete(x,list);
break;
case 'f':
fscanf(fp,"%d",&x);
FindPrev(x,list);
break;
case '\n':
case '\t':
break;
default:
printf("error in file\n");
exit(1);
}
}
fclose(fp);
return 0;
}