Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,147 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,998 people online right now. Registration is fast and FREE... Join Now!




Help with Linked Lists

 
Reply to this topicStart new topic

Help with Linked Lists, Using Linked List with class

UofMCreed
post 4 Oct, 2008 - 02:58 PM
Post #1


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 8


My Contributions


Im having a little trouble understanding how to code a linked list in C++ Code. I understand how the Linked List works, but i am having trouble writting it in code. I have a class and i have several member functions in the class. I am having a difficult time coding them. Im new to programming, i have just under a years experience, any help would be appreciated. I understand the concepts im having trouble thinking and making sence of how to code it. The preconditions and posts are there for each function. HELP ME PLZ!!!

CODE

//This is the implementation file for the class ListType

#include <iostream>
#include <cstddef> //to access NULL
#include "ListType.h"

typedef NodeType* NodePtr;
typedef int ItemType;

struct NodeType
{
ItemType item;
NodePtr next;
};

List::List()
//Post: listPtr is set to NULL.
{
listPtr = NULL;
}

//*****************************************************************

List::List(const List& otherList)
//Copy-constructor for ListType
{
//FILL IN CODE
}

//*****************************************************************
//This one in particuallar im confused on
bool List::IsThere(ItemType item) const
//Post: If item is in the list IsThere is
//        True; False; otherwise.
{
//FILL IN CODE
}

//*****************************************************************

void List::Insert(ItemType item)
//Pre: item is not already in the list.
//Post: item is the first item in the list.
{
//FILL IN CODE
}

//*****************************************************************

void List::Delete(ItemType item)
//Pre: item is in the list.
//Post: item is no longer in the list.
{
//FILL IN THE CODE
}

//*****************************************************************

void List::Print() const
//Post: Items in the list are printed to the screen.
{
//FILL IN THE CODE.
}

//*****************************************************************

int List::Length() const
//Post: Number of items have been counted; result returned
{
//FILL IN CODE
}

//*****************************************************************

List::~List()
//Post: All the components are deleted.
{
//FILL IN CODE
}
User is offlineProfile CardPM

Go to the top of the page

Sadaiy
post 4 Oct, 2008 - 03:16 PM
Post #2


New D.I.C Head

*
Joined: 3 Oct, 2008
Posts: 35



Thanked 1 times
My Contributions


there are a lot of tutorials on the net about linked list (some are on this site) they will give you the code of the implementation of most of the functions you have listed.
User is online!Profile CardPM

Go to the top of the page

JackOfAllTrades
post 4 Oct, 2008 - 03:25 PM
Post #3


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 501



Thanked 44 times

Dream Kudos: 25
My Contributions


Your list class must maintain a pointer to the head node of the list.
cpp
//This one in particuallar im confused on
bool List::IsThere(ItemType item) const
//Post: If item is in the list IsThere is
// True; False; otherwise.
{
// Pseudocode:
// 1. Create a local pointer, pointing to the head of the list; if head is NULL, list is empty so return false.
// 2. Start loop of your choice.
// 3. Check the value of the pointer's item, compare to passed-in ItemType, if the same, return true.
// 4. If the value of the pointer's next is NULL, return false, else set local pointer to value of next pointer and continue loop.
}
User is offlineProfile CardPM

Go to the top of the page

Sadaiy
post 4 Oct, 2008 - 05:23 PM
Post #4


New D.I.C Head

*
Joined: 3 Oct, 2008
Posts: 35



Thanked 1 times
My Contributions


Ok , here is my very basic implementation of those functions you have asked for but note that i couldn't get the copy constructor to work right so I left it empty... Also note that instead of listitemtype i used int...


cpp

#include <iostream>
#include "list.h"

using namespace std;

list::list()
{
head = NULL;
tail = NULL;
}

list::list(const list& otherList)
{

}

void list::insert(int data)
{
if(head == NULL)
{
current = new node;
head = current;
tail = current;
current->prev = NULL;
current->next = NULL;
current->data = data;
}
else if(head != NULL)
{
current = new node;
current->next = NULL;
tail->next = current;
current->prev = tail;
tail = current;
current->data = data;
}
}

void list::print() const
{
node *display;
display = head;

while(display != NULL)
{
cout << display->data << " ";
display = display->next;
}
}

int list::length() const
{
int Length = 0;

node *loopPtr;
loopPtr = head;

while(loopPtr != NULL)
{
++Length;
loopPtr = loopPtr->next;
}

return Length;
}

bool list::isThere(int data) const
{
node *testPtr;
testPtr = head;

while(testPtr != NULL)
{
if(testPtr->data == data)
return true;
else
testPtr = testPtr->next;
}

return false;
}

void list::del(int data)
{
node *delPtr;
delPtr = head;

while(delPtr != NULL)
{
if(delPtr->data == data)
break;
else
delPtr = delPtr->next;
}

if(delPtr == head)
{
head = delPtr->next;
delPtr->next->prev = delPtr->prev;
}
else if(delPtr == tail)
{
tail = delPtr->prev;
delPtr->prev->next = delPtr->next;
}
else if(delPtr != NULL)
{
delPtr->next->prev = delPtr->prev;
delPtr->prev->next = delPtr->next;
}
else
{
cout << "That value you entered is not part of the list!" << endl;
}
}

list::~list()
{
node* clear = head;
node* var = NULL;

while(clear)
{
var = clear->next;
delete clear;
clear = var;
}

var = NULL;
clear = NULL;
}


This post has been edited by Sadaiy: 4 Oct, 2008 - 05:25 PM
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 01:02PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month