[Linked List] Week 1 Introduction To D.S.

6 views
Skip to first unread message

gaurav gupta

unread,
Aug 12, 2010, 4:42:25 PM8/12/10
to DS & Algo@itbhu
Hello friends,

Since many of 2nd year guys joined recently and don't have knowledge of Data Structure. So First week will be just introduction. I want 2nd year to submit their solutions and others to optimize/correct.

Data Structure : Linked List
Prerequisite : English ( To read book/web ), any programming language
Explanation : Linked List, Insertion, Deletion, Search
Code : Insertion, Deletion, Search


Useful Links :
http://en.wikipedia.org/wiki/Linked_list
http://cslibrary.stanford.edu/103/LinkedListBasics.pdf
http://www.youtube.com/watch?v=htzJdKoEmO0
http://www.google.co.in

Rule Of the Game for this week Only :

1. Since 2nd year guys are new so let them post first, 3rd and 4th year guys please post your solution after 19:00 only so 2nd year guys can get enough time.
2. No solution from CSE pass outs for this week, they can review the other solutions and post modifications/suggestions.
3. Please Please Please don't copy paste, Its not a competition its just for our learning.

--
Gaurav Gupta
Computer Science Undergraduate
IT BHU
GSOC 10 KDE marble
http://techespanto.wordpress.com/
http://gitorious.org/~1989gaurav

labham sahani

unread,
Aug 14, 2010, 2:35:54 AM8/14/10
to DS & Algo@itbhu
//linklist using concept of stack......
#include<iostream>
using namespace std;
struct linklist
{
int item;
linklist *link;
}*top,*n;
//define top=NULL in main()
void insertion(int m)
{
n=new linklist;
if(n)
{
n->item=m;
n->link=top;
top=n;
}
else
cout<<"no empty space";
}
void deletion()
{
if(top==NULL)
cout<<"list is empty";
else
n=top;
top=top->link;
delete n;
}
void search(int element)
{
int flag=0;
linklist *p=top;
while(p)
{
if(p->item==element)
{
flag=1;
break;
}
p=p->link;
}
if(flag==1)
cout<<"element is present in linklist";
else
cout<<"element is not present in linklist";
}






//linklist using concept of Queue




#include<iostream>
using namespace std;
struct linklist
{
int item;
linklist *link;
}*front,*curr,*n;
//define front=NULL & curr=NULL in main()
void insertion(int m)
{
n=new linklist;
if(n)
{
n->item=m;
n->link=NULL;
if(front==null)
{
front=n;
curr=n;
}
else
{
curr->link=n;
curr=n;
}
}
else
cout<<"no empty space";
}
void deletion()
{
if(front==NULL)
cout<<"list is empty";
else
n=front;
front=front->link;
delete n;
if(front==NULL)
curr=NULL;
}
void search(int element)
{
int flag=0;
linklist *p=front;
while(p)
{
if(p->item==element)
{
flag=1;
break;
}
p=p->link;
}
if(flag==1)
cout<<"element is present in linklist";
else
cout<<"element is not present in linklist";
}



On Aug 12, 4:42 pm, gaurav gupta <1989.gau...@googlemail.com> wrote:
> Hello friends,
>
> Since many of 2nd year guys joined recently and don't have knowledge of Data
> Structure. So First week will be just introduction. I want 2nd year to
> submit their solutions and others to optimize/correct.
>
> Data Structure : Linked List
> Prerequisite : English ( To read book/web ), any programming language
> Explanation : Linked List, Insertion, Deletion, Search
> Code : Insertion, Deletion, Search
>
> Useful Links :http://en.wikipedia.org/wiki/Linked_listhttp://cslibrary.stanford.edu/103/LinkedListBasics.pdfhttp://www.youtube.com/watch?v=htzJdKoEmO0http://www.google.co.in
>
> Rule Of the Game for this week Only :
>
> 1. Since 2nd year guys are new so let them post first, 3rd and 4th year guys
> please post your solution after 19:00 only so 2nd year guys can get enough
> time.
> 2. No solution from CSE pass outs for this week, they can review the other
> solutions and post modifications/suggestions.
> 3. Please Please Please don't copy paste, Its not a competition its just for
> our learning.
>
> --
> Gaurav Gupta
> Computer Science Undergraduate
> IT BHU
> GSOC 10 KDE marblehttp://techespanto.wordpress.com/http://gitorious.org/~1989gaurav<http://gitorious.org/%7E1989gaurav>

