Jump to content

[C++] Singly Link List


Alright The Arrogant
 Share

Recommended Posts

I will make a topic about Singly Link List. This method is important for Queue and Stack. Let me try the best for this tutorial.

We need 2 files such as,

1. .CPP (Main)

2. .h (Header)

 

1. main.cpp

 

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

using namespace std;

int main()
{
list l;
int menu;
bool exit = false;
l.first = nil;
list *li = &l;
while(exit==false)
{
cout<<"Menu :\n";
cout<<"1. insert first\n";
cout<<"2. insert last\n";
cout<<"3. delete first\n";
cout<<"4. delete last\n";
cout<<"5. view data\n";
cout<<"6. search\n";
cout<<"7. exit\n";
cout<<"Menu :";cin>>menu;

if(menu==1){
address p = new elementList;
cout<<"Input New Data :";cin>>info(p);
p->next = nil;
insertFirst(li,p);
}else if(menu==2){
address p = new elementList;
cout<<"Input Data :";cin>>info(p);
p->next = nil;
insertLast(li,p);
}else if(menu==3){
deleteFirst(li);
}else if(menu==4){
deleteLast(li);
}else if(menu==5){
traversal(li);
}else if(menu==6){
int x;
cout<< "Input Data for Search : ";cin>>x;
searching(li,x);
}else if(menu==7){
exit = true;
}

}

return 0;
}

 

2. List.h

 

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED
#define nil NULL
#define info(p) (p)->info
#define next(p) (p)->next
#define first(l) ((l).first)
#define root(l) (l)->first
#include <iostream>

using namespace std;

typedef struct elementList *address;
struct elementList{
int info;
address next;
};

struct list{
address first;
};

void insertFirst(list *l, address p)
{
next(p) = first(*l);
//cout<<first(*l)<<endl;
first(*l)= p;
}

void insertLast(list *l, address p)
{
if(first(*l)==nil){
insertFirst(l,p);
}else{
address q = first(*l);
while(next(q)!=nil){
q=next(q);
}
next(q)=p;
}
}

void traversal(list *l)
{
address p = first(*l);
if(first(*l)==nil){
cout<<"Empty List\n";
}else{
while(p!=nil){
cout<<info(p)<<endl;
p = next(p);
}
}
}

void deleteFirst(list *l)
{
if(first(*l)==nil){
cout<<"Empty list\n";
}else{
address p =first(*l);
first(*l)=next(p);
next(p)=nil;
delete(p);
cout<<"First data has removed\n";
}
}

void deleteLast(list *l)
{
if(first(*l)==nil){
cout<<"empty list\n";
}else{
address prec;
address p =first(*l);
while(next(p)!=nil){
prec = p;
p = next(p);
}
//cout<<next(prec);
next(prec)=nil;
delete(p);
cout<< "Last data has removed\n";
}
}

void searching(list *l,int x)
{
address p = first(*l);
bool found = false;
int n=0;
while(found == false && p!=nil)
{
if(info(p)==x){
found=true;
}
n++;
p=next(p);
}
if(found==true){
cout<< "Data found at-"<<n<<endl;
}else{
cout<< "NO data\n";
}
}

We have 7 menus, insert first, insert last, delete first, delete last, view data, search, and exit.

 

In the Delete method, we need 2 variabel. First variabel for First(*L) and the second is next of address.

In the Insert method , we only need one variabel that is parameter input. So, the user will input data and we have made a procedure before for process this parameter.

View data is show your all data which you input such as first or last, this method will sort as you did before.

Search is for searching data what you want. It will show where is the location, first input, second, third , and so on.

 

I can't give you more. But i will answer your question.

 

I hope you like this tutorial. I can't speak romanian language. Thanks a lot :)



#endif // LIST_H_INCLUDED

Link to comment
Share on other sites

Hi,

Is a good tutorial, but, can be optimised, at menu condition can use a switch expresion

switch (menu)

{

case 1: {sintax}

case 2: {sintax}

.

.

case n: {sintax}

}

Is just a tip, sorry for mistakes, I write that message on mobile phone.

Link to comment
Share on other sites

@@CoSmE, Thanks for having time to read my topic.

Honestly, with using switch-case or if-then-else is very different.

If-then-else is use for logic operator such as < (less than), > (greater than), <= (less than or equal to), and so on. But, Switch-case is use for comparison between first variabel with other variabel. And, you are right CoSmE, i should use switch-case because that's not logic operator.

Thx a lot, bro ! :)

Link to comment
Share on other sites

@, Nice question.

So, if you wanna include a library, basicly you must use <library>.

But, when you want include a header or the library which not include on C++ library, you must use "yourlibrary".

"...." -> This is using for your own library or header

<...> -> This is using for C++ library or basic library e.g iostream, math, stdlib, and so on.


@@5061756C, :) Share what do you know about singly link list :)

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.