How can I write a function for a linked list that inserts? How can I write a remove function?
Hello, everyone.
Currently a bit confused as to how to write my insert and delete functions.
I am using a linked list class and trying to implement these functions to no avail.
Can anyone help me write these functions.
This is my code right now.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
#include <cstdlib>
#include <string>
#include <assert.h>
class Node
{
public:
int data;
Node * next;
Node(int x){
data = x;
next = NULL;
}
Node(int x, Node * y){
data = x;
next = y;
}
};
class SortedLinkedList
{
Node *head;
public:
SortedLinkedList(){
head = NULL;
}
void addANode(int value){
Node *p;
if(head == NULL)
head = new Node (value, NULL);
else{
p=head;
while(p->next !=NULL)
p=p->next;
p->next = new Node (value, NULL);
}
}
void print(){
Node * p;
p = head;
while(p != NULL){
if (p->data>=0){
std::cout << p->data << "\n";}
p = p->next;
}
}
int sortedGetLength()
{
int count = 0;
Node * iterator = head;
while(iterator != 0){
count++;
iterator = iterator->next;}
std::cout<<count<<std::endl;
return count;
}
bool sortedIsEmpty()
{
return head=NULL;
}
bool SortedFind(int x, int&k)
{
Node * first = head;
node* p=first;
do{
if(head->data==x){
return true;}
p=p->first;}
while(first->next!=NULL);}
return false;
}
void SortedDelete(int k, int &x, bool& success)
{
Node *temp = front, *prev = 0;
for(int i = 0;i < size; i++)
{
if (temp == n)
{
if (temp == front)
{
front = n->next;
}
else
{
prev->next = temp->next;
}
delete temp;
size --;
return true;
}
prev = temp;
temp = temp->next;
}
}
};
|
Topic archived. No new replies allowed.