Shalabh Vidyarthi

unread,
Aug 14, 2010, 2:38:47 AM8/14/10
to ds--al...@googlegroups.com
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package link_list;

/**
 *
 * @author shalabh
 */
public class List {
    List N;
    int a;
    List()
    {
        N=null;
        a=0;
    }
}







/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package link_list;

/**
 *
 * @author shalabh
 */



import java.io.*;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)throws Exception {
        // TODO code application logic here
        DataInputStream k=new DataInputStream(System.in);
       int a,ans=1;
       
        List L=new List();
        L.a=Integer.parseInt(k.readLine());
        L.N=null;


        System.out.println("1:Insertion");
        System.out.println("2:Removal");
        System.out.println("3:Searching");
        System.out.println("4:Display");

        System.out.println("5:Exit");

List L1,L2;
int pos,i=0,term,ct=1;
L1=null;
L1=L;
L2=L;
L2.N=null;
        while(true)
        { ans=Integer.parseInt(k.readLine());
          i=0;
          if(ans==1)
            { ct++;
              L1 =new List();
                L1.a=Integer.parseInt(k.readLine());
                //L.N=L1;
                L.N=L1;
                L=L.N;
                L.N=null;
                
            }
            if(ans==2)
            {

                i=0;
                L1=L2;
               if(L==L2)
            {
                System.out.println("UnderFlow");
                continue;
            }
                pos=Integer.parseInt(k.readLine());
            System.out.println("L2.a:"+L2.a);
            System.out.println("L.a:"+L.a);
             
              while(i!=(pos-1))
              { i++;
             
              L1=L1.N;
              }
              System.out.println("ELEMENT:"+L1.a);
              if(pos!=ct)
              { if(L1.N.N!=null)
                {L1.N=L1.N.N;
                System.out.println("yes");
                }
                }
              else
              {System.out.println(L1.a);
                  L1.N=null;
              }
                  ct--;
            }

            if(ans==3)
            {
                term=Integer.parseInt(k.readLine());
                L1=L2;
                while(L1.N.a!=term)
                L1=L1.N;
                if(L1.N!=null)
                System.out.println(L1.N);
                else
                    System.out.println("NOT Found");
            }
          if(ans==4)
          {   System.out.println("List");
              L1=L2;
              while(L1.N!=null)
              {
                  System.out.println(L1.a);
                  L1=L1.N;
              }
          System.out.println(L1.a);
          }
          if(ans==5)
          {
              break;
          }
       }
}

}

Shalabh Vidyarthi

unread,
Aug 14, 2010, 4:48:10 AM8/14/10
to ds--al...@googlegroups.com
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package link_list;

/**
 *
 * @author shalabh
 */
