2 baybaye baybaye 于 2016.03.20 12:07 提问

大量悬赏 数据结构的问题 自己是新手 实在搞不懂 求大神帮忙

图片说明

图片说明

图片说明

2个回答

dead911
dead911   2016.03.20 13:00
已采纳

是要题目的翻译么?

实现一个链表及以下操作

  • 插入,给定Key值,找到拥有该Key值的节点,并把新节点插入此节点后。若没有在链表中找到该Key值,打印出错误信息。
  • 删除,给定Key值,找到拥有该Key值的节点并删除。若没有在链表中找到该Key值,打印出错误信息。
  • 查询前序节点,给定Key值,找到拥有该Key值的节点的前序节点。若没有在链表中找到该Key值,打印错误信息。
  • 显式整个链表,若链表为空,则显示链表为空。

从文件中读取操作并构建链表。文件中每一行代表了一组操作。你可以认为文件中不包含重复的key值,并且key值总是正数。

  • i x y,表示在key值为x的节点后,插入一个key值为y的节点
  • i x -1,表示在表头插入一个key值为x的节点
  • d x,表示在链表里删除key值为x的节点
  • f x,找到key值为x的前序节点,并打印出前序节点。
  • p,从头打印出链表中所有的节点

lab2_input.txt是输入文件

  • i 3 -1: 在表头插入key为3的节点。此后链表只有一个节点[Head -> 3]
  • i 4 3:在key为3的节点后,插入key为4节点。此后链表有2个节点[Head -> 3 -> 4]
  • i 7 -1:在表头插入key为7的节点。此后链表有3个节点[Head -> 7 -> 3 -> 4]
  • i 5 8:在key为8的节点后,插入key为5的节点。由于链表中没有key为8的节点,此时打印“Insertion(5) Failed: element 8 is not iin the list”
  • d 3:删除key为3的节点。此后链表有2个节点[Head -> 7 -> 4]
  • i 2 7:在key为7的节点后,插入key为2的节点。此后链表[Head -> 7 -> 2 -> 4]
  • d 9:删除key为9的节点。由于链表中没有key为9的节点,此时打印"Deletion failed: element 9 is not in the list"
  • f 3:找到key=3的前序节点,由于链表中没有key为3的节点,此时打印"Could not find 3 in the list"
  • f 7:找到key=7的前序节点,7的前序节点是当前链表的表头,此时打印"Key of previous node of 7 is header"
  • f 2:找到key=2的前序节点,2的前序节点是key为7的节点,此时打印"Key of previous node of 2 is 7"
  • p:打印链表,[Head -> 7 -> 2 -> 4],此时打印"Key:7 Key:2 Key:4"

后面就是写个p2.c的文件,在里面实现这些功能。
数据结构和函数的定义,如下
typedef struct Node *PtrToNode;
typedef PtrToNode List;
...
List MakeEmpty(List L);

题目大概意思就是这个...代码自己写呗~~

dead911
dead911 回复baybaye:随手写了个,不保证完全正确。。。但输出的效果和你给的图是一样的。ubuntu下gcc编译通过。
一年多之前 回复
baybaye
baybaye 回复dead911: 帮忙写个代码可以吗大神 谢谢了
一年多之前 回复
baybaye
baybaye 回复dead911: 不是大哥 谢谢你 但是我要的是代码 我是初学者 实在是不知道怎么写 求帮助了 谢谢好心人
一年多之前 回复
dead911
dead911   2016.03.21 22:15

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;
}
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!