Delete operation function of separation link method __ Data structure-pta

Source: Internet
Author: User

The deletion operation function of the separation link method is implemented. function Interface Definition:

BOOL Delete (HashTable H, ElementType Key);

Where Hashtable is a detached link hash list, defined as follows:

typedef struct LNODE *ptrtolnode;
struct Lnode {
    ElementType Data;
    Ptrtolnode Next;
typedef ptrtolnode Position;
typedef ptrtolnode List;

typedef struct TBLNODE *hashtable; /* Hash List type
/* struct Tblnode {/   * Hash list node definition
    /int tablesize;/* Maximum length of table/list
    Heads;    /* The array pointing to the node of the linked table
/};

Function Delete should be based on the referee defined hash function hash (key, h->tablesize) from the hash list H to find the location of the Key and delete it, and then lose

Out one line of text: Key is deleted from list heads[i], where key is the incoming keyword that is deleted, I is the list of key

, and finally returns true. Returns False if key does not exist. Sample Referee Test procedure:

#include <stdio.h> #include <string.h> #define KEYLENGTH 15 * Maximum length of the keyword String * * typedef char ELEMENTTYPE[KEYLENGTH+1];                     /* keyword type with string/typedef int INDEX;

/* Hash Address Type */typedef enum {false, true} bool;
typedef struct LNODE *ptrtolnode;
    struct Lnode {ElementType Data;
Ptrtolnode Next;
};
typedef ptrtolnode Position;

typedef ptrtolnode List; typedef struct TBLNODE *hashtable;    /* Hash list type/* struct Tblnode {/* Hash list node definition/int tablesize;/* Maximum length of table/list Heads;

/* The array pointing to the node of the linked table/};

Index Hash (ElementType Key, int tablesize) {return (key[0]-' a ')%tablesize;} HashTable buildtable ();

/* Referee implementation, details do not table * * bool Delete (HashTable H, ElementType Key);
    int main () {HashTable H;

    ElementType Key; 
    H = Buildtable ();
    scanf ("%s", Key);
    if (Delete (H, Key) = = False) printf ("ERROR:%s is not found\n", Key);
    if (Delete (H, Key) = = True) printf ("Are you kidding me?\n");
return 0; }/* Your code willbe embedded in here * *
 
Input Sample 1: The hash table is shown below

Able
Output Sample 1:
Able is deleted from list heads[0]
Input Sample 2: Hash List example 1 figure
Date
Output Sample 2:
Error:date is not found


Test input samples (excluding nodes to be deleted)
Key
Able
Zero
Bye
Very
Day
If you want to delete a piece of information, press Ctrl+z and then enter the message to be deleted.















#include <stdio.h> #include <string.h> #define KEYLENGTH typedef char ELEMENTTYPE[KEYLENGTH+1
];
typedef int INDEX;

typedef enum {false, true} bool;
typedef struct LNODE *ptrtolnode;
    struct Lnode {ElementType Data;
Ptrtolnode Next;
};
typedef ptrtolnode Position;

typedef ptrtolnode List;
typedef struct TBLNODE *hashtable;
    struct tblnode{int tablesize;
List Heads;

};

Index Hash (ElementType Key, int tablesize) {return (key[0]-' a ')%tablesize;}
    HashTable buildtable () {HashTable T = (HashTable) malloc (sizeof (struct tblnode));
    T->heads = (ptrtolnode) malloc (sizeof (struct lnode) * 5); T->tablesize = 5;
    According to the title of the map to know Char key[16];
    int i; for (i = 0; i < t->tablesize; ++i) {t->heads[i].
    Next = NULL;
        while (~SCANF ("%s", key)) {int pos = Hash (key, t->tablesize);
        Ptrtolnode ins = (ptrtolnode) malloc (sizeof (struct lnode));
      strcpy (Ins->data, key);  Ins->next = T->heads[pos].
        Next; T->heads[pos].
    Next = INS;
} return T;
   BOOL Delete (HashTable H, ElementType key) {int hvalue = Hash (key, h->tablesize);
   Ptrtolnode del, pre;
   Pre = H->heads + Hvalue; del = H->heads[hvalue].
   Next;
           while (del!= NULL) {if (strcmp (del->data, Key) = = 0) {Pre->next = del->next;
           Free (DEL);
           del = NULL;
           printf ("%s is deleted from List heads[%d]\n", Key, Hvalue);
       return true;
          else {pre = del;
       del = del->next;
return false;
    int main () {HashTable H;

    ElementType Key;
   H = Buildtable (); Slightly modified to test  while (~scanf ("%s", key)) {if (Delete (H, key) = 0) printf ("ERROR:%s is not found\n", Ke
    y);
    if (Delete (H, Key) = = 1) printf ("Are you kidding me?\n");
return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.