public class List {
    List N;
    int a;
    List()
    {
        N=null;
        a=0;
    }
}


            //System.out.println("L2.a:"+L2.a);
            //System.out.println("L.a:"+L.a);
             
              while(i!=(pos-1))
              { i++;
             
              L1=L1.N;
              }
              //System.out.println("ELEMENT:"+L1.a);
              if(pos!=ct)
              { if(L1.N.N!=null)
                {L1.N=L1.N.N;
                //System.out.println("yes");
                }
                }
              else
              {System.out.println(L1.a);
                  L1.N=null;
              }
                  ct--;
            }

            if(ans==3)
            {
                term=Integer.parseInt(k.readLine());
                L1=L2;
                while(L1.N.a!=term)
                L1=L1.N;
                if(L1.N!=null)
                System.out.println(L1.N);
                else
                    System.out.println("NOT Found");
            }
          if(ans==4)
          {  // System.out.println("List");

prashant chaudhary

unread,
Aug 14, 2010, 5:05:28 AM8/14/10
to DS & Algo@itbhu
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct linklist
{
int iteam;
linklist *link;
}*n,*top;
void insrt(int p)
{
n=new linklist;
n->iteam=p;
n->link=top;
top=n;
}
void deletion()
{
linklist *k=top;
if(top==NULL)
{
cout<<"stack is empty";
}
else
{
top=top->link;
cout<<k->iteam<<" deleted "<<endl;
delete k;
}
}
void search(int p)
{
linklist *k=top;
int flag=0;
if(top==NULL)
{
cout<<"element is not present";
}
else
{
while(k)
{
if(k->iteam==p)
{
cout<<"element is present";
flag=1;
break;
}
k=k->link;
}
if(flag==0)
cout<<"element is not present";
}
}
void check()
{
int i,k,j;
cout<<"for insertion press 1,for deletion 2,searching3,exit 4";
cin>>i;
switch(i)
{
case 1:
cout<<"enter any no.";
cin>>k;
insrt(k);
check();
break;
case 2:
deletion();
check();
break;
case 3:
cout<<"enter d no. 2 search";
cin>>j;
search(j);
check();
break;
case 4:exit(0);
}
}
int main()
{
top=NULL;
check();



}


On Aug 12, 1:42 pm, gaurav gupta <1989.gau...@googlemail.com> wrote:
> Hello friends,
>
> Since many of 2nd year guys joined recently and don't have knowledge of Data
> Structure. So First week will be just introduction. I want 2nd year to
> submit their solutions and others to optimize/correct.
>
> Data Structure : Linked List
> Prerequisite : English ( To read book/web ), any programming language
> Explanation : Linked List, Insertion, Deletion, Search
> Code : Insertion, Deletion, Search
>
> Useful Links :http://en.wikipedia.org/wiki/Linked_listhttp://cslibrary.stanford.edu/103/LinkedListBasics.pdfhttp://www.youtube.com/watch?v=htzJdKoEmO0http://www.google.co.in
>
> Rule Of the Game for this week Only :
>
> 1. Since 2nd year guys are new so let them post first, 3rd and 4th year guys
> please post your solution after 19:00 only so 2nd year guys can get enough
> time.
> 2. No solution from CSE pass outs for this week, they can review the other
> solutions and post modifications/suggestions.
> 3. Please Please Please don't copy paste, Its not a competition its just for
> our learning.
>
> --
> Gaurav Gupta
> Computer Science Undergraduate
> IT BHU

Gaurav Gupta

unread,
Aug 14, 2010, 3:10:39 PM8/14/10
to DS & Algo@itbhu
Hello Friends,

Nice to see some solutions. Just want to add some points you should
keep in mind :
1. Don't paste your code here. Use http://pastebin.org/ to paste your
code and post link in the mailing list. So it would be better to
review the code and you can update your solution on the same page
itself
2. Add proper comments so we can understand easily otherwise it would
be a big mess after a week or two.
3. Use proper name and coding convention to maintain readbility.
http://en.wikipedia.org/wiki/Coding_conventions
4. Please in the starting of your code, attach logic as comment.

Some of you might think that all above thing is just waste of time,
but really believe me these all are best practices I learned from my
experiences and want you to learn all these things.


Hope to see some new coders :)
On Aug 14, 2:05 pm, prashant chaudhary <pcprashant...@gmail.com>
wrote:
> > Useful Links :http://en.wikipedia.org/wiki/Linked_listhttp://cslibrary.stanford.edu...

Gaurav Kumar

unread,
Aug 14, 2010, 3:16:43 PM8/14/10
to ds--al...@googlegroups.com
I would suggest http://www.ideone.com/ instead of pastebin. In ideone, you can compile and run your code with inputs. Also if you make a profile, you can make folders and save your codes.

--
You received this message because you are subscribed to the Google Groups "DS & Algo@itbhu" group.
To post to this group, send an email to ds--al...@googlegroups.com.
To unsubscribe from this group, send email to ds--algoitbh...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/ds--algoitbhu?hl=en-GB.


nishant.kumar.cse09

unread,
Aug 19, 2010, 12:33:18 PM8/19/10
to DS & Algo@itbhu
code for double linked list
http://pastebin.org/614248

