SlideShare a Scribd company logo
Prepared By : Rakesh Kumar                                             D.A.V.Centenary Public School Chander Nagar



                                             Pointer in C++
Pointer is a special variable, used to store the address of another variable of same data type.
 #include<iostream>
 #include<conio.h>                                                                          a
 using namespace std;
 int main()                                                                                  5
 {
    int a;                                                                                 10234
    a=5;
    cout<<"n Value of a :"<<a;           // 5 is the value stored at a
    cout<<"n Address of a :"<<&a;        // 10234 is the address of variable a
    getch( );
    return 0;
 }

&      :- is known as address operator
*      :- is known as value operator if it is operative on address, otherwise it behaves like a multiplication
          operator
Arithmetic Operation on Pointer : Only allowed arithmetic operation is addition(+) and Subtraction(-)
 Program                                                   Output
 #include<iostream>
 #include<conio.h>
 using namespace std;
 int main()
 {
   int a;
   a=5;
   int *b;
   b=&a;
   cout<<"n Value of b :"<<b<<endl;
   b=b+1 ;    // increase block size
   cout<<"n New value of b :"<<b<<endl;
   getch();
   return 0;
 }

Pointer of Pointer

 Program                                                   Output
 #include<iostream>
 #include<conio.h>
 using namespace std;
 int main()
 {
       int a;
       a=5;
       int *b;
       b=&a;
       int **c;
       c=&b;
       int ***d;
       d=&c;
       cout<<"n Address        of ann";
       cout<<"n Using          &a    :"<<&a;
       cout<<"n Using          b     :"<<b;
       cout<<"n Using          *c    :"<<*c;
       cout<<"n Using          **d   :"<<**d;

        cout<<"nnn Value of          an";
        cout<<"n Using a                       :"<<a;


                                                  Page 1 of 11
Prepared By : Rakesh Kumar                                           D.A.V.Centenary Public School Chander Nagar


        cout<<"n    Using   *(&a)   :"<<*(&a);
        cout<<"n    Using   *b      :"<<*b;
        cout<<"n    Using   **c     :"<<**c;
        cout<<"n    Using   ***d    :"<<***d;
        getch();
        return 0;
 }



Pointer as a function parameter

 Program                                                        Output
 // program to demonstrate call by pointer method
 #include<iostream>
 #include<conio.h>
 using namespace std;
 void change(int *a)     // parameter as pointer
 {
      *a = *a+20;
      }
 int main()
 {
   int x=20;
   cout<<"n Value of x before function call:"<<x;
   change(&x);        // passing parameter
   cout<<"n Value of x before function call:"<<x;
   getch();
   return 0;
 }

Pointer as a function Return type value
 Program                                                    Output
 #include<iostream>
 #include<conio.h>
 using namespace std;
 int* read(void)
 {
      int a;
      a=20;
      return(&a);
      }
 int main()
 {
     int *res;
     res = read(); // store retured address
     cout<<"n Value of a :"<<*res;
     cout<<"n Value of a :"<<*(read());
     getch();
     return 0;
 }


                                          Pointer and Array
When an array is defined like
      int x[5];        // The memory block is as shown below
                                          X

       100                   102                  104                      106                     108

      The address of 0th index block is called BASE ADDRESS and it can be obtained by the following
methods

                                             Page 2 of 11
Prepared By : Rakesh Kumar                                      D.A.V.Centenary Public School Chander Nagar


        (a)     x                         -> 100
        (b)     &x[0]                     ->100
        (c)     x+0                       ->100
        (d)     0+x                       ->100
        (e)     &0[x]                     ->100

Processing array as a pointer without using any extra pointer variable
 Program                               Output
 // program to access array as
 a pointer
 #include<iostream>
 #include<iomanip>
 #include<conio.h>
 #include<math.h>
 using namespace std;
 int main()
 {
     int x[10],i;
     // input phase
     for(i=0;i<10;i++)
     {
         cout<<"Enter value :";
           cin>>x[i];
       }
     // output
     cout<<"n Output using as
 a pointer:n";
       for(i=0;i<10;i++)
       cout<<setw(6)<<*(x+i);
     getch();
     return 0;
 }



