SlideShare a Scribd company logo
1 of 5
Download to read offline
I'm creating a skip list program for my Algorithms and Advanced Data Structure class. I was
tasked with implementing a find & add method for the skiplist. Here is my program, I'm running
into a segmentation fault when I add my second value into the skiplist. My first issue that I fixed
was that I wasn't checking the first node in the list to see if it was nullptr, but that was not my
only issue apparently.
Add Method:
void SkipList::add(const ElemType &el) {
// Create a new node with the given element
Node *newNode = new Node(el);
// Get the height of the new node
int newHeight = newNode->nodeHeight;
// If the height of the new node is greater than the current list height,
// update the list height.
if (newHeight > listHeight) {
listHeight = newHeight;
}
// Array to keep track of nodes to update
Node *update[MAX_NUM_LISTS];
// Start from the top level
Node *current = head[listHeight - 1]; // Initialize current correctly
// If the list is empty, assign the first node to head
if (head[0] == nullptr) {
head[0] = newNode;
} else {
// Loop to traverse through list
for (int i = listHeight - 1; i >= 0; i--) {
// Move forward in the current level
while (current->next[i] != nullptr && current->next[i]->elem < el) {
current = current->next[i];
}
// Store the node before which the new node should be inserted
update[i] = current;
}
// Insert the new node at each level
for (int i = 0; i < newHeight; i++) {
newNode->next[i] = update[i]->next[i];
update[i]->next[i] = newNode;
}
}
}
Find Method:
bool SkipList::find(const ElemType &el) const {
// Start from the top level
Node *current = head[listHeight - 1];
bool found = false;
// Iterate through the levels of the skip list
for (int i = listHeight - 1; i >= 0; i--) {
// Move forward in the current level until reaching the end or finding a larger element
while (current->next[i] != nullptr && current->next[i]->elem < el) {
current = current->next[i];
}
// Check if the current node's next node contains the target element
if (current->next[i] != nullptr && current->next[i]->elem == el) {
found = true; // Element found
}
}
return found; // Element not found in the skip list
}
Main:
int main()
{
ElemType inputs[] = {5, 7, 8, 10, 12, 17, 19, 22, 28, 31, 33, 35, 42, 51, 59};
SkipList list1;
cout << "For list1 (same as in the slide 10 of Skip List1 Lecture Note):n";
// for (int i=0; i(std::size(inputs)); i++)
{
cout << "add " << inputs[i] << endl;
list1.add(inputs[i]);
cout << "list1:n"
<< list1 << endl;
}
if (list1.find(33))
cout << "33 is found!n";
else
cout << "33 is not found!!!!n";
if (list1.find(70))
cout << "70 is found!n";
else
cout << "70 is not found!!!!n";
if (list1.find(10))
cout << "10 is found!n";
else
cout << "10 is not found!!!!n";
cout << "n===================================n";
cout << "For list2:n";
ElemType inputs2[] = {19, 8, 17, 59, 12, 7, 5, 22, 28, 35, 33, 31, 42, 51, 10};
SkipList list2;
for (int i = 0; i < static_cast(std::size(inputs2)); i++)
{
cout << "add " << inputs2[i] << endl;
list2.add(inputs2[i]);
cout << "list2:n"
<< list2 << endl;
}
if (list2.find(33))
cout << "33 is found!n";
else
cout << "33 is not found!!!!n";
if (list2.find(70))
cout << "70 is found!n";
else
cout << "70 is not found!!!!n";
if (list2.find(10))
cout << "10 is found!n";
else
cout << "10 is not found!!!!n";
return 0;
}

More Related Content

Similar to Im creating a skip list program for my Algorithms and Advanced Data.pdf

double linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdfdouble linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdffacevenky
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queuessuser7319f8
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfEricvtJFraserr
 
In the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfIn the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfbirajdar2
 
Write a function to merge two doubly linked lists. The input lists ha.pdf
Write a function to merge two doubly linked lists. The input lists ha.pdfWrite a function to merge two doubly linked lists. The input lists ha.pdf
Write a function to merge two doubly linked lists. The input lists ha.pdfinfo706022
 
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdfDividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdftesmondday29076
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEswathirajstar
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfcallawaycorb73779
 
lab stack and queue.docx
lab stack and queue.docxlab stack and queue.docx
lab stack and queue.docxR S Anu Prabha
 
STAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfSTAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfbabitasingh698417
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfannaelctronics
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfrozakashif85
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfinfo430661
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfconnellalykshamesb60
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfezycolours78
 
Node file code below#ifndef NODE_H#define NODE_H#include .pdf
Node file code below#ifndef NODE_H#define NODE_H#include .pdfNode file code below#ifndef NODE_H#define NODE_H#include .pdf
Node file code below#ifndef NODE_H#define NODE_H#include .pdfcontact41
 

