This document describes data structures and algorithms for maintaining a table of key-value pairs using linked lists and hash tables. It discusses:
1) Using a linked list where each node contains a key-value pair and a pointer to the next node. Algorithms described include creating the list, adding nodes, searching the list, and freeing the list.
2) Using a hash table with a fixed-size array where each element points to a linked list. Keys are mapped to array indices using a hash function.
3) Details of implementing a hash function for string keys by summing weighted ASCII values of characters modulo the array size.
The document provides examples and diagrams to illustrate how these data structures and
- Data structures allow for efficient handling of large volumes of data through logical organization and relationships between data elements. Common linear data structures include arrays, lists, stacks, and queues, while trees and graphs are examples of non-linear data structures.
- Abstract data types (ADTs) define a set of complex data objects and operations that can be performed on those objects without specifying their implementation. Data structures provide a way to implement the logical relationships and operations defined in an ADT.
- Linked lists provide an alternative to arrays for storing data by linking each data element to the next using pointers, rather than requiring contiguous memory locations. This allows for more flexible insertion and deletion compared to arrays.
In computer science, a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Instead, each element points to the next
A linked list is a data structure where each node contains a data field and a pointer to the next node. A linked list can be singly linked, where each node only points to the next node, or doubly linked where each node points to both the next and previous nodes. A circular linked list is a variation where the last node points back to the first node, allowing it to be traversed indefinitely. Common operations on linked lists include insertion and deletion of nodes at different positions through manipulation of the node pointers.
A linked list is a collection of nodes that are randomly stored in memory and connected through pointers. Each node contains data and a pointer to the next node. The last node points to null. Linked lists allow for dynamic sizes, easy insertion/deletion, and non-contiguous storage in memory. Common operations include inserting nodes at the beginning, end, or a specified position and deleting nodes from the beginning, end, or a specified position.
The document discusses implementing a sorted linked list data structure in C++. It describes how to insert a new node into the linked list in the correct sorted position by finding the previous and current nodes between which the new node should be inserted. The key steps are to create a new node, find the previous and current nodes using a while loop, and connect the new node by updating the next pointers of the previous and current nodes.
The document contains 6 questions related to C programming concepts like linked lists, pointers, and functions that operate on linked lists. The questions test understanding of creating nodes, linked list traversal, rearranging elements, reversing a linked list, and printing nodes.
This document contains the code for a C program that implements a linked list data structure. It defines functions to insert elements into the list, append elements, sort the list, and print the list. The main function tests the list operations by prompting the user to input elements, printing the original and sorted lists, inserting more elements, and printing the final list.
Revisiting a data structures in detail with linked list stack and queuessuser7319f8
This document discusses various data structures including arrays, stacks, queues, and linked lists. It provides code examples for creating and manipulating each type of data structure. Specifically, it shows code for inserting and deleting elements from arrays and linked lists. It also includes algorithms and code for common stack and queue operations like push, pop, enqueue, and dequeue. The document provides a detailed overview of linear and non-linear data structures.
- Data structures allow for efficient handling of large volumes of data through logical organization and relationships between data elements. Common linear data structures include arrays, lists, stacks, and queues, while trees and graphs are examples of non-linear data structures.
- Abstract data types (ADTs) define a set of complex data objects and operations that can be performed on those objects without specifying their implementation. Data structures provide a way to implement the logical relationships and operations defined in an ADT.
- Linked lists provide an alternative to arrays for storing data by linking each data element to the next using pointers, rather than requiring contiguous memory locations. This allows for more flexible insertion and deletion compared to arrays.
In computer science, a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Instead, each element points to the next
A linked list is a data structure where each node contains a data field and a pointer to the next node. A linked list can be singly linked, where each node only points to the next node, or doubly linked where each node points to both the next and previous nodes. A circular linked list is a variation where the last node points back to the first node, allowing it to be traversed indefinitely. Common operations on linked lists include insertion and deletion of nodes at different positions through manipulation of the node pointers.
A linked list is a collection of nodes that are randomly stored in memory and connected through pointers. Each node contains data and a pointer to the next node. The last node points to null. Linked lists allow for dynamic sizes, easy insertion/deletion, and non-contiguous storage in memory. Common operations include inserting nodes at the beginning, end, or a specified position and deleting nodes from the beginning, end, or a specified position.
The document discusses implementing a sorted linked list data structure in C++. It describes how to insert a new node into the linked list in the correct sorted position by finding the previous and current nodes between which the new node should be inserted. The key steps are to create a new node, find the previous and current nodes using a while loop, and connect the new node by updating the next pointers of the previous and current nodes.
The document contains 6 questions related to C programming concepts like linked lists, pointers, and functions that operate on linked lists. The questions test understanding of creating nodes, linked list traversal, rearranging elements, reversing a linked list, and printing nodes.
This document contains the code for a C program that implements a linked list data structure. It defines functions to insert elements into the list, append elements, sort the list, and print the list. The main function tests the list operations by prompting the user to input elements, printing the original and sorted lists, inserting more elements, and printing the final list.
Revisiting a data structures in detail with linked list stack and queuessuser7319f8
This document discusses various data structures including arrays, stacks, queues, and linked lists. It provides code examples for creating and manipulating each type of data structure. Specifically, it shows code for inserting and deleting elements from arrays and linked lists. It also includes algorithms and code for common stack and queue operations like push, pop, enqueue, and dequeue. The document provides a detailed overview of linear and non-linear data structures.
This document discusses linked lists and their implementation in C/C++. It begins with the objectives of understanding linked lists as a dynamic data structure that can grow and shrink. It then covers memory allocation and the differences between static and dynamic memory allocation. Key concepts discussed include using malloc, calloc and realloc to allocate memory dynamically. The document defines different types of linked lists like singly linked lists and explains basic linked list operations like traversing, inserting and deleting nodes. It concludes by discussing the pros and cons of linked lists.
A linked list is a data structure that stores elements in memory locations that are linked using pointers. Each element contains a data field and a pointer to the next element. Elements are added by allocating memory and linking the new element to an existing element using its pointer. Elements can be inserted at the front or end of the list and traversed using a pointer that walks through each element. Functions are used to initialize, insert, print, find, and delete elements from the linked list.
Linked lists are data structures that store data in scattered locations in memory. Each data item or node contains a data part that holds the actual data and a link part that points to the next node. The nodes are linked together using these links. A linked list requires an external pointer to point to the first node. Operations like traversing, searching, inserting and deleting nodes can be performed on linked lists by manipulating these node links.
The document discusses linked lists and their basic operations. It defines a linked list as a series of connected nodes where each node contains a data element and a pointer to the next node. The basic operations of linked lists include inserting nodes, finding or deleting nodes by value, and traversing the list. It also discusses different types of linked lists like singly linked lists, doubly linked lists, and circular linked lists. The document explains how to implement operations like insertion, deletion, and traversal in detail with examples.
The document discusses linked lists as an alternative to arrays for storing a collection of elements. Linked lists allocate elements individually and connect them using pointers, rather than allocating a single block of memory. Each element contains a data field and a pointer to the next element. Operations like insertion, deletion, finding elements can be implemented by traversing the linked list using these pointers. Variations like dummy head nodes and sorted linked lists are also described.
The document discusses linked lists and their implementation in C. It defines linked lists as dynamic data structures that store data in nodes linked together via pointers. The key operations on linked lists include insertion and deletion of nodes, as well as traversing and searching the list. It describes implementing linked lists as singly linked lists, doubly linked lists, and circular linked lists. Functions are provided for basic linked list operations like insertion, deletion, display on singly linked lists, and implementations of stacks and queues using linked lists.
The document discusses data structures and abstract data types (ADTs). It provides examples of linear lists and linked lists as implementations of the ADT linear list. Key points made include:
- An ADT defines operations on a data structure without specifying its implementation.
- Linked lists provide flexibility for insertion and deletion compared to arrays.
- The main linked list operations are insertion, deletion, finding an element, and traversal.
- Queues and stacks are examples of linear lists with restricted insertion/deletion points.
#include
#include \"linkylist.h\"
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
//node* head = new node;
LinkedList list(1);
ifstream infile1 (argv[1]);
//make sure the file opens properly and read the contents of the file in to a list
if(!infile1)
{
cout<<\"Your Testfile could not be opened!\"<>users;
if (users.find(\"I\")!=string::npos)
{
//int spot = atoi((users[3]).c_str());
string pot = string(users.substr(2));
stringstream spot(pot);
string data;
cin>>data;
insertAfter(list.getNode(spot),data);
}
/*else if (users.find(\"D\")!=string::npos)
{
int spoty = atoi((users[3]).c_str());
deleteNode(spoty);
}*/
else if (users==\"L\")
{
list.traverse_and_print();
}
}
return 0;
#include
using namespace std;
class Node
{
friend class LinkedList;
private:
int value; /* data, can be any data type, but use integer for easiness */
Node *Next; /* pointer to the next node */
public:
/* Constructors with No Arguments */
Node(void)
: Next(NULL)
{ }
/* Constructors with a given value */
Node(int val)
: value(val), Next(NULL)
{ }
/* Constructors with a given value and a link of the next node */
Node(int val, Node* next)
: value(val), Next(next)
{}
/* Getters */
int getValue(void)
{ return value; }
Node* getNext(void)
{ return Next; }
};
/* definition of the linked list class */
class LinkedList
{
private:
/* pointer of head node */
Node *Head;
/* pointer of tail node */
Node *Tail;
public:
/* Constructors with a given value of a list node */
LinkedList(int val);
/* Destructor */
~LinkedList(void);
/* Function to append a node to the end of a linked list */
void tailAppend(string val);
/* Remove a node with a specific value if it exists */
void remove(int val);
void insertAfter(Node* afterMe, string val);
Node* getNode(int pos);
/* Traversing the list and printing the value of each node */
void traverse_and_print();
};
#include
#include \"linkylist.h\"
#include
using namespace std;
LinkedList::LinkedList(int val)
{
/* Create a new node, acting as both the head and tail node */
Head = new Node(val);
Tail = Head;
}
Node* LinkedList::getNode(int pos)
{
Node* p = Head;
/* Counter */
while (pos--) {
if (p != NULL)
p = p->Next;
else
return NULL;
}
return p;
}
void LinkedList::insertAfter(Node* afterMe, string val)
{
if(afterme != NULL)
{
afterMe->Next = new Node(val, afterMe->Next);
}
}
LinkedList::~LinkedList()
{
}
void LinkedList::tailAppend(string val)
{
/* The list is empty? */
if (Head == NULL) {
/* the same to create a new list with a given value */
Tail = Head = new Node(val);
}
else
{
/* Append the new node to the tail */
Tail->Next = new Node(val);
/* Update the tail pointer */
Tail = Tail->Next;
}
}
void LinkedList::remove(int val)
{
Node *pPre = NULL, *pDel = NULL;
/* Check whether it is the head node?
if it is, delete and update the head node */
if (Head->value == val) {
/* point to the node to be deleted */
pDel = Head;
/* update */
Head = pDel->Next;
delete pDel;
return;
}
pPre = Head;
pDel = Head->Next;
/*.
This document discusses linked lists and representing stacks and queues using linked lists. It provides information on different types of linked lists including singly, doubly, and circular linked lists. It describes inserting, deleting, and traversing nodes in a singly linked list. It also provides a C program example to implement a stack using a linked list with functions for push, pop, and display operations. The document discusses how linked lists are dynamic data structures that can grow and shrink in size as needed, unlike arrays.
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
This assignment and the next (#5) involve design and development of a sequential
non contiguous and dynamic datastructure called LinkedList. A linked list object is
a container consisting of connected ListNode objects. As before, we are not going
to use pre-fabricated classes from the c++ library, but construct the LinkedList
ADT from scratch.
The first step is construction and testing of the ListNode class. A ListNode object
contains a data field and a pointer to the next ListNode object (note the recursive
definition).
#This assignment requires you to
1. Read the Assignment 4 Notes
2. Watch the Assignment 4 Support video
3. Implement the following methods of the ListNode class
-custom constructor
-setters for next pointer and data
4. Implement the insert and remove method in the main program
5. Scan the given template to find the above //TODO and implement the code
needed
//TODO in ListNodecpp.h file
public: ListNode(T idata, ListNode<T> * newNext);
public: void setNext(ListNode<T> * newNext);
public: void setData(T newData);
// TODO in main program
void remove(ListNode<int> * &front,int value)
void insert(ListNode<int> * &front,int value)
# The driver is given ListNodeMain.cpp is given to you that does the following
tests
1. Declares a pointer called front to point to a ListNode of datatype integer
2. Constructs four ListNodes with data 1,2,4 and adds them to form a linked
list.
3. Inserts ListNode with data 3 to the list
4. Removes node 1 and adds it back to test removing and adding the first
element
5. Removes node 3 to test removing a middle node
6. Removes node 4 to test removing the last node
7. Attempt to remove a non existent node
8. Remove all existing nodes to empty the list
9. Insert node 4 and then node 1 to test if insertions preserve order
10.Print the list
Main.cpp
#include <iostream>
#include "ListNodecpp.h"
// REMEMBER each ListNode has two parts : a data field
// and an address field. The address is either null or points to the next node
//in the chain
//Requires: integer value for searching, address of front
//Effects: traverses the list node chain starting from front until the end comparing search value
with listnode getData. Returns the original search value if found, if not adds +1 to indicate not
found
//Modifies: Nothing
int search(ListNode<int> * front, int value);
//Requires: integer value for inserting, address of front
//Effects: creates a new ListNode with value and inserts in proper position (increasing order)in
the chain. If chain is empty, adds to the beginning
//Modifies: front, if node is added at the beginning.
//Also changes the next pointer of the previous node to point to the
//newly inserted list node. the next pointer of the newly inserted pointer
//points to what was the next of the previous node.
//This way both previous and current links are adjusted
//******** NOTE the use of & in passing pointer to front as parameter -
// Why do you think this is needed ?**********
void insert(ListNode<int> * &fr.
This document discusses lists as an abstract data type (ADT) and their implementation using arrays and linked lists. It defines a list as a finite, ordered sequence of elements and describes common list operations like insertion, deletion, traversal etc. Linked lists are introduced as an alternative implementation where each node contains a data field and link to the next node. The advantages of linked lists over arrays for dynamic data are also highlighted. Various real-world applications of lists are listed like implementing stacks, queues, graphs and dynamic memory allocation.
A linked list is a data structure where each node contains a data field and a reference to the next node. The head node points to the first node, and nodes are connected through next references. Linked lists allow for efficient insertions and deletions compared to arrays. Common types include singly linked, doubly linked, circular singly linked, and circular doubly linked lists. Operations on linked lists include insertion, deletion, and searching.
IN C LANGUAGE- I've been trying to finish this program for the last fe.docxGordonpACKellyb
IN C LANGUAGE: I've been trying to finish this program for the last few days, and I'm stuck on one part of it, and I can't figure out what to do.
The goal is to read in a file (separated by commas), put the information into a linked list, and then sort that linked list by either first or last name using function pointers.
Everything I have so far works, aside from the fact that my output is never sorted. I'm doing something wrong with the listSort function, and I don't know what it is.
Can anyone please just take a look at what I have and offer a suggestion or solution? It might not even be an issue with the function itself, but something somewhere else.
I would greatly appreciate it.
Please do not make any changes to the variables in the structs. Month has to be a string, it cannot be an integer.
driver.c:
#include "functions.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
int main(int argc, char* argv[]) {
//creating input and output files
FILE* in = NULL;
FILE* out = NULL;
node_t* head = NULL;
node_t* temp = NULL;
//checking for 4 command line arguments
assert(argc == 4);
//opening input file
in = fopen(argv[1], "r");
assert(in != NULL);
//opening output file
out = fopen(argv[2], "w");
assert(out != NULL);
int sortBy = atoi(argv[4]);
temp = createList(in, &head);
if(sortBy == 1) {
sortList(&head, compare_by_firstname);
}
else if(sortBy == 2) {
sortList(&head, compare_by_lastname);
}
printList(out, temp);
deleteList(&head);
//closing files
fclose(in);
fclose(out);
return 0;
}
functions.c:
#include "functions.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
bool isLeapYear(int year) {
if(year % 4 != 0) {
return false;
}
else if(year % 100 != 0) {
return true;
}
else if(year % 400 != 0) {
return false;
}
else {
return true;
}
}
void printBorder(FILE* out)
{
int i = 0;
for(i = 0; i < 80; i++)
fprintf(out,"*");
fprintf(out, "\n");
}
bool checkInvalidDate(node_t *node) {
int year = (int)node->birthday.year;
if(node->birthday.day < 1 || node->birthday.year < 1) {
return true;
}
if(strcmp(node->birthday.month, "February") == 0) {
if(isLeapYear(year)) {
if(node->birthday.day > 29) {
return true;
}
}
else if(node->birthday.day > 28) {
return true;
}
}
else if((strcmp(node->birthday.month, "April") == 0) || (strcmp(node->birthday.month, "June") == 0) ||
(strcmp(node->birthday.month, "September") == 0) || (strcmp(node->birthday.month, "November") == 0)) {
if(node->birthday.day > 30) {
return true;
}
}
else {
if(node->birthday.day > 31) {
return true;
}
}
return false;
}
void add(node_t **node, node_t **head) {
node_t* current = *head;
while(current != NULL) {
//comparing first and last names to check for duplicates; if duplicate the info isn't added to the list
if((strcmp(current->firstName, (*node)->firstName) == 0) && (strcmp(current->lastName, (*node)->lastName) == 0)) {
return;
}
current = current->next;
}
//adding information as a node to the list
(*node.
This document discusses different data structures. It begins by defining a data structure as an organization of data in different ways. It then classifies common data structures as arrays, linked lists, stacks, queues, trees and graphs. It provides definitions and examples of algorithms, arrays, and linked lists. It also includes algorithms for operations on arrays and linked lists like insertion, deletion and traversal. Finally, it discusses implementation of singly and doubly linked lists as stacks and deletion operations in linked lists.
The document discusses sorting algorithms, abstract data types (ADTs), and linked lists. It describes selection sort and insertion sort algorithms for sorting arrays. It then explains that linked lists are a more flexible data structure than arrays and defines a singly linked list as an ADT with nodes that point to the next node in the list. Functions for typical linked list operations like insertion, deletion, checking if empty, and printing the list are discussed.
Linked lists are linear data structures where elements are linked using pointers. The three main types are singly, doubly, and circular linked lists. Linked lists allow dynamic memory allocation and fast insertion/deletion compared to arrays but slower access. A linked list contains nodes, each with a data field and pointer to the next node. Basic operations on linked lists include insertion, deletion, traversal, and search. Doubly linked lists include pointers to both the next and previous nodes.
This document discusses different types of linked lists including singly linked lists, circular linked lists, and doubly linked lists. It provides details on representing stacks and queues using linked lists. Key advantages of linked lists over arrays are that linked lists can dynamically grow in size as needed, elements can be inserted and deleted without shifting other elements, and there is no memory wastage. Operations like insertion, deletion, traversal, and searching are described for singly linked lists along with sample C code to implement a linked list, stack, and queue.
The document outlines the key concepts of linked lists including:
- Linked lists allow for dynamic resizing and efficient insertion/deletion unlike arrays.
- A linked list contains nodes that have a data field and a pointer to the next node.
- Common operations on linked lists include insertion, deletion, searching, and traversing the list.
- The time complexity of these operations depends on whether it's at the head, tail, or interior node.
- Linked lists can be implemented using classes for the node and linked list objects.
This C code defines structures for nodes and linked lists. It provides functions for creating nodes, inserting nodes at the beginning or end of a linked list, deleting nodes with a specific value, searching for a node, and displaying the linked list. These functions are used in the main function to demonstrate manipulating a linked list, including inserting nodes, deleting nodes, and searching.
This document discusses linked lists and their implementation in C/C++. It begins with the objectives of understanding linked lists as a dynamic data structure that can grow and shrink. It then covers memory allocation and the differences between static and dynamic memory allocation. Key concepts discussed include using malloc, calloc and realloc to allocate memory dynamically. The document defines different types of linked lists like singly linked lists and explains basic linked list operations like traversing, inserting and deleting nodes. It concludes by discussing the pros and cons of linked lists.
A linked list is a data structure that stores elements in memory locations that are linked using pointers. Each element contains a data field and a pointer to the next element. Elements are added by allocating memory and linking the new element to an existing element using its pointer. Elements can be inserted at the front or end of the list and traversed using a pointer that walks through each element. Functions are used to initialize, insert, print, find, and delete elements from the linked list.
Linked lists are data structures that store data in scattered locations in memory. Each data item or node contains a data part that holds the actual data and a link part that points to the next node. The nodes are linked together using these links. A linked list requires an external pointer to point to the first node. Operations like traversing, searching, inserting and deleting nodes can be performed on linked lists by manipulating these node links.
The document discusses linked lists and their basic operations. It defines a linked list as a series of connected nodes where each node contains a data element and a pointer to the next node. The basic operations of linked lists include inserting nodes, finding or deleting nodes by value, and traversing the list. It also discusses different types of linked lists like singly linked lists, doubly linked lists, and circular linked lists. The document explains how to implement operations like insertion, deletion, and traversal in detail with examples.
The document discusses linked lists as an alternative to arrays for storing a collection of elements. Linked lists allocate elements individually and connect them using pointers, rather than allocating a single block of memory. Each element contains a data field and a pointer to the next element. Operations like insertion, deletion, finding elements can be implemented by traversing the linked list using these pointers. Variations like dummy head nodes and sorted linked lists are also described.
The document discusses linked lists and their implementation in C. It defines linked lists as dynamic data structures that store data in nodes linked together via pointers. The key operations on linked lists include insertion and deletion of nodes, as well as traversing and searching the list. It describes implementing linked lists as singly linked lists, doubly linked lists, and circular linked lists. Functions are provided for basic linked list operations like insertion, deletion, display on singly linked lists, and implementations of stacks and queues using linked lists.
The document discusses data structures and abstract data types (ADTs). It provides examples of linear lists and linked lists as implementations of the ADT linear list. Key points made include:
- An ADT defines operations on a data structure without specifying its implementation.
- Linked lists provide flexibility for insertion and deletion compared to arrays.
- The main linked list operations are insertion, deletion, finding an element, and traversal.
- Queues and stacks are examples of linear lists with restricted insertion/deletion points.
#include
#include \"linkylist.h\"
#include
#include
#include
#include
using namespace std;
int main(int argc, char *argv[])
{
//node* head = new node;
LinkedList list(1);
ifstream infile1 (argv[1]);
//make sure the file opens properly and read the contents of the file in to a list
if(!infile1)
{
cout<<\"Your Testfile could not be opened!\"<>users;
if (users.find(\"I\")!=string::npos)
{
//int spot = atoi((users[3]).c_str());
string pot = string(users.substr(2));
stringstream spot(pot);
string data;
cin>>data;
insertAfter(list.getNode(spot),data);
}
/*else if (users.find(\"D\")!=string::npos)
{
int spoty = atoi((users[3]).c_str());
deleteNode(spoty);
}*/
else if (users==\"L\")
{
list.traverse_and_print();
}
}
return 0;
#include
using namespace std;
class Node
{
friend class LinkedList;
private:
int value; /* data, can be any data type, but use integer for easiness */
Node *Next; /* pointer to the next node */
public:
/* Constructors with No Arguments */
Node(void)
: Next(NULL)
{ }
/* Constructors with a given value */
Node(int val)
: value(val), Next(NULL)
{ }
/* Constructors with a given value and a link of the next node */
Node(int val, Node* next)
: value(val), Next(next)
{}
/* Getters */
int getValue(void)
{ return value; }
Node* getNext(void)
{ return Next; }
};
/* definition of the linked list class */
class LinkedList
{
private:
/* pointer of head node */
Node *Head;
/* pointer of tail node */
Node *Tail;
public:
/* Constructors with a given value of a list node */
LinkedList(int val);
/* Destructor */
~LinkedList(void);
/* Function to append a node to the end of a linked list */
void tailAppend(string val);
/* Remove a node with a specific value if it exists */
void remove(int val);
void insertAfter(Node* afterMe, string val);
Node* getNode(int pos);
/* Traversing the list and printing the value of each node */
void traverse_and_print();
};
#include
#include \"linkylist.h\"
#include
using namespace std;
LinkedList::LinkedList(int val)
{
/* Create a new node, acting as both the head and tail node */
Head = new Node(val);
Tail = Head;
}
Node* LinkedList::getNode(int pos)
{
Node* p = Head;
/* Counter */
while (pos--) {
if (p != NULL)
p = p->Next;
else
return NULL;
}
return p;
}
void LinkedList::insertAfter(Node* afterMe, string val)
{
if(afterme != NULL)
{
afterMe->Next = new Node(val, afterMe->Next);
}
}
LinkedList::~LinkedList()
{
}
void LinkedList::tailAppend(string val)
{
/* The list is empty? */
if (Head == NULL) {
/* the same to create a new list with a given value */
Tail = Head = new Node(val);
}
else
{
/* Append the new node to the tail */
Tail->Next = new Node(val);
/* Update the tail pointer */
Tail = Tail->Next;
}
}
void LinkedList::remove(int val)
{
Node *pPre = NULL, *pDel = NULL;
/* Check whether it is the head node?
if it is, delete and update the head node */
if (Head->value == val) {
/* point to the node to be deleted */
pDel = Head;
/* update */
Head = pDel->Next;
delete pDel;
return;
}
pPre = Head;
pDel = Head->Next;
/*.
This document discusses linked lists and representing stacks and queues using linked lists. It provides information on different types of linked lists including singly, doubly, and circular linked lists. It describes inserting, deleting, and traversing nodes in a singly linked list. It also provides a C program example to implement a stack using a linked list with functions for push, pop, and display operations. The document discusses how linked lists are dynamic data structures that can grow and shrink in size as needed, unlike arrays.
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
This assignment and the next (#5) involve design and development of a sequential
non contiguous and dynamic datastructure called LinkedList. A linked list object is
a container consisting of connected ListNode objects. As before, we are not going
to use pre-fabricated classes from the c++ library, but construct the LinkedList
ADT from scratch.
The first step is construction and testing of the ListNode class. A ListNode object
contains a data field and a pointer to the next ListNode object (note the recursive
definition).
#This assignment requires you to
1. Read the Assignment 4 Notes
2. Watch the Assignment 4 Support video
3. Implement the following methods of the ListNode class
-custom constructor
-setters for next pointer and data
4. Implement the insert and remove method in the main program
5. Scan the given template to find the above //TODO and implement the code
needed
//TODO in ListNodecpp.h file
public: ListNode(T idata, ListNode<T> * newNext);
public: void setNext(ListNode<T> * newNext);
public: void setData(T newData);
// TODO in main program
void remove(ListNode<int> * &front,int value)
void insert(ListNode<int> * &front,int value)
# The driver is given ListNodeMain.cpp is given to you that does the following
tests
1. Declares a pointer called front to point to a ListNode of datatype integer
2. Constructs four ListNodes with data 1,2,4 and adds them to form a linked
list.
3. Inserts ListNode with data 3 to the list
4. Removes node 1 and adds it back to test removing and adding the first
element
5. Removes node 3 to test removing a middle node
6. Removes node 4 to test removing the last node
7. Attempt to remove a non existent node
8. Remove all existing nodes to empty the list
9. Insert node 4 and then node 1 to test if insertions preserve order
10.Print the list
Main.cpp
#include <iostream>
#include "ListNodecpp.h"
// REMEMBER each ListNode has two parts : a data field
// and an address field. The address is either null or points to the next node
//in the chain
//Requires: integer value for searching, address of front
//Effects: traverses the list node chain starting from front until the end comparing search value
with listnode getData. Returns the original search value if found, if not adds +1 to indicate not
found
//Modifies: Nothing
int search(ListNode<int> * front, int value);
//Requires: integer value for inserting, address of front
//Effects: creates a new ListNode with value and inserts in proper position (increasing order)in
the chain. If chain is empty, adds to the beginning
//Modifies: front, if node is added at the beginning.
//Also changes the next pointer of the previous node to point to the
//newly inserted list node. the next pointer of the newly inserted pointer
//points to what was the next of the previous node.
//This way both previous and current links are adjusted
//******** NOTE the use of & in passing pointer to front as parameter -
// Why do you think this is needed ?**********
void insert(ListNode<int> * &fr.
This document discusses lists as an abstract data type (ADT) and their implementation using arrays and linked lists. It defines a list as a finite, ordered sequence of elements and describes common list operations like insertion, deletion, traversal etc. Linked lists are introduced as an alternative implementation where each node contains a data field and link to the next node. The advantages of linked lists over arrays for dynamic data are also highlighted. Various real-world applications of lists are listed like implementing stacks, queues, graphs and dynamic memory allocation.
A linked list is a data structure where each node contains a data field and a reference to the next node. The head node points to the first node, and nodes are connected through next references. Linked lists allow for efficient insertions and deletions compared to arrays. Common types include singly linked, doubly linked, circular singly linked, and circular doubly linked lists. Operations on linked lists include insertion, deletion, and searching.
IN C LANGUAGE- I've been trying to finish this program for the last fe.docxGordonpACKellyb
IN C LANGUAGE: I've been trying to finish this program for the last few days, and I'm stuck on one part of it, and I can't figure out what to do.
The goal is to read in a file (separated by commas), put the information into a linked list, and then sort that linked list by either first or last name using function pointers.
Everything I have so far works, aside from the fact that my output is never sorted. I'm doing something wrong with the listSort function, and I don't know what it is.
Can anyone please just take a look at what I have and offer a suggestion or solution? It might not even be an issue with the function itself, but something somewhere else.
I would greatly appreciate it.
Please do not make any changes to the variables in the structs. Month has to be a string, it cannot be an integer.
driver.c:
#include "functions.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
int main(int argc, char* argv[]) {
//creating input and output files
FILE* in = NULL;
FILE* out = NULL;
node_t* head = NULL;
node_t* temp = NULL;
//checking for 4 command line arguments
assert(argc == 4);
//opening input file
in = fopen(argv[1], "r");
assert(in != NULL);
//opening output file
out = fopen(argv[2], "w");
assert(out != NULL);
int sortBy = atoi(argv[4]);
temp = createList(in, &head);
if(sortBy == 1) {
sortList(&head, compare_by_firstname);
}
else if(sortBy == 2) {
sortList(&head, compare_by_lastname);
}
printList(out, temp);
deleteList(&head);
//closing files
fclose(in);
fclose(out);
return 0;
}
functions.c:
#include "functions.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>
bool isLeapYear(int year) {
if(year % 4 != 0) {
return false;
}
else if(year % 100 != 0) {
return true;
}
else if(year % 400 != 0) {
return false;
}
else {
return true;
}
}
void printBorder(FILE* out)
{
int i = 0;
for(i = 0; i < 80; i++)
fprintf(out,"*");
fprintf(out, "\n");
}
bool checkInvalidDate(node_t *node) {
int year = (int)node->birthday.year;
if(node->birthday.day < 1 || node->birthday.year < 1) {
return true;
}
if(strcmp(node->birthday.month, "February") == 0) {
if(isLeapYear(year)) {
if(node->birthday.day > 29) {
return true;
}
}
else if(node->birthday.day > 28) {
return true;
}
}
else if((strcmp(node->birthday.month, "April") == 0) || (strcmp(node->birthday.month, "June") == 0) ||
(strcmp(node->birthday.month, "September") == 0) || (strcmp(node->birthday.month, "November") == 0)) {
if(node->birthday.day > 30) {
return true;
}
}
else {
if(node->birthday.day > 31) {
return true;
}
}
return false;
}
void add(node_t **node, node_t **head) {
node_t* current = *head;
while(current != NULL) {
//comparing first and last names to check for duplicates; if duplicate the info isn't added to the list
if((strcmp(current->firstName, (*node)->firstName) == 0) && (strcmp(current->lastName, (*node)->lastName) == 0)) {
return;
}
current = current->next;
}
//adding information as a node to the list
(*node.
This document discusses different data structures. It begins by defining a data structure as an organization of data in different ways. It then classifies common data structures as arrays, linked lists, stacks, queues, trees and graphs. It provides definitions and examples of algorithms, arrays, and linked lists. It also includes algorithms for operations on arrays and linked lists like insertion, deletion and traversal. Finally, it discusses implementation of singly and doubly linked lists as stacks and deletion operations in linked lists.
The document discusses sorting algorithms, abstract data types (ADTs), and linked lists. It describes selection sort and insertion sort algorithms for sorting arrays. It then explains that linked lists are a more flexible data structure than arrays and defines a singly linked list as an ADT with nodes that point to the next node in the list. Functions for typical linked list operations like insertion, deletion, checking if empty, and printing the list are discussed.
Linked lists are linear data structures where elements are linked using pointers. The three main types are singly, doubly, and circular linked lists. Linked lists allow dynamic memory allocation and fast insertion/deletion compared to arrays but slower access. A linked list contains nodes, each with a data field and pointer to the next node. Basic operations on linked lists include insertion, deletion, traversal, and search. Doubly linked lists include pointers to both the next and previous nodes.
This document discusses different types of linked lists including singly linked lists, circular linked lists, and doubly linked lists. It provides details on representing stacks and queues using linked lists. Key advantages of linked lists over arrays are that linked lists can dynamically grow in size as needed, elements can be inserted and deleted without shifting other elements, and there is no memory wastage. Operations like insertion, deletion, traversal, and searching are described for singly linked lists along with sample C code to implement a linked list, stack, and queue.
The document outlines the key concepts of linked lists including:
- Linked lists allow for dynamic resizing and efficient insertion/deletion unlike arrays.
- A linked list contains nodes that have a data field and a pointer to the next node.
- Common operations on linked lists include insertion, deletion, searching, and traversing the list.
- The time complexity of these operations depends on whether it's at the head, tail, or interior node.
- Linked lists can be implemented using classes for the node and linked list objects.
This C code defines structures for nodes and linked lists. It provides functions for creating nodes, inserting nodes at the beginning or end of a linked list, deleting nodes with a specific value, searching for a node, and displaying the linked list. These functions are used in the main function to demonstrate manipulating a linked list, including inserting nodes, deleting nodes, and searching.
বাংলাদেশের অর্থনৈতিক সমীক্ষা ২০২৪ [Bangladesh Economic Review 2024 Bangla.pdf] কম্পিউটার , ট্যাব ও স্মার্ট ফোন ভার্সন সহ সম্পূর্ণ বাংলা ই-বুক বা pdf বই " সুচিপত্র ...বুকমার্ক মেনু 🔖 ও হাইপার লিংক মেনু 📝👆 যুক্ত ..
আমাদের সবার জন্য খুব খুব গুরুত্বপূর্ণ একটি বই ..বিসিএস, ব্যাংক, ইউনিভার্সিটি ভর্তি ও যে কোন প্রতিযোগিতা মূলক পরীক্ষার জন্য এর খুব ইম্পরট্যান্ট একটি বিষয় ...তাছাড়া বাংলাদেশের সাম্প্রতিক যে কোন ডাটা বা তথ্য এই বইতে পাবেন ...
তাই একজন নাগরিক হিসাবে এই তথ্য গুলো আপনার জানা প্রয়োজন ...।
বিসিএস ও ব্যাংক এর লিখিত পরীক্ষা ...+এছাড়া মাধ্যমিক ও উচ্চমাধ্যমিকের স্টুডেন্টদের জন্য অনেক কাজে আসবে ...
Thinking of getting a dog? Be aware that breeds like Pit Bulls, Rottweilers, and German Shepherds can be loyal and dangerous. Proper training and socialization are crucial to preventing aggressive behaviors. Ensure safety by understanding their needs and always supervising interactions. Stay safe, and enjoy your furry friends!
A Strategic Approach: GenAI in EducationPeter Windle
Artificial Intelligence (AI) technologies such as Generative AI, Image Generators and Large Language Models have had a dramatic impact on teaching, learning and assessment over the past 18 months. The most immediate threat AI posed was to Academic Integrity with Higher Education Institutes (HEIs) focusing their efforts on combating the use of GenAI in assessment. Guidelines were developed for staff and students, policies put in place too. Innovative educators have forged paths in the use of Generative AI for teaching, learning and assessments leading to pockets of transformation springing up across HEIs, often with little or no top-down guidance, support or direction.
This Gasta posits a strategic approach to integrating AI into HEIs to prepare staff, students and the curriculum for an evolving world and workplace. We will highlight the advantages of working with these technologies beyond the realm of teaching, learning and assessment by considering prompt engineering skills, industry impact, curriculum changes, and the need for staff upskilling. In contrast, not engaging strategically with Generative AI poses risks, including falling behind peers, missed opportunities and failing to ensure our graduates remain employable. The rapid evolution of AI technologies necessitates a proactive and strategic approach if we are to remain relevant.
Assessment and Planning in Educational technology.pptxKavitha Krishnan
In an education system, it is understood that assessment is only for the students, but on the other hand, the Assessment of teachers is also an important aspect of the education system that ensures teachers are providing high-quality instruction to students. The assessment process can be used to provide feedback and support for professional development, to inform decisions about teacher retention or promotion, or to evaluate teacher effectiveness for accountability purposes.
A review of the growth of the Israel Genealogy Research Association Database Collection for the last 12 months. Our collection is now passed the 3 million mark and still growing. See which archives have contributed the most. See the different types of records we have, and which years have had records added. You can also see what we have for the future.
Strategies for Effective Upskilling is a presentation by Chinwendu Peace in a Your Skill Boost Masterclass organisation by the Excellence Foundation for South Sudan on 08th and 09th June 2024 from 1 PM to 3 PM on each day.
This presentation was provided by Steph Pollock of The American Psychological Association’s Journals Program, and Damita Snow, of The American Society of Civil Engineers (ASCE), for the initial session of NISO's 2024 Training Series "DEIA in the Scholarly Landscape." Session One: 'Setting Expectations: a DEIA Primer,' was held June 6, 2024.
How to Fix the Import Error in the Odoo 17Celine George
An import error occurs when a program fails to import a module or library, disrupting its execution. In languages like Python, this issue arises when the specified module cannot be found or accessed, hindering the program's functionality. Resolving import errors is crucial for maintaining smooth software operation and uninterrupted development processes.
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Dr. Vinod Kumar Kanvaria
Exploiting Artificial Intelligence for Empowering Researchers and Faculty,
International FDP on Fundamentals of Research in Social Sciences
at Integral University, Lucknow, 06.06.2024
By Dr. Vinod Kumar Kanvaria
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
08DsAlg.pdf
1. 1
Data Structures and Algorithms
!
The material for this lecture is drawn, in part, from!
The Practice of Programming (Kernighan & Pike) Chapter 2!
Jennifer Rexford
!
2. 2
Motivating Quotations!
“Every program depends on algorithms and data
structures, but few programs depend on the
invention of brand new ones.”!
-- Kernighan & Pike!
“I will, in fact, claim that the difference between a
bad programmer and a good one is whether he
considers his code or his data structures more
important. Bad programmers worry about the
code. Good programmers worry about data
structures and their relationships.”!
-- Linus Torvalds!
3. 3
Goals of this Lecture!
• Help you learn (or refresh your memory) about:!
• Common data structures and algorithms!
• Why? Shallow motivation:!
• Provide examples of pointer-related C code!
• Why? Deeper motivation:!
• Common data structures and algorithms serve as “high
level building blocks”!
• A power programmer:!
• Rarely creates programs from scratch!
• Often creates programs using building blocks!
4. 4
A Common Task!
• Maintain a table of key/value pairs!
• Each key is a string; each value is an int
• Unknown number of key-value pairs!
• Examples!
• (student name, grade)!
• (“john smith”, 84), (“jane doe”, 93), (“bill clinton”, 81)!
• (baseball player, number)!
• (“Ruth”, 3), (“Gehrig”, 4), (“Mantle”, 7)!
• (variable name, value)!
• (“maxLength”, 2000), (“i”, 7), (“j”, -10)!
• For simplicity, allow duplicate keys (client responsibility)!
• In Assignment #3, must check for duplicate keys!!
5. 5
Data Structures and Algorithms!
• Data structures!
• Linked list of key/value pairs!
• Hash table of key/value pairs!
• Algorithms!
• Create: Create the data structure!
• Add: Add a key/value pair!
• Search: Search for a key/value pair, by key!
• Free: Free the data structure!
6. 6
Data Structure #1: Linked List!
• Data structure: Nodes; each contains key/value
pair and pointer to next node!
• Algorithms:!
• Create: Allocate Table structure to point to first node!
• Add: Insert new node at front of list!
• Search: Linear search through the list!
• Free: Free nodes while traversing; free Table structure!
4
"Gehrig"
3
"Ruth"
NULL
7
"Mantle"
30. 30
Linked List Performance!
• Create: !fast!
• Add:! !fast!
• Search: !slow!
• Free: !slow!
Would it be better to
keep the nodes
sorted by key?!
What are the
asymptotic run times
(big-oh notation)?!
31. 31
Data Structure #2: Hash Table!
• Fixed-size array where each element points to a linked list!
• Function maps each key to an array index !
• For example, for an integer key h
• Hash function: i = h % ARRAYSIZE (mod function)!
• Go to array element i, i.e., the linked list hashtab[i]
• Search for element, add element, remove element, etc.!
0
ARRAYSIZE-1
struct Node *array[ARRAYSIZE];
32. 32
Hash Table Example!
• Integer keys, array of size 5 with hash function “h
mod 5” !
• “1776 % 5” is 1!
• “1861 % 5” is 1!
• “1939 % 5” is 4!
1776
Revolution
1861
Civil
1939
WW2
0
1
2
3
4
33. 33
How Large an Array?!
• Large enough that average “bucket” size is 1!
• Short buckets mean fast search!
• Long buckets mean slow search!
• Small enough to be memory efficient!
• Not an excessive number of elements!
• Fortunately, each array element is just storing a pointer!
• This is OK:!
0
ARRAYSIZE-1
34. 34
What Kind of Hash Function?!
• Good at distributing elements across the array!
• Distribute results over the range 0, 1, …, ARRAYSIZE-1!
• Distribute results evenly to avoid very long buckets!
• This is not so good:!
0
ARRAYSIZE-1
What would be the
worst possible hash
function?!
35. 35
Hashing String Keys to Integers!
• Simple schemes donʼt distribute the keys evenly enough!
• Number of characters, mod ARRAYSIZE!
• Sum the ASCII values of all characters, mod ARRAYSIZE!
• …!
• Hereʼs a reasonably good hash function!
• Weighted sum of characters xi in the string!
• (Σ aixi) mod ARRAYSIZE!
• Best if a and ARRAYSIZE are relatively prime!
• E.g., a = 65599, ARRAYSIZE = 1024!
36. 36
Implementing Hash Function!
• Potentially expensive to compute ai for each value of i!
• Computing ai for each value of I!
• Instead, do (((x[0] * 65599 + x[1]) * 65599 + x[2]) * 65599 + x[3]) * …!
unsigned int hash(const char *x) {
int i;
unsigned int h = 0U;
for (i=0; x[i]!='0'; i++)
h = h * 65599 + (unsigned char)x[i];
return h % 1024;
}
Can be more clever than this for powers of two!!
(Described in Appendix)!
37. 37
Hash Table Example!
Example: ARRAYSIZE = 7!
Lookup (and enter, if not present) these strings: the, cat, in, the, hat!
Hash table initially empty.!
First word: the. hash(“the”) = 965156977. 965156977 % 7 = 1.!
Search the linked list table[1] for the string “the”; not found.!
0
1
2
3
4
5
6
38. 38
Hash Table Example (cont.)!
Example: ARRAYSIZE = 7!
Lookup (and enter, if not present) these strings: the, cat, in, the, hat!
Hash table initially empty.!
First word: “the”. hash(“the”) = 965156977. 965156977 % 7 = 1.!
Search the linked list table[1] for the string “the”; not found!
Now: table[1] = makelink(key, value, table[1])!
0
1
2
3
4
5
6
the
39. 39
Hash Table Example (cont.)!
Second word: “cat”. hash(“cat”) = 3895848756. 3895848756 % 7 = 2.!
Search the linked list table[2] for the string “cat”; not found!
Now: table[2] = makelink(key, value, table[2])!
0
1
2
3
4
5
6
the
40. 40
Hash Table Example (cont.)!
Third word: “in”. hash(“in”) = 6888005. 6888005% 7 = 5.!
Search the linked list table[5] for the string “in”; not found!
Now: table[5] = makelink(key, value, table[5])!
0
1
2
3
4
5
6
the
cat
41. 41
Hash Table Example (cont.)!
Fourth word: “the”. hash(“the”) = 965156977. 965156977 % 7 = 1.!
Search the linked list table[1] for the string “the”; found it!!
0
1
2
3
4
5
6
the
cat
in
42. 42
Hash Table Example (cont.)!
Fourth word: “hat”. hash(“hat”) = 865559739. 865559739 % 7 = 2.!
Search the linked list table[2] for the string “hat”; not found.!
Now, insert “hat” into the linked list table[2]. !
At beginning or end? Doesnʼt matter.!
0
1
2
3
4
5
6
the
cat
in
43. 43
Hash Table Example (cont.)!
Inserting at the front is easier, so add “hat” at the front !
0
1
2
3
4
5
6
the
hat
in
cat
65. 65
Hash Table Performance!
• Create: !fast!
• Add: !fast!
• Search: !fast!
• Free: !slow!
What are the
asymptotic run times
(big-oh notation)?!
Is hash table
search always
fast?!
66. 66
Key Ownership!
• Note: Table_add() functions contain this code:!
• Caller passes key, which is a pointer to memory where a string
resides!
• Table_add() function stores within the table the address where the
string resides!
void Table_add(struct Table *t, const char *key, int value) {
…
struct Node *p = (struct Node*)malloc(sizeof(struct Node));
p->key = key;
…
}
67. 67
Key Ownership (cont.)!
• Problem: Consider this calling code:!
struct Table t;
char k[100] = "Ruth";
…
Table_add(t, k, 3);
strcpy(k, "Gehrig");
…
• Via Table_add(), table contains
memory address k!
• Client changes string at
memory address k!
• Thus client changes key within table!
What happens if the
client searches t for
“Ruth”?!
What happens if the
client searches t for
“Gehrig”?!
68. 68
Key Ownership (cont.)!
• Solution: Table_add() saves copy of given key!
• If client changes string at memory address k, data structure is not
affected!
• Then the data structure “owns” the copy, that is:!
• The data structure is responsible for freeing the memory in which
the copy resides!
• The Table_free() function must free the copy!
void Table_add(struct Table *t, const char *key, int value) {
…
struct Node *p = (struct Node*)malloc(sizeof(struct Node));
p->key = (const char*)malloc(strlen(key) + 1);
strcpy(p->key, key);
…
} Why add 1?!
69. 69
Summary!
• Common data structures & associated algorithms!
• Linked list!
• Fast insert, slow search!
• Hash table!
• Fast insert, (potentially) fast search!
• Invaluable for storing key/value pairs!
• Very common!
• Related issues!
• Hashing algorithms!
• Memory ownership!
71. 71
Revisiting Hash Functions!
• Potentially expensive to compute “mod c”!
• Involves division by c and keeping the remainder!
• Easier when c is a power of 2 (e.g., 16 = 24)!
• An alternative (by example)!
• 53 = 32 + 16 + 4 + 1!
• 53 % 16 is 5, the last four bits of the number!
• Would like an easy way to isolate the last four bits… !
0 0 1 1 0 1 0 1
1
2
4
8
16
32
0 0 0 0 0 1 0 1
1
2
4
8
16
32
72. 72
Recall: Bitwise Operators in C!
• Bitwise AND (&)!
• Mod on the cheap!!
• E.g., h = 53 & 15;!
• Bitwise OR (|)!
&
0
1
0 1
0 0
0 1
|
0
1
0 1
0 1
1 1
0 0 1 1 0 1 0 1
0 0 0 0 1 1 1 1
53
& 15
0 0 0 0 0 1 0 1
5
• Oneʼs complement (~)!
• Turns 0 to 1, and 1 to 0!
• E.g., set last three bits to 0!
• x = x & ~7;!
73. 73
A Faster Hash Function!
unsigned int hash(const char *x) {
int i;
unsigned int h = 0U;
for (i=0; x[i]!='0'; i++)
h = h * 65599 + (unsigned char)x[i];
return h % 1024;
}
unsigned int hash(const char *x) {
int i;
unsigned int h = 0U;
for (i=0; x[i]!='0'; i++)
h = h * 65599 + (unsigned char)x[i];
return h & 1023;
}
Faster!
Previous!
version!
What happens if
you mistakenly
write “h & 1024”?!
74. 74
Speeding Up Key Comparisons!
• Speeding up key comparisons!
• For any non-trivial value comparison function!
• Trick: store full hash result in structure!
int Table_search(struct Table *t,
const char *key, int *value) {
struct Node *p;
int h = hash(key); /* No % in hash function */
for (p = t->array[h%1024]; p != NULL; p = p->next)
if ((p->hash == h) && strcmp(p->key, key) == 0) {
*value = p->value;
return 1;
}
return 0;
}
Why is this so
much faster?!