Processing array as a pointer using extra pointer variable
 Program                                  Output
 #include<iostream>
 #include<iomanip>
 #include<conio.h>
 #include<math.h>
 using namespace std;
 int main()
 {
     int x[10],i,*p;
     p= x;    // assign base
 address to p
     // input phase
     for(i=0;i<10;i++)
        x[i]=rand();
     // output
     for(i=0;i<10;i++)
      {
        cout<<setw(6)<<*p;
        p++;
        }
     getch();
     return 0;
 }




                                             Page 3 of 11
Prepared By : Rakesh Kumar                                           D.A.V.Centenary Public School Chander Nagar


Accessing pointer as an array
 Program                                   Output
 #include<iostream>
 #include<iomanip>
 #include<conio.h>
 #include<math.h>
 using namespace std;
 void change(int *x)
 {
     int i;
     for(i=0;i<10;i++)
         x[i]=x[i]+20;
 }
 int main()
 {
     int x[10],i;
        // input phase
     for(i=0;i<10;i++)
      {
         cout<<"Enter value :";
         cin>>x[i];
      }
     // processing phase

     change(x);

      // output phase
      cout<<"n Modified List :";
       for(i=0;i<10;i++)
          cout<<setw(6)<<x[i];
       getch();
       return 0;
 }

                                           Pointer & String
String : it is an array of character, which always terminates with NULL (‘0’).

        Char str[80]=”RAKESH”; // It’s allocation in stin is as follows

 0       1  2   3   4   5 6 7 8 9 ……………………………………………………..79
 R     A K     E   S   H 0
101    102 103 104 105………………………………………………………………………… ………180

        cout<<str;  // Please note that only the base address of string has been given to cout
                    // and cout is here print the whole string not the base address
        Result : RAKESH

Example 2.

        char str[80]=”RAKESH”;
        cout<<*str; // now compiler try to print value stored at base address

        Result          : ‘R’

Example 3
     char str[80]=”RAKESH”;
     cout<<*++str; // process nearest first

        Result          : ‘A’

                                                Page 4 of 11
Prepared By : Rakesh Kumar                                    D.A.V.Centenary Public School Chander Nagar




Example 4
     char str[80]=”RAKESH”;
     cout<<++*str; // process nearest first

       Result           : ‘S’

Example 5
     char str[80]=”RAKESH”;
     cout<<++str;    // print 101 address onward upto NULL

       Result           : AKESH




                                  Pointers and Structure

 Assigning and Accesssing structure type Pointers
 #include<iostream>
 #include<conio.h>
 using namespace std;
 struct student
   {
          int roll;
          char name[30];
          char address[60];
   };
 int main()
 {
       student s;
       student *s1;
       s1= &s;
       //input phase
       cout<<"n Enter roll no :";
       cin>>s.roll;
       cout<<"n Enter name :";
       cin>>s.name;
       cout<<"n Enter address :";
       cin>>s.address;
       // output - using pointer variable
       cout<<"n Roll           :"<<(*s1).roll;    //        s1->roll
       cout<<"n Nane           :"<<(*s1).name;    //        s1->name;
       cout<<"n Address        :"<<(*s1).address; //        s1->address
       getch();
       return 0;
 }


                                              Page 5 of 11
Prepared By : Rakesh Kumar                                           D.A.V.Centenary Public School Chander Nagar




New ( ) : This function is used to assign memory to a pointer variable from the available memory heap. The
           function is responsible to calculate no of bytes and types of data, required.
Delete ( ) : This function is used to release assigned pointer memory to memory heap

 Accessing Pointer Variable using new( ) and delete ( ) function
 #include<iostream>
 #include<conio.h>
 using namespace std;
 struct student
   {
          int roll;
          char name[30];
          char address[60];
   };
 int main()
 {
       student *s;
       s= new(student);
       //input phase
       cout<<"n Enter roll no :";
       cin>>s->roll;
       cout<<"n Enter name :";
       cin>>s->name;
       cout<<"n Enter address :";
       cin>>s->address;
       // output - using pointer variable
       cout<<"n Roll           :"<<s->roll;
       cout<<"n Name           :"<<s->name;
       cout<<"n Address        :"<<s->address;
       delete(s);
       getch();
        return 0; }

Self Referential Structure: A structure which can have a variable of it’s own type inside it’s declaration,
                            then the structure is known as self referential structure.