Similar to Im creating a skip list program for my Algorithms and Advanced Data.pdf (20)

double linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdfdouble linked list header file code#include iostream#include.pdf
double linked list header file code#include iostream#include.pdf
 
Linked list
Linked listLinked list
Linked list
 
Stack
StackStack
Stack
 
Revisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queueRevisiting a data structures in detail with linked list stack and queue
Revisiting a data structures in detail with linked list stack and queue
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
In the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdfIn the class we extensively discussed a generic singly linked list i.pdf
In the class we extensively discussed a generic singly linked list i.pdf
 
Write a function to merge two doubly linked lists. The input lists ha.pdf
Write a function to merge two doubly linked lists. The input lists ha.pdfWrite a function to merge two doubly linked lists. The input lists ha.pdf
Write a function to merge two doubly linked lists. The input lists ha.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdfDividing a linked list into two sublists of almost equal sizesa. A.pdf
Dividing a linked list into two sublists of almost equal sizesa. A.pdf
 
DSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUEDSA- Unit III- STACK AND QUEUE
DSA- Unit III- STACK AND QUEUE
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
 
lab stack and queue.docx
lab stack and queue.docxlab stack and queue.docx
lab stack and queue.docx
 
STAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdfSTAGE 2 The Methods 65 points Implement all the methods t.pdf
STAGE 2 The Methods 65 points Implement all the methods t.pdf
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
 
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdfWrite a Java Class to Implement a Generic Linked ListYour list mus.pdf
Write a Java Class to Implement a Generic Linked ListYour list mus.pdf
 
Given below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdfGiven below is the completed implementation of MyLinkedList class. O.pdf
Given below is the completed implementation of MyLinkedList class. O.pdf
 
Using the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdfUsing the provided table interface table.h and the sample linked lis.pdf
Using the provided table interface table.h and the sample linked lis.pdf
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
To complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdfTo complete the task, you need to fill in the missing code. I’ve inc.pdf
To complete the task, you need to fill in the missing code. I’ve inc.pdf
 
Node file code below#ifndef NODE_H#define NODE_H#include .pdf
Node file code below#ifndef NODE_H#define NODE_H#include .pdfNode file code below#ifndef NODE_H#define NODE_H#include .pdf
Node file code below#ifndef NODE_H#define NODE_H#include .pdf
 

More from arkmuzikllc

