Add to Favorites    Make Home Page 14339 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Beginners / Lab Assignments Home » Implimentation of a list using a linked list with pointers.

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Implimentation of a list using a linked list with pointers.
Author Ahmad Nashef
Author Email anashef [at] gmail.com
Description
Category C++ » Beginners / Lab Assignments
Hits 383327
Code Select and Copy the Code
//Implimentation of a list, using a linked list with pointers. #include <iostream.h> struct element { int number; element *next; }; /*************************************************************/ /* the operators declared on the type list.*/ void read(element *&list); void write(element *list); int length(element *list); int position(element *list, int num); void remove(element *&list,int position); void insert(element *&list,int new_num,int position); element *ele(element *list,int position); void add(int new_num,element *&list,element *this_el); void main() { element *list,*p,*q; int leng; read(list); write(list); q = ele (list,3); add (5,list,q); write(list); insert(list,10,2); write(list); remove(list,4); write(list); insert(list,12,1); write(list); leng = position(list,10); cout<< " pos of 10 is " << leng <<endl; cout<< " The length of the list is: " << length(list)<<endl; cout<<endl; ] } /* reading a list from input */ void read(element *&list) { element *p,*last; list = new element; p = list; cout<< " enter a number"; cin>>p->number; while (!cin.eof()) { p->next = new element; last = p; p = p->next; cout<< " enter a number "; cin>>p->number; } delete (p); last->next = NULL; } /***************************************************************/ /* writing a list on the output */ void write(element *list) { element *p; cout<<endl; p = list; while (p!=NULL) { cout<< p->number<<" "; p=p->next; } cout<<endl; } /****************************************************************/ /* Gets a list and returns its length */ int length (element *list) { element *p; int count=0; p=list; while (p!=NULL) { count++; p = p->next; } return count; } /******************************************************************/ /*Gets an element and returns its positon in the list. If the element*/ /*is not in the list, returns 0 */ int position(element *list,int num) { element *p; int i=1; for (p=list;p!=NULL;p=p->next) { if ((p->number)==num) return i; i++; } return 0; } /***************************************************************/ /*Gets a number and removes the element that stays in this position */ void remove(element *&list,int position) { element *before,*p; if (position==1) { before=list; list = list->next; delete (before); } else { before = ele (list,position-1); if (before!=NULL) { p = before->next; before->next = p->next; delete (p); } } } /***********************************************************/ /* Insert a new element to be the i-th element of the list */ /* the function uses the function add*/ void insert(element *&list,int new_num,int position) { element *p; if (position==1) add(new_num,list,NULL); else { p = ele(list,position-1); add(new_num,list,p); } } /****************************************************************/ /* Gets a position in the list and returns a pointer to the element*/ /* in this position*/ element *ele(element *list,int position) { element *p=list; int i; for (i=1;i<position;i++) { if (p==NULL) return p; p=p->next; } return p; } /************************************************************/ /* Add a new element after a given element.If the given element is NULL*/ /* Add the new element to be the first element*/ void add(int new_num,element *&list,element *this_el) { element *p; if (list==NULL) { list = new element; list->number = new_num; list->next = NULL; } else { p = new element; p->number = new_num; if (this_el==NULL) { p->next = list; list = p; } else { p->next=this_el->next; this_el->next = p; } } }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=778


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review