Example
              struct student
                      {
                             Int roll;
                      `      student *s;
                      };

                                            Example of link list
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
struct node
{
         int info;
         node *ptr;
         };
int main()
{
      node *x,*y,*temp ;
      x= NULL;
      int choice;
      do
       {
               system("cls");
               cout<<"n 1.                  Add at beginning";


                                                Page 6 of 11
Prepared By : Rakesh Kumar                                        D.A.V.Centenary Public School Chander Nagar


                  cout<<"n 2.                  Add at end";
                  cout<<"n           3.               Delete from beginning";
                  cout<<"n 4.                  Display ";
                  cout<<"n 5.                  Exit";
                  cout<<"nnn Enter your choice :";
                  cin>>choice;
                  switch(choice)
                    {
                      case 1: if(x==NULL)
                                {
                                      x = new(node);
                                      cout<<"n Enter value :";
                                      cin>>x->info;
                                      x->ptr = NULL;
                                  }
                               else
                                  {
                                      temp = new(node);
                                      cout<<"n Enter value :";
                                      cin>>temp->info;
                                      temp->ptr = x;
                                      x = temp;
                                    }
                                  break;
                      case 2: if(x==NULL)
                                    {
                                        x = new(node);
                                        cout<<"n Enter value :";
                                        cin>>x->info;
                                        x->ptr = NULL;
                                      }
                                      else
                                        {
                                            y =x;
                                            while(y->ptr!=NULL)
                                            y = y->ptr;
                                            y->ptr = new(node);
                                            y = y->ptr;
                                            cout<<"n Enter value :";
                                            cin>>y->info;
                                            y->ptr = NULL;
                                          }
                                          break;
                      case 3:
                                        if(x==NULL)
                                          {
                                            cout<<"n Link List empty";
                                            getch();
                                            }
                                        else
                                            {
                                                temp =x;
                                                x = x->ptr;
                                                delete(temp);
                                              }
                                              break;
                        case 4:
                                            if(x==NULL)
                                                   cout<<"n Link list empty";
                                            else
                                                {
                                                  y = x;
                                                  while(y!=NULL)
                                                    {
                                                         cout<<setw(6)<<y->info;
                                                         y = y->ptr;


                                               Page 7 of 11
Prepared By : Rakesh Kumar                                  D.A.V.Centenary Public School Chander Nagar


                                                  }
                                  }
                                  getch();
                                  break;
                       case 5:      break;
                       default:
                             cout<<"n Wrong Choice.... Try again";
                             getch();
            } // end of switch statement
      }while(choice!=5);
return 0;
}


                                  LINK LIST EMPLEMENTED STACK
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
struct node
{
         int info;
         node *ptr;
         };

class stack
        {
                  node *x,*y,*temp;
               public:
                        stack()      // constructor to initialize variable
                          {
                              x= NULL;
                            }
                     void push(void); // function to add element
                     void pop(void);    // function to delete element
                     void display(void); // function to display stack element
               };

void stack::push()
{
       if(x==NULL)
           {
             x = new(node);
                   cout<<"n Enter value :";
                   cin>>x->info;
                   x->ptr = NULL;
           }
      else
           {
             temp = new(node);
                   cout<<"n Enter value :";
                   cin>>temp->info;
                   temp->ptr = x;
                   x = temp;
             }
      return;
}

void stack::pop(void)
{
  if(x==NULL)
    {
       cout<<"n Stack is empty";
         getch();
    }
  else


                                             Page 8 of 11
Prepared By : Rakesh Kumar                                     D.A.V.Centenary Public School Chander Nagar


      {
          temp =x;
          x = x->ptr;
          delete(temp);
      }
     return;
 }

void stack::display()
{
  if(x==NULL)
    cout<<"n Link list empty";
  else
     {
         y = x;
         while(y!=NULL)
           {
                cout<<setw(6)<<y->info;
                y = y->ptr;
           }
       }
  return;
  }

int main()
{
      stack S;
      int choice;
      do
       {
                             system("cls");
                             cout<<"n                    S T A C K  M E N U ";
                             cout<<"n       1.           Push";
                             cout<<"n       2.           Pop";
                             cout<<"n       3.           Display ";
                             cout<<"n       4.           Exit";
                             cout<<"nnn Enter your choice :";
                             cin>>choice;
                             switch(choice)
                              {
                                       case 1:            S.push();
                                                          break;
                                       case 2:     S.pop();
                                                   break;
                                       case 3:     S.display();
                                                          getch();
                                                   break;
                                       case 4:     break;
                                       default:
                                                   cout<<"n Wrong Choice.... Try again";
                                                   getch();
                                }
                                }while(choice!=4);
                return 0;
                }



                                   Link List Implemented queue
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
struct node
{


                                              Page 9 of 11
Prepared By : Rakesh Kumar                                 D.A.V.Centenary Public School Chander Nagar


          int info;
          node *ptr;
          };
class queue
        {
                node *x,*y,*temp;
             public:
                       queue()     // constructor to initliaze variable
                         {
                             x= NULL;
                           }
                    void add_element(void); // function to add element
                    void delete_element(void);   // function to delete element
                    void display(void); // function to display stack element
             };

void queue::add_element()
{
       if(x==NULL)
           {
             x = new(node);
                    cout<<"n Enter value :";
                    cin>>x->info;
                    x->ptr = NULL;
           }
      else
           {
             y = x;
               while(y->ptr!=NULL)
                  y = y->ptr;
               y->ptr = new(node);
               y = y->ptr;
               cout<<"n Enter value :";
               cin>>y->info;
               y->ptr = NULL;

             }
       return;
}

void queue::delete_element(void)
{
    if(x==NULL)
       {
         cout<<"n queue is empty";
           getch();
       }
    else
     {
         temp =x;
         x = x->ptr;
         delete(temp);
     }
    return;
  }
void queue::display()
{
  if(x==NULL)
     cout<<"n Queue is empty";
  else
       {
          y = x;
          while(y!=NULL)
            {
                 cout<<setw(6)<<y->info;
                 y = y->ptr;


                                           Page 10 of 11
Prepared By : Rakesh Kumar                                     D.A.V.Centenary Public School Chander Nagar


           }
      }
 return;
 }

int main()
{
      queue q;
      int choice;
      do
       {
                             system("cls");
                             cout<<"n                    QUEUE    M E N U ";
                             cout<<"n       1.           Add Element";
                             cout<<"n       2.           Delete Element";
                             cout<<"n       3.           Display ";
                             cout<<"n       4.           Exit";
                             cout<<"nnn Enter your choice :";
                             cin>>choice;
                             switch(choice)
                              {
                                       case 1:     q.add_element();
                                                   break;
                                       case 2:     q.delete_element();
                                                   break;
                                       case 3:     q.display();
                                                          getch();
                                                   break;
                                  case 4:
                                                   break;
                                default:
                                                   cout<<"n Wrong Choice.... Try again";
                                                   getch();
                                }
                                }while(choice!=4);
               return 0;
               }




                                              Page 11 of 11

More Related Content

What's hot

C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
Farhan Ab Rahman
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
kinan keshkeh
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
Farhan Ab Rahman
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functionsPrincess Sam
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
Russell Childs
 
C++11
C++11C++11
Pointers
PointersPointers
Pointers
sanya6900
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
Mohammad Shaker
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
Farhan Ab Rahman
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
Farhan Ab Rahman
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
Mohammad Shaker
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
Han Lee
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
Mouna Guru
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Kevlin Henney
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
Farhan Ab Rahman
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
Farhan Ab Rahman
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2rohassanie
 
Hidden Gems in Swift
Hidden Gems in SwiftHidden Gems in Swift
Hidden Gems in Swift
Netguru
 

What's hot (20)

C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
Opp compile
Opp compileOpp compile
Opp compile
 
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading2 BytesC++ course_2014_c3_ function basics&parameters and overloading
2 BytesC++ course_2014_c3_ function basics&parameters and overloading
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
Lec 45.46- virtual.functions
Lec 45.46- virtual.functionsLec 45.46- virtual.functions
Lec 45.46- virtual.functions
 
Day 1
Day 1Day 1
Day 1
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
C++11
C++11C++11
C++11
 
Pointers
PointersPointers
Pointers
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
Oops lab manual2
Oops lab manual2Oops lab manual2
Oops lab manual2
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
C++ TUTORIAL 8
C++ TUTORIAL 8C++ TUTORIAL 8
C++ TUTORIAL 8
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Hidden Gems in Swift
Hidden Gems in SwiftHidden Gems in Swift
Hidden Gems in Swift
 

Viewers also liked

Switkes01200543268
Switkes01200543268Switkes01200543268
Switkes01200543268Hitesh Wagle
 
Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966Hitesh Wagle
 
Lecture notes on infinite sequences and series
Lecture notes on infinite sequences and seriesLecture notes on infinite sequences and series
Lecture notes on infinite sequences and seriesHitesh Wagle
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahniHitesh Wagle
 

Viewers also liked (10)

Switkes01200543268
Switkes01200543268Switkes01200543268
Switkes01200543268
 
Convergence tests
Convergence testsConvergence tests
Convergence tests
 
Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966Quote i2 cns_cnr_25064966
Quote i2 cns_cnr_25064966
 
Computer
ComputerComputer
Computer
 
Lecture notes on infinite sequences and series
Lecture notes on infinite sequences and seriesLecture notes on infinite sequences and series
Lecture notes on infinite sequences and series
 
Diode logic crkts
Diode logic crktsDiode logic crkts
Diode logic crkts
 
Pointers
PointersPointers
Pointers
 
Zinkprinter
ZinkprinterZinkprinter
Zinkprinter
 
Fundamentals of data structures ellis horowitz & sartaj sahni
Fundamentals of data structures   ellis horowitz & sartaj sahniFundamentals of data structures   ellis horowitz & sartaj sahni
Fundamentals of data structures ellis horowitz & sartaj sahni
 
fire fighting robot
fire fighting robotfire fighting robot
fire fighting robot
 

Similar to P1

Oops presentation
Oops presentationOops presentation
Oops presentation
sushamaGavarskar1
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
sandeep54552
 
C++ practical
C++ practicalC++ practical
C++ practical
Rahul juneja
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
SirRafiLectures
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++Dendi Riadi
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
HIMANSUKUMAR12
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
DeepasCSE
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
ShashiShash2
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
Abubakar524802
 
C++ file
C++ fileC++ file
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Abu Saleh
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
DeepasCSE
 
Pointers
PointersPointers
Pointers
rajshreemuthiah
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
SWATIKUMARIRA2111030
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
George Erfesoglou
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
yamew16788
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical file
Mitul Patel
 

Similar to P1 (20)

Oops presentation
Oops presentationOops presentation
Oops presentation
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
C++ practical
C++ practicalC++ practical
C++ practical
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
 
Tugas praktikukm pemrograman c++
Tugas praktikukm  pemrograman c++Tugas praktikukm  pemrograman c++
Tugas praktikukm pemrograman c++
 
Computer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdfComputer_Practicals-file.doc.pdf
Computer_Practicals-file.doc.pdf
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptxC++ FUNCTIONS-1.pptx
C++ FUNCTIONS-1.pptx
 
C++ file
C++ fileC++ file
C++ file
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
oop Lecture 4
oop Lecture 4oop Lecture 4
oop Lecture 4
 
FUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptxFUNCTIONS, CLASSES AND OBJECTS.pptx
FUNCTIONS, CLASSES AND OBJECTS.pptx
 
Oop lab report
Oop lab reportOop lab report
Oop lab report
 
Pointers
PointersPointers
Pointers
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Include
IncludeInclude
Include
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Cs pritical file
Cs pritical fileCs pritical file
Cs pritical file
 

More from Hitesh Wagle

48695528 the-sulphur-system
48695528 the-sulphur-system48695528 the-sulphur-system
48695528 the-sulphur-systemHitesh Wagle
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructuresHitesh Wagle
 
Google search tips
Google search tipsGoogle search tips
Google search tipsHitesh Wagle
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructuresHitesh Wagle
 
9.double linked list circular
9.double linked list circular9.double linked list circular
9.double linked list circularHitesh Wagle
 

More from Hitesh Wagle (20)

48695528 the-sulphur-system
48695528 the-sulphur-system48695528 the-sulphur-system
48695528 the-sulphur-system
 
Diode logic crkts
Diode logic crktsDiode logic crkts
Diode logic crkts
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
 
Oops index
Oops indexOops index
Oops index
 
Google search tips
Google search tipsGoogle search tips
Google search tips
 
Applicationof datastructures
Applicationof datastructuresApplicationof datastructures
Applicationof datastructures
 
Green chem 2
Green chem 2Green chem 2
Green chem 2
 
Cryptoghraphy
CryptoghraphyCryptoghraphy
Cryptoghraphy
 
Notes
NotesNotes
Notes
 
Inheritance
InheritanceInheritance
Inheritance
 
Function notes 2
Function notes 2Function notes 2
Function notes 2
 
Function notes
Function notesFunction notes
Function notes
 
Flow of control
Flow of controlFlow of control
Flow of control
 
File
FileFile
File
 
Cpp
CppCpp
Cpp
 
Class object
Class objectClass object
Class object
 
Constructor
ConstructorConstructor
Constructor
 
Array notes
Array notesArray notes
Array notes
 
9.double linked list circular
9.double linked list circular9.double linked list circular
9.double linked list circular
 
Structure notes
Structure notesStructure notes
Structure notes
 

Recently uploaded

TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 

Recently uploaded (20)

TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 

P1

  • 1. Prepared By : Rakesh Kumar D.A.V.Centenary Public School Chander Nagar Pointer in C++ Pointer is a special variable, used to store the address of another variable of same data type. #include<iostream> #include<conio.h> a using namespace std; int main() 5 { int a; 10234 a=5; cout<<"n Value of a :"<<a; // 5 is the value stored at a cout<<"n Address of a :"<<&a; // 10234 is the address of variable a getch( ); return 0; } & :- is known as address operator * :- is known as value operator if it is operative on address, otherwise it behaves like a multiplication operator Arithmetic Operation on Pointer : Only allowed arithmetic operation is addition(+) and Subtraction(-) Program Output #include<iostream> #include<conio.h> using namespace std; int main() { int a; a=5; int *b; b=&a; cout<<"n Value of b :"<<b<<endl; b=b+1 ; // increase block size cout<<"n New value of b :"<<b<<endl; getch(); return 0; } Pointer of Pointer Program Output #include<iostream> #include<conio.h> using namespace std; int main() { int a; a=5; int *b; b=&a; int **c; c=&b; int ***d; d=&c; cout<<"n Address of ann"; cout<<"n Using &a :"<<&a; cout<<"n Using b :"<<b; cout<<"n Using *c :"<<*c; cout<<"n Using **d :"<<**d; cout<<"nnn Value of an"; cout<<"n Using a :"<<a; Page 1 of 11
  • 2. Prepared By : Rakesh Kumar D.A.V.Centenary Public School Chander Nagar cout<<"n Using *(&a) :"<<*(&a); cout<<"n Using *b :"<<*b; cout<<"n Using **c :"<<**c; cout<<"n Using ***d :"<<***d; getch(); return 0; } Pointer as a function parameter Program Output // program to demonstrate call by pointer method #include<iostream> #include<conio.h> using namespace std; void change(int *a) // parameter as pointer { *a = *a+20; } int main() { int x=20; cout<<"n Value of x before function call:"<<x; change(&x); // passing parameter cout<<"n Value of x before function call:"<<x; getch(); return 0; } Pointer as a function Return type value Program Output #include<iostream> #include<conio.h> using namespace std; int* read(void) { int a; a=20; return(&a); } int main() { int *res; res = read(); // store retured address cout<<"n Value of a :"<<*res; cout<<"n Value of a :"<<*(read()); getch(); return 0; } Pointer and Array When an array is defined like int x[5]; // The memory block is as shown below X 100 102 104 106 108 The address of 0th index block is called BASE ADDRESS and it can be obtained by the following methods Page 2 of 11
  • 3. Prepared By : Rakesh Kumar D.A.V.Centenary Public School Chander Nagar (a) x -> 100 (b) &x[0] ->100 (c) x+0 ->100 (d) 0+x ->100 (e) &0[x] ->100 Processing array as a pointer without using any extra pointer variable Program Output // program to access array as a pointer #include<iostream> #include<iomanip> #include<conio.h> #include<math.h> using namespace std; int main() { int x[10],i; // input phase for(i=0;i<10;i++) { cout<<"Enter value :"; cin>>x[i]; } // output cout<<"n Output using as a pointer:n"; for(i=0;i<10;i++) cout<<setw(6)<<*(x+i); getch(); return 0; } Processing array as a pointer using extra pointer variable Program Output #include<iostream> #include<iomanip> #include<conio.h> #include<math.h> using namespace std; int main() { int x[10],i,*p; p= x; // assign base address to p // input phase for(i=0;i<10;i++) x[i]=rand(); // output for(i=0;i<10;i++) { cout<<setw(6)<<*p; p++; } getch(); return 0; } Page 3 of 11
  • 4. Prepared By : Rakesh Kumar D.A.V.Centenary Public School Chander Nagar Accessing pointer as an array Program Output #include<iostream> #include<iomanip> #include<conio.h> #include<math.h> using namespace std; void change(int *x) { int i; for(i=0;i<10;i++) x[i]=x[i]+20; } int main() { int x[10],i; // input phase for(i=0;i<10;i++) { cout<<"Enter value :"; cin>>x[i]; } // processing phase change(x); // output phase cout<<"n Modified List :"; for(i=0;i<10;i++) cout<<setw(6)<<x[i]; getch(); return 0; } Pointer & String String : it is an array of character, which always terminates with NULL (‘0’). Char str[80]=”RAKESH”; // It’s allocation in stin is as follows 0 1 2 3 4 5 6 7 8 9 ……………………………………………………..79 R A K E S H 0 101 102 103 104 105………………………………………………………………………… ………180 cout<<str; // Please note that only the base address of string has been given to cout // and cout is here print the whole string not the base address Result : RAKESH Example 2. char str[80]=”RAKESH”; cout<<*str; // now compiler try to print value stored at base address Result : ‘R’ Example 3 char str[80]=”RAKESH”; cout<<*++str; // process nearest first Result : ‘A’ Page 4 of 11
  • 5. Prepared By : Rakesh Kumar D.A.V.Centenary Public School Chander Nagar Example 4 char str[80]=”RAKESH”; cout<<++*str; // process nearest first Result : ‘S’ Example 5 char str[80]=”RAKESH”; cout<<++str; // print 101 address onward upto NULL Result : AKESH Pointers and Structure Assigning and Accesssing structure type Pointers #include<iostream> #include<conio.h> using namespace std; struct student { int roll; char name[30]; char address[60]; }; int main() { student s; student *s1; s1= &s; //input phase cout<<"n Enter roll no :"; cin>>s.roll; cout<<"n Enter name :"; cin>>s.name; cout<<"n Enter address :"; cin>>s.address; // output - using pointer variable cout<<"n Roll :"<<(*s1).roll; // s1->roll cout<<"n Nane :"<<(*s1).name; // s1->name; cout<<"n Address :"<<(*s1).address; // s1->address getch(); return 0; } Page 5 of 11
  • 6. Prepared By : Rakesh Kumar D.A.V.Centenary Public School Chander Nagar New ( ) : This function is used to assign memory to a pointer variable from the available memory heap. The function is responsible to calculate no of bytes and types of data, required. Delete ( ) : This function is used to release assigned pointer memory to memory heap Accessing Pointer Variable using new( ) and delete ( ) function #include<iostream> #include<conio.h> using namespace std; struct student { int roll; char name[30]; char address[60]; }; int main() { student *s; s= new(student); //input phase cout<<"n Enter roll no :"; cin>>s->roll; cout<<"n Enter name :"; cin>>s->name; cout<<"n Enter address :"; cin>>s->address; // output - using pointer variable cout<<"n Roll :"<<s->roll; cout<<"n Name :"<<s->name; cout<<"n Address :"<<s->address; delete(s); getch(); return 0; } Self Referential Structure: A structure which can have a variable of it’s own type inside it’s declaration, then the structure is known as self referential structure. Example struct student { Int roll; ` student *s; }; Example of link list #include<iostream> #include<iomanip> #include<conio.h> using namespace std; struct node { int info; node *ptr; }; int main() { node *x,*y,*temp ; x= NULL; int choice; do { system("cls"); cout<<"n 1. Add at beginning"; Page 6 of 11
  • 7. Prepared By : Rakesh Kumar D.A.V.Centenary Public School Chander Nagar cout<<"n 2. Add at end"; cout<<"n 3. Delete from beginning"; cout<<"n 4. Display "; cout<<"n 5. Exit"; cout<<"nnn Enter your choice :"; cin>>choice; switch(choice) { case 1: if(x==NULL) { x = new(node); cout<<"n Enter value :"; cin>>x->info; x->ptr = NULL; } else { temp = new(node); cout<<"n Enter value :"; cin>>temp->info; temp->ptr = x; x = temp; } break; case 2: if(x==NULL) { x = new(node); cout<<"n Enter value :"; cin>>x->info; x->ptr = NULL; } else { y =x; while(y->ptr!=NULL) y = y->ptr; y->ptr = new(node); y = y->ptr; cout<<"n Enter value :"; cin>>y->info; y->ptr = NULL; } break; case 3: if(x==NULL) { cout<<"n Link List empty"; getch(); } else { temp =x; x = x->ptr; delete(temp); } break; case 4: if(x==NULL) cout<<"n Link list empty"; else { y = x; while(y!=NULL) { cout<<setw(6)<<y->info; y = y->ptr; Page 7 of 11
  • 8. Prepared By : Rakesh Kumar D.A.V.Centenary Public School Chander Nagar } } getch(); break; case 5: break; default: cout<<"n Wrong Choice.... Try again"; getch(); } // end of switch statement }while(choice!=5); return 0; } LINK LIST EMPLEMENTED STACK #include<iostream> #include<iomanip> #include<conio.h> using namespace std; struct node { int info; node *ptr; }; class stack { node *x,*y,*temp; public: stack() // constructor to initialize variable { x= NULL; } void push(void); // function to add element void pop(void); // function to delete element void display(void); // function to display stack element }; void stack::push() { if(x==NULL) { x = new(node); cout<<"n Enter value :"; cin>>x->info; x->ptr = NULL; } else { temp = new(node); cout<<"n Enter value :"; cin>>temp->info; temp->ptr = x; x = temp; } return; } void stack::pop(void) { if(x==NULL) { cout<<"n Stack is empty"; getch(); } else Page 8 of 11
  • 9. Prepared By : Rakesh Kumar D.A.V.Centenary Public School Chander Nagar { temp =x; x = x->ptr; delete(temp); } return; } void stack::display() { if(x==NULL) cout<<"n Link list empty"; else { y = x; while(y!=NULL) { cout<<setw(6)<<y->info; y = y->ptr; } } return; } int main() { stack S; int choice; do { system("cls"); cout<<"n S T A C K M E N U "; cout<<"n 1. Push"; cout<<"n 2. Pop"; cout<<"n 3. Display "; cout<<"n 4. Exit"; cout<<"nnn Enter your choice :"; cin>>choice; switch(choice) { case 1: S.push(); break; case 2: S.pop(); break; case 3: S.display(); getch(); break; case 4: break; default: cout<<"n Wrong Choice.... Try again"; getch(); } }while(choice!=4); return 0; } Link List Implemented queue #include<iostream> #include<iomanip> #include<conio.h> using namespace std; struct node { Page 9 of 11
  • 10. Prepared By : Rakesh Kumar D.A.V.Centenary Public School Chander Nagar int info; node *ptr; }; class queue { node *x,*y,*temp; public: queue() // constructor to initliaze variable { x= NULL; } void add_element(void); // function to add element void delete_element(void); // function to delete element void display(void); // function to display stack element }; void queue::add_element() { if(x==NULL) { x = new(node); cout<<"n Enter value :"; cin>>x->info; x->ptr = NULL; } else { y = x; while(y->ptr!=NULL) y = y->ptr; y->ptr = new(node); y = y->ptr; cout<<"n Enter value :"; cin>>y->info; y->ptr = NULL; } return; } void queue::delete_element(void) { if(x==NULL) { cout<<"n queue is empty"; getch(); } else { temp =x; x = x->ptr; delete(temp); } return; } void queue::display() { if(x==NULL) cout<<"n Queue is empty"; else { y = x; while(y!=NULL) { cout<<setw(6)<<y->info; y = y->ptr; Page 10 of 11
  • 11. Prepared By : Rakesh Kumar D.A.V.Centenary Public School Chander Nagar } } return; } int main() { queue q; int choice; do { system("cls"); cout<<"n QUEUE M E N U "; cout<<"n 1. Add Element"; cout<<"n 2. Delete Element"; cout<<"n 3. Display "; cout<<"n 4. Exit"; cout<<"nnn Enter your choice :"; cin>>choice; switch(choice) { case 1: q.add_element(); break; case 2: q.delete_element(); break; case 3: q.display(); getch(); break; case 4: break; default: cout<<"n Wrong Choice.... Try again"; getch(); } }while(choice!=4); return 0; } Page 11 of 11