Here is selection filepublic class Selection { public static in.pdf
Here is selection filepublic class Selection {  public static in.pdfHere is selection filepublic class Selection {  public static in.pdf
Here is selection filepublic class Selection { public static in.pdfarkmuzikllc
 
Help me with these multiple-choice questions. Please dont use chat .pdf
Help me with these multiple-choice questions. Please dont use chat .pdfHelp me with these multiple-choice questions. Please dont use chat .pdf
Help me with these multiple-choice questions. Please dont use chat .pdfarkmuzikllc
 
Hello I am stuck in my code I know is everything is goid but I think.pdf
Hello I am stuck in my code I know is everything is goid but I think.pdfHello I am stuck in my code I know is everything is goid but I think.pdf
Hello I am stuck in my code I know is everything is goid but I think.pdfarkmuzikllc
 
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfHeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfarkmuzikllc
 
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdf
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdfHINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdf
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdfarkmuzikllc
 
Hi, I need some assistance with this UML use case diagram. I manage .pdf
Hi, I need some assistance with this UML use case diagram. I manage .pdfHi, I need some assistance with this UML use case diagram. I manage .pdf
Hi, I need some assistance with this UML use case diagram. I manage .pdfarkmuzikllc
 
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdf
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdfGive an NFA that recognizes the language (10 + 110 + 101) and then .pdf
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdfarkmuzikllc
 
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType Java file pr.pdf
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType  Java file pr.pdfIN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType  Java file pr.pdf
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType Java file pr.pdfarkmuzikllc
 
Goal Create a hypothetical threat assessment based on vulnerabilities.pdf
Goal Create a hypothetical threat assessment based on vulnerabilities.pdfGoal Create a hypothetical threat assessment based on vulnerabilities.pdf
Goal Create a hypothetical threat assessment based on vulnerabilities.pdfarkmuzikllc
 
import React, { useEffect } from react;import { BrowserRouter as.pdf
import React, { useEffect } from react;import { BrowserRouter as.pdfimport React, { useEffect } from react;import { BrowserRouter as.pdf
import React, { useEffect } from react;import { BrowserRouter as.pdfarkmuzikllc
 
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdf
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdfim solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdf
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdfarkmuzikllc
 
if any additional information is required let me know Work through t.pdf
if any additional information is required let me know Work through t.pdfif any additional information is required let me know Work through t.pdf
if any additional information is required let me know Work through t.pdfarkmuzikllc
 
I need help with providing a 2 page overview of the case study provi.pdf
I need help with providing a 2 page overview of the case study provi.pdfI need help with providing a 2 page overview of the case study provi.pdf
I need help with providing a 2 page overview of the case study provi.pdfarkmuzikllc
 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfarkmuzikllc
 
I have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdfI have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdfarkmuzikllc
 
I am trying to pass a date to a function in postgres.My function w.pdf
I am trying to pass a date to a function in postgres.My function w.pdfI am trying to pass a date to a function in postgres.My function w.pdf
I am trying to pass a date to a function in postgres.My function w.pdfarkmuzikllc
 
How can an event such as the terrorist attacks of September 2001 aff.pdf
How can an event such as the terrorist attacks of September 2001 aff.pdfHow can an event such as the terrorist attacks of September 2001 aff.pdf
How can an event such as the terrorist attacks of September 2001 aff.pdfarkmuzikllc
 
Hey! Could I possibly get some help with this Its a little confusi.pdf
Hey! Could I possibly get some help with this Its a little confusi.pdfHey! Could I possibly get some help with this Its a little confusi.pdf
Hey! Could I possibly get some help with this Its a little confusi.pdfarkmuzikllc
 

More from arkmuzikllc (18)

Here is selection filepublic class Selection { public static in.pdf
Here is selection filepublic class Selection {  public static in.pdfHere is selection filepublic class Selection {  public static in.pdf
Here is selection filepublic class Selection { public static in.pdf
 
Help me with these multiple-choice questions. Please dont use chat .pdf
Help me with these multiple-choice questions. Please dont use chat .pdfHelp me with these multiple-choice questions. Please dont use chat .pdf
Help me with these multiple-choice questions. Please dont use chat .pdf
 
Hello I am stuck in my code I know is everything is goid but I think.pdf
Hello I am stuck in my code I know is everything is goid but I think.pdfHello I am stuck in my code I know is everything is goid but I think.pdf
Hello I am stuck in my code I know is everything is goid but I think.pdf
 
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdfHeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
HeadWallRam Inc. has expanded its reach globally and needs to re-lay.pdf
 
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdf
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdfHINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdf
HINT For i = 1 , . . . ,n, let S[i] be the max sum ending with A[i].pdf
 
Hi, I need some assistance with this UML use case diagram. I manage .pdf
Hi, I need some assistance with this UML use case diagram. I manage .pdfHi, I need some assistance with this UML use case diagram. I manage .pdf
Hi, I need some assistance with this UML use case diagram. I manage .pdf
 
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdf
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdfGive an NFA that recognizes the language (10 + 110 + 101) and then .pdf
Give an NFA that recognizes the language (10 + 110 + 101) and then .pdf
 
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType Java file pr.pdf
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType  Java file pr.pdfIN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType  Java file pr.pdf
IN JAVA BJP5 Exercise 6.19 leetSpeak LanguageType Java file pr.pdf
 
Goal Create a hypothetical threat assessment based on vulnerabilities.pdf
Goal Create a hypothetical threat assessment based on vulnerabilities.pdfGoal Create a hypothetical threat assessment based on vulnerabilities.pdf
Goal Create a hypothetical threat assessment based on vulnerabilities.pdf
 
import React, { useEffect } from react;import { BrowserRouter as.pdf
import React, { useEffect } from react;import { BrowserRouter as.pdfimport React, { useEffect } from react;import { BrowserRouter as.pdf
import React, { useEffect } from react;import { BrowserRouter as.pdf
 
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdf
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdfim solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdf
im solving tha m-puzzel sliding tail problem where m = n^2 - 1 I ha.pdf
 
if any additional information is required let me know Work through t.pdf
if any additional information is required let me know Work through t.pdfif any additional information is required let me know Work through t.pdf
if any additional information is required let me know Work through t.pdf
 
I need help with providing a 2 page overview of the case study provi.pdf
I need help with providing a 2 page overview of the case study provi.pdfI need help with providing a 2 page overview of the case study provi.pdf
I need help with providing a 2 page overview of the case study provi.pdf
 
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdfI keep getting NullPointerExcepetion, can someone help me with spinL.pdf
I keep getting NullPointerExcepetion, can someone help me with spinL.pdf
 
I have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdfI have been tasked to write a code for a Singly Linked list that inc.pdf
I have been tasked to write a code for a Singly Linked list that inc.pdf
 
I am trying to pass a date to a function in postgres.My function w.pdf
I am trying to pass a date to a function in postgres.My function w.pdfI am trying to pass a date to a function in postgres.My function w.pdf
I am trying to pass a date to a function in postgres.My function w.pdf
 
How can an event such as the terrorist attacks of September 2001 aff.pdf
How can an event such as the terrorist attacks of September 2001 aff.pdfHow can an event such as the terrorist attacks of September 2001 aff.pdf
How can an event such as the terrorist attacks of September 2001 aff.pdf
 
Hey! Could I possibly get some help with this Its a little confusi.pdf
Hey! Could I possibly get some help with this Its a little confusi.pdfHey! Could I possibly get some help with this Its a little confusi.pdf
Hey! Could I possibly get some help with this Its a little confusi.pdf
 

Recently uploaded

24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...Nguyen Thanh Tu Collection
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportDenish Jangid
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscapingDr. M. Kumaresan Hort.
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code ExamplesPeter Brusilovsky
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...Nguyen Thanh Tu Collection
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 

Recently uploaded (20)

24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
24 ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH SỞ GIÁO DỤC HẢI DƯ...
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of TransportBasic Civil Engineering notes on Transportation Engineering & Modes of Transport
Basic Civil Engineering notes on Transportation Engineering & Modes of Transport
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
Climbers and Creepers used in landscaping
Climbers and Creepers used in landscapingClimbers and Creepers used in landscaping
Climbers and Creepers used in landscaping
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
SPLICE Working Group: Reusable Code Examples
SPLICE Working Group:Reusable Code ExamplesSPLICE Working Group:Reusable Code Examples
SPLICE Working Group: Reusable Code Examples
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 

Im creating a skip list program for my Algorithms and Advanced Data.pdf

  • 1. I'm creating a skip list program for my Algorithms and Advanced Data Structure class. I was tasked with implementing a find & add method for the skiplist. Here is my program, I'm running into a segmentation fault when I add my second value into the skiplist. My first issue that I fixed was that I wasn't checking the first node in the list to see if it was nullptr, but that was not my only issue apparently. Add Method: void SkipList::add(const ElemType &el) { // Create a new node with the given element Node *newNode = new Node(el); // Get the height of the new node int newHeight = newNode->nodeHeight; // If the height of the new node is greater than the current list height, // update the list height. if (newHeight > listHeight) { listHeight = newHeight; } // Array to keep track of nodes to update Node *update[MAX_NUM_LISTS]; // Start from the top level Node *current = head[listHeight - 1]; // Initialize current correctly // If the list is empty, assign the first node to head if (head[0] == nullptr) { head[0] = newNode; } else {
  • 2. // Loop to traverse through list for (int i = listHeight - 1; i >= 0; i--) { // Move forward in the current level while (current->next[i] != nullptr && current->next[i]->elem < el) { current = current->next[i]; } // Store the node before which the new node should be inserted update[i] = current; } // Insert the new node at each level for (int i = 0; i < newHeight; i++) { newNode->next[i] = update[i]->next[i]; update[i]->next[i] = newNode; } } } Find Method: bool SkipList::find(const ElemType &el) const { // Start from the top level Node *current = head[listHeight - 1]; bool found = false; // Iterate through the levels of the skip list for (int i = listHeight - 1; i >= 0; i--) { // Move forward in the current level until reaching the end or finding a larger element while (current->next[i] != nullptr && current->next[i]->elem < el) { current = current->next[i]; }
  • 3. // Check if the current node's next node contains the target element if (current->next[i] != nullptr && current->next[i]->elem == el) { found = true; // Element found } } return found; // Element not found in the skip list } Main: int main() { ElemType inputs[] = {5, 7, 8, 10, 12, 17, 19, 22, 28, 31, 33, 35, 42, 51, 59}; SkipList list1; cout << "For list1 (same as in the slide 10 of Skip List1 Lecture Note):n"; // for (int i=0; i(std::size(inputs)); i++) { cout << "add " << inputs[i] << endl; list1.add(inputs[i]); cout << "list1:n" << list1 << endl; }
  • 4. if (list1.find(33)) cout << "33 is found!n"; else cout << "33 is not found!!!!n"; if (list1.find(70)) cout << "70 is found!n"; else cout << "70 is not found!!!!n"; if (list1.find(10)) cout << "10 is found!n"; else cout << "10 is not found!!!!n"; cout << "n===================================n"; cout << "For list2:n"; ElemType inputs2[] = {19, 8, 17, 59, 12, 7, 5, 22, 28, 35, 33, 31, 42, 51, 10}; SkipList list2; for (int i = 0; i < static_cast(std::size(inputs2)); i++) { cout << "add " << inputs2[i] << endl; list2.add(inputs2[i]); cout << "list2:n" << list2 << endl; }
  • 5. if (list2.find(33)) cout << "33 is found!n"; else cout << "33 is not found!!!!n"; if (list2.find(70)) cout << "70 is found!n"; else cout << "70 is not found!!!!n"; if (list2.find(10)) cout << "10 is found!n"; else cout << "10 is not found!!!!n"; return 0; }