All source code in C/ C++ Ask a C/ C++ Pro Discussion Forum Categories All jobs in C/ C++
simple,linked,list,example,with,various,opera
   Code/Articles � |  Newest/Best � |  Community � |  Jobs � |  Other � |  Goto � | 
CategoriesSearch Newest CodeCoding ContestCode of the DayAsk A ProJobsUpload
C/ C++ Stats

 Code: 1,245,082. lines
 Jobs: 173. postings

 How to support the site

 
Sponsored by:
Quick Search for:  in language:    
You are in:
 
Login





Latest postings for C/ C++.
Click here to see a screenshot of this code!Student Information System Version 1.0
By Jake Rodriguez Pomperada on 3/11

(Screen Shot)

Click here to see a screenshot of this code!heart break by vijendra
By vjendra singh on 3/11

(Screen Shot)

Click here to see a screenshot of this code!Highest and Lowest Numbers Determiner Version 3.0
By Jake Rodriguez Pomperada on 3/10

(Screen Shot)

Pyramid numbers
By Leomar V. Ramos on 3/10


Character Stuffing Program in C
By Himanshu Pandey on 3/9


Priority Scheduling Algorithm
By Mahmud_Hasan on 3/8


Click here to see a screenshot of this code!Simple Payroll Syste Using Object Oriented Programming
By Jake Rodriguez Pomperada on 3/8

(Screen Shot)

Black Jack OOP
By Micu Danie on 3/8


Click here to see a screenshot of this code!Tasks
By Ali Moussawi on 3/7

(Screen Shot)

Click here to see a screenshot of this code!vijendra's car
By vjendra singh on 3/6

(Screen Shot)

Taj Lahal
By vjendra singh on 3/6


To Find Minimum and Maximum Height of a Binary Tree
By jeyaveerapandiy an on 3/3


Click here to see a screenshot of this code!ATM Machine
By arifliminto86 on 3/3

(Screen Shot)

Click here to see a screenshot of this code!Gap Buffer varient without a gap
By Joe Smith on 3/2

(Screen Shot)

Click here to see a screenshot of this code!Simple calculator
By walid brahem on 3/2

(Screen Shot)

File Editor
By Prasad Priyadarshana Fernando on 3/2


Text Finder
By Prasad Priyadarshana Fernando on 3/2


Simple TCP/IP Client & Server
By Prasad Priyadarshana Fernando on 3/1


LUTTAPI-1
By melwin jose on 2/27


Prime Factorization
By SergiuXxCracker on 2/27


netcmdx
By sam lee on 6/18


Click here to put this ticker on your site!


Add this ticker to your desktop!


Daily Code Email
To join the 'Code of the Day' Mailing List click here!



 
 
   

A Simple Linked List

Print
Email
 
VB icon
Submitted on: 2/8/2010 9:19:25 AM
By: Maqsood  
Level: Intermediate
User Rating: Unrated
Compatibility:C, C++ (general)

Users have accessed this code 1032 times.
 
 
     A simple linked list example with various list operations. Demonstrates how to use a linked list class with a separate Node class. A good example for someone who want to learn about linked lists. Please comment
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.

//**************************************
//     
// Name: A Simple Linked List
// Description:A simple linked list exam
//     ple with various list operations. Demons
//     trates how to use a linked list class wi
//     th a separate Node class.
A good example for someone who want to learn about linked lists.
Please comment
// By: Maqsood
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/vb/scripts/Sh
//     owCode.asp?txtCodeId=13032&lngWId=3//for details.//**************************************
//     

#include <iostream.h>
#include <conio.h>


    class Node{
    private:
    int data;
    Node *nextNode; 
    
    public:
    void setData(int data)


        {
        this->data=data;
    } 

int getData() { return data; }
void setNext(Node *nextNode) { this->nextNode = nextNode; }
Node *getNext() { return nextNode; }
};
class List{ private: int size; Node *headNode; Node *currentNode; Node *lastCurrentNode; public: List(); void addData(int data); void remove(); void del(int pos, List l); void update(int pos, int newVal, List l); void search(int data, List l); void start(); int get(); bool next(); void traverse(List); };
List::List() { headNode = new Node(); headNode->setNext(NULL); currentNode = NULL; lastCurrentNode = NULL; size = 0; }
void List::addData(int data) { Node *newNode = new Node(); newNode->setData(data); if (currentNode == NULL) { headNode->setNext(newNode); newNode->setNext(NULL); lastCurrentNode = headNode; currentNode = newNode; }
else { newNode->setNext(currentNode->getNext()); currentNode->setNext(newNode); lastCurrentNode = currentNode; currentNode = newNode; }
size++; }
void List::remove() { if (currentNode != NULL && size != 0) { lastCurrentNode->setNext(currentNode->getNext()); delete currentNode; currentNode = lastCurrentNode; }
size--; }
bool List::next() { if (currentNode == NULL) return false; lastCurrentNode = currentNode; currentNode = currentNode->getNext(); if (currentNode == NULL || size == 0) return false; else return true; }
int List::get() { return currentNode->getData(); }
void List::traverse(List l) { Node *savedCurrentNode = l.currentNode; l.currentNode = l.headNode->getNext(); for (int i = 1;l.next();i++) { cout<<"Element "<<i<<":"<<l.get()<<"\n"; }
l.currentNode = savedCurrentNode; }
void List::search(int data, List l) { Node *savedCurrentNode = l.currentNode; l.currentNode = headNode->getNext(); bool found = false; for(int i=1;l.next();i++) { if (l.get() == data) { cout<<"Element "<<data<<" found at position "<<i; found = true; break; }
}
if (!found) cout<<"Element does not exist in the list!"; l.currentNode = savedCurrentNode; }
void List::update(int pos, int newVal, List l) { Node *savedCurrentNode = l.currentNode; l.currentNode = headNode->getNext(); //l.start(); bool update=false; for (int i=1;l.next();i++) { if (i==pos) { l.currentNode->setData(newVal); cout<<"\nData updated "<<" at position "<<pos<<"\n"; update = true; break; }
}
if (!update) cout<<"\n\nPosition does not exist"; l.currentNode = savedCurrentNode; }
void List::start() { currentNode = headNode; lastCurrentNode = headNode; }
void List::del(int pos, List l) { Node *savedCurrentNode = l.currentNode; l.currentNode = headNode->getNext(); for (int i=1;l.next();i++) { if (pos == i) { l.lastCurrentNode->setNext(l.currentNode->getNext()); delete l.currentNode; l.currentNode = l.lastCurrentNode; l.size--; cout<<"\n\n\n\nNode deleted\n\n"; break; }
}
l.currentNode = savedCurrentNode; }
main() { List l; l.addData(23); l.addData(45); l.addData(35); l.addData(12); l.addData(56); l.addData(11); l.addData(78); l.addData(99); //l.traverse(l); //l.remove(); //cout<<"After removal: \n\n"; //l.traverse(l); l.addData(101); //cout<<"After insertion: \n\n"; //l.traverse(l); //l.search(45,l); l.addData(101); l.addData(102); l.addData(103); l.addData(104); l.traverse(l); /* l.update(5,117,l); cout<<"\n\n\nAfter Updation:\n"; l.traverse(l); */ //l.update(10,678,l); //l.traverse(l); //l.search(1026,l); //l.addData(32423); //l.traverse(l); l.del(7,l); l.traverse(l); getch(); }

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.


 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Games | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | Open Letter from Moderators | About the Site | Feedback | Link to the Site | Awards | Advertising | Privacy

Copyright� 1997-2010 by Exhedra Solutions, Inc. All Rights Reserved.  By using this site you agree to its Terms and Conditions.   Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.