On Aug 13, 1:42 am, gaurav gupta <1989.gau...@googlemail.com> wrote:
> Hello friends,
>
> Since many of 2nd year guys joined recently and don't have knowledge of Data
> Structure. So First week will be just introduction. I want 2nd year to
> submit their solutions and others to optimize/correct.
>
> Data Structure : Linked List
> Prerequisite : English ( To read book/web ), any programming language
> Explanation : Linked List, Insertion, Deletion, Search
> Code : Insertion, Deletion, Search
>
> Useful Links :http://en.wikipedia.org/wiki/Linked_listhttp://cslibrary.stanford.edu/103/LinkedListBasics.pdfhttp://www.youtube.com/watch?v=htzJdKoEmO0http://www.google.co.in
>
> Rule Of the Game for this week Only :
>
> 1. Since 2nd year guys are new so let them post first, 3rd and 4th year guys
> please post your solution after 19:00 only so 2nd year guys can get enough
> time.
> 2. No solution from CSE pass outs for this week, they can review the other
> solutions and post modifications/suggestions.
> 3. Please Please Please don't copy paste, Its not a competition its just for
> our learning.
>
> --
> Gaurav Gupta
> Computer Science Undergraduate
> IT BHU

Ayush Rawat

unread,
Aug 20, 2010, 4:32:06 AM8/20/10
to ds--al...@googlegroups.com
#include <iostream.h>

//structure defining a linklist
using namespace std;
struct node{
int info;               //content for a node
node *link;             //node type ptr to point to next location
}*a,*last;              //a=temp. pointer for a node
                       //last=last node added to linklist
void search(int k)
{
int t=0;
node *m=last;            //pointer set to last node
if(last==NULL)
cout<<"element not present"<<endl;  //nothing available
while(m)
{
if(m->info==k) 
{
  cout<<"element is present"<<endl;t=1;
}
m=m->link;                     //pointed to next location
}
if(t==1)
cout<<"element not present"<<endl;
}
void addNode(int k)
{
a=new node;                     //new node initialized
if(a)
{
 a->info=k;
 a->link=last;
 last=a;                         //all things set for new node... if memory space available
}
else
 cout<<"no space left"<<endl;
}
void delNode()
{
if(last==NULL)
cout<<"empty list"<<endl;
else
{a=last;
last=last->link;                  //last element deleted as referenced using stacks
delete a;
cout<<"last element deleted"<<endl;
}
}
dispList()
{
if(last==NULL)
cout<<"empty list"<<endl;
else
node *m=last;
while(m)
{
   cout<<m->info<<endl;
   m=m->link;
}
}
int main()
{
j:
cout<<"enter ur choice:"<<endl;
cout<<"1.Add a node to list"<<endl;
cout<<"2.Delete a node"<<endl;
cout<<"3.Search for a element"<<endl;
cout<<"4.Display list"<<endl;
int raw;
cin>>raw;
int k;
switch(raw)
{
case(1):{
cout<<"enter the value to be added to list"<<endl;
cin>>k;
addNode(k);break;
}
case(2):delNode();break;
case(3):{
cout<<"enter the value to be searched in the list"<<endl;
cin>>k;
search(k);
}
case(4):dispList();break;

default:goto j;break;
}
goto j;
}

<!-- RAWAT -->

Ayush Rawat
cse09,IT-BHU,
Varanasi.

masterm...@gmail.com
ayush.ra...@itbhu.ac.in



nishant.kumar.cse09

unread,
Aug 20, 2010, 12:41:47 PM8/20/10
to DS & Algo@itbhu
attached some comments for better understanding
http://pastebin.org/618406

On Aug 13, 1:42 am, gaurav gupta <1989.gau...@googlemail.com> wrote:
> Hello friends,
>
> Since many of 2nd year guys joined recently and don't have knowledge of Data
> Structure. So First week will be just introduction. I want 2nd year to
> submit their solutions and others to optimize/correct.
>
> Data Structure : Linked List
> Prerequisite : English ( To read book/web ), any programming language
> Explanation : Linked List, Insertion, Deletion, Search
> Code : Insertion, Deletion, Search
>
> Useful Links :http://en.wikipedia.org/wiki/Linked_listhttp://cslibrary.stanford.edu/103/LinkedListBasics.pdfhttp://www.youtube.com/watch?v=htzJdKoEmO0http://www.google.co.in
>
> Rule Of the Game for this week Only :
>
> 1. Since 2nd year guys are new so let them post first, 3rd and 4th year guys
> please post your solution after 19:00 only so 2nd year guys can get enough
> time.
> 2. No solution from CSE pass outs for this week, they can review the other
> solutions and post modifications/suggestions.
> 3. Please Please Please don't copy paste, Its not a competition its just for
> our learning.
>
> --
> Gaurav Gupta
> Computer Science Undergraduate
> IT BHU
Reply all
Reply to author
Forward
0 new messages