0% found this document useful (0 votes)
3 views26 pages

Basic Computer

The document is a lab manual for Basic Computer Engineering (BT-205) at Bhopal Institute of Technology & Science, detailing various experiments and practices. It covers internal and external DOS commands, basic Linux commands, MS Windows operations, and the use of MS Word and Excel. Each section outlines specific aims, theoretical background, and practical tasks for students to complete.

Uploaded by

phachp247
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views26 pages

Basic Computer

The document is a lab manual for Basic Computer Engineering (BT-205) at Bhopal Institute of Technology & Science, detailing various experiments and practices. It covers internal and external DOS commands, basic Linux commands, MS Windows operations, and the use of MS Word and Excel. Each section outlines specific aims, theoretical background, and practical tasks for students to complete.

Uploaded by

phachp247
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

lOMoARcPSD|69108464

BT-205 BCE LAB Manual - summy and imp notes

basic computer engg (Rajiv Gandhi Proudyogiki Vishwavidyalaya)

messages.pdf_cover_qr_code_label

messages.studocu_not_sponsored_or_endorsed_by_college
messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

BH BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BASIC COMPUTER ENGINEERING

BT-205

LIST OF EXPERIMENTS

1. Study and practice of Internal & External DOS commands.

2. Study and practice of Basic linux Commands – ls, cp, mv, rm, chmod, kill, ps etc.

3. Study and Practice of MS windows – Folder related operations, My-Computer,

window explorer, Control Panel,

4. Creation and editing of Text files using MS- word.

5. Creation and operating of spreadsheet using MS-Excel.

6. Creation and editing power-point slides using MS- power point

7. Creation and manipulation of database table using SQL in MS-Access.

8. WAP to illustrate Arithmetic expressions

9. WAP to illustrate Arrays.

10. WAP to illustrate functions.

11. WAP to illustrate constructor & Destructor

12. WAP to illustrate Object and classes.

13. WAP to illustrate Operator overloading

14. WAP to illustrate Function overloading

15. WAP to illustrate Derived classes & Inheritance

16. WAP to insert and delete and element from the Stack

17. WAP to insert and delete and element from the Queue

18. WAP to insert and delete and element from the Linked List.

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-1

AIM: Study and practice of Internal & External DOS commands.

THEORY: In DOS systems, the internal commands reside in [Link], which


loads into memory when the computer system is started; these commands do not reside on
disk. The external commands are files that do reside on disk and have an extension of .COM,
.EXE, or .BAT. Both command types are executed from the MS-DOS prompt.

Internal Command

1. CLS: this command is used to clean the screen.

2. DIR: this command allows the user to see all files and sub-directory in the current
directory. DIR Command lists file information in five columns; (first) column gives primary
name of the file (second) column gives extension of the file (third) column gives the file size
- number of bytes used; (fourth) column gives the last updated date; (fifth) column gives the
last updated time.

Dir/p - page by page display of file name and directory names;

Dir/w - width-wise display;

Dir/s - displays all sub directory and files in the sub-directory of current directory.

3. Copy con this command copies whatever typed on keyboard to the file; the file can be
closed by giving the command A2 or F6 key: eg: Copy con test .....A2.

4. Date it displays two system date and allows the user to change it if desired ; it is displayed
in the form of mm - dd - yy; eg:c:\> date - enter.

5. Time it displays the system type and enables the user to change it; eg: C:\> Time - enter.

6. md(mkdir) it creates a new directory in sub directory in the current directory; eg: C:\> md
<New directory name>

7. rd(rmdir) this command is used to remove a directory from the disk; it can't remove a
directory which contains sub directory or files, ie, the child should be removed from the
parent; similarly this command can't remove the current directory and root directory. Syntax:
c:\> RD<Dir name>.

8. Type it displays the content of saved file; eg: C:\> Type> file name.

9. Ren this command changes the name of existing file or directory: Syntax: C:\> ren <old
name> new name>

10. Delete delete a file from current directory; Syntax: C;\> del<file name>

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

11. Ver it displays the version of DOS currently being used in the system; Syntax: C:> ver

12. Copy it copies the given file or files from the source directory to the largest directory;
Syntax:C:\> copy<source file name> <target file name>.

13. Prompt allows the user to set a new DOS prompt instead of usual C:\> or A:\>; eg C:\>
prompt pcc; Prompt$p$g - this allows you to reset default prompt; Prompt $d (current date);
Prompt $t (current time);

External Command

1. Attrib this command is used for protecting the files from accidental changes or
modification. It can also be used for making a hidden file, archive files, read only files;
Syntax: Attrib +R/-R/+H/-H/+A/-A <file name> +FR protects the file by making it read only,
-R removes the read only protection; eg: Attrib + r <file name >

2. Scandisk/ Chkdisk this command checks the status of the disk; it shows a graphical
display, information about the user file.

3. Tree this command graphically displays the path of each directory and sub directory in
given drive; Syntax: C:\> tree<

4. More it displays one screen of data at a time and is used with another command when one
screen is full; if you press any key on the next screen is displayed: Syntax C:\> type [Link]|
more.

5. Edit the command loads the MSDOS editor, where we can edit files, create new files, open
existing files; Syntax: C:\> edit < file name>

6. Label a label is a name given to a disk which refers to collection of filers and directories
on disk; Syntax: C:\>label A.

7. Sort this command is used for sorting data and displaying the result on the screen:
Syntax:C:\>dir/sort/r (reverse order)

8. Format; this command prepares a disk by arranging random magnetic impulses in to a


series of track and sectors so that it is addressable by a DOS version; Syntax : C:\> format
A:/s

9. Sys this command transfers MSDOS System files to specified areas to make the disk boo
table; Syntax: C:\>Sys A:<

10 Pipes (|) it connects two files ie the standard output of one filter command becomes
standard input of another filter; eg Dir/Sort/ more ||

11. Batchfiles all batch files on DOS must have the file extn on bat to execute the batch file,
the user has just type the file name and press enter key, in addition to usual DOS command.

12. Echo this command can be used to display a message on the screen

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

13. Pause when this command is obeyed, the system waits for the user to press a key by
displaying a line "strike a key when ready"

14. Rem a command or remark can be used on batch file by the rem command; to symbol @
can be put in a REM command to prevent DOS from displaying the commend during the
execution of batch files.

[Link] the purpose of this file is to tell DOS how to configure the computer; it contains
configurable parameters of DOS, such as number of drives, files, buffers that can be opened
at a time.

break - ON/OFF it can be used to change the frequency to check the break character (Ctrl+c,
ctrl+break); it break + OFF, then the frequency of checking is less, otherwise it is ON, then it
makes DOS extensively check the break character.

Buffer = 20/10 it tells DOS how many disk buffers be maintained; the default value is 2 each
buffers of 528 bytes (512 + 16); this feature is needed for file manipulation, because the
performance increase with the addition of buffers.

Device: [Link]/V [Link]/Tapeover it tells DOS how to include a particular device


driver which controls and configure a device such as Floppy disl or Tape Unit.

Files = 10/20/30 it tells DOS how many files to be opened at one time, its default value is 8; a
max of 99 files can be opened at a time

Stacks = 15, 128it tells DOS the number of stacks to be used by the H/W interrupts; the
default value is 9 with 128 byte.

Shell = [Link] it tells DOS to install the command processor from a particular path
such as Shell = C:\DOS\ [Link]

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-2

AIM: Study and practice of Basic linux Commands – ls, cp, mv, rm, chmod, kill, ps etc.

ls [option(s)] [file(s)]

If you run ls without any additional parameters, the program will list the contents of the
current directory in short form.

-l

detailed list

-a

displays hidden files

cp [option(s)] sourcefile targetfile

Copies sourcefile to targetfile.

-i

Waits for confirmation, if necessary, before an existing targetfile is overwritten

-r

Copies recursively (includes subdirectories)

mv [option(s)] sourcefile targetfile

Copies sourcefile to targetfile then deletes the original sourcefile.

-b

Creates a backup copy of the sourcefile before moving

-i

Waits for confirmation, if necessary, before an existing targetfile is overwritten

rm [option(s)] file(s)

Removes the specified files from the file system. Directories are not removed by rm unless
the option -r is used.

-r

Deletes any existing subdirectories

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

-i

Waits for confirmation before deleting each file.

kill [option(s)] process ID

Unfortunately, sometimes a program cannot be terminated in the normal way. However, in


most cases, you should still be able to stop such a runaway program by executing
the kill command, specifying the respective process ID (see top and ps).

kill sends a TERM signal that instructs the program to shut itself down. If this does not help,
the following parameter can be used:

-9

Sends a KILL signal instead of a TERM signal, with which the process really
is annihilated by the operating system. This brings the specific processes to an end in almost
all cases.

ps [option(s)] [process ID]

If run without any options, this command displays a table of all your own programs or
processes — those you started. The options for this command are not preceded by hyphen.

aux

Displays a detailed list of all processes, independent of the owner.

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-3

AIM: Study and Practice of MS windows – Folder related operations, My-Computer,


window explorer, Control Panel.

THEORY:

Folders
In Windows, the files, folders, and subfolders are represented by small pictures called icons.
The icon for folders and subfolders intuitively looks like a folder. Every Windows folder
provides easy access to common file and folder management tasks. When you open any
folder on your computer, a list of hyperlinked tasks is displayed next to the folder contents.
You can select a file or folder, and then click a task to rename, copy, move, or delete it.

Folder related operations



Create

Delete

Rename

Copy

Move

Modify

Customize

My Computer

In Windows, you can locate and manage your files and directories (folders) in either My
Computer or Explorer. My Computer is most convenient for viewing one directory or
window at a time; Explorer is your best choice when you want to view or reorganize files in
different parts of your computer. In My Computer, you choose a drive, and by Default, view
icons or a list of folders and files in that drive. Your view is one window at a time, unless you
choose explore in My Computer to move to Explorer, which makes it easy to work in a
particular folder, but difficult to move between folders. When you open a folder in My
Compute, a new window opens; when you open a folder within that window, another window
opens. Soon your screen is cluttered with windows, unless you initially choose, in view,
options to use a single window.

Windows Explorer
Windows Explorer is an application that provides detailed information about your files,
folders, and drives. You can use it to see how your files are organized and to copy, move, and
rename files, as well as perform other tasks pertaining to files, folders, and [Link]
simplest way to access Windows Explorer is to right-click the Start menu or the My

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

Computer icon and selects Explore. If your keyboard has a Windows logo key, you can press
Win-e to launch Windows Explorer. Alternatively, you can access it from the Start menu. In
newer operating systems, it is under Accessories.

Control Panel

Control Panel is full of specialized tools that are used to change the way Windows looks and
behaves. Some of these tools help you adjust settings that make your computer more fun to
use. For example, use Mouse to replace standard mouse pointers with animated icons that
move on your screen, or use Sounds and Audio Devices to replace standard system sounds
with sounds you choose. Other tools help you set up Windows so that your computer is easier
to use. For example, if you are left-handed, you can use Mouse to switch the mouse buttons
so that the button on the right performs the primary functions of selecting and dragging. To
open Control Panel, click Start and then click Control Panel. If your computer is set up in
Classic view using the more familiar Start menu, click Start, point to Settings, and then click
Control Panel.

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-4

AIM: Creation and editing of Text files using MS- word.

THEORY:

MS-Word: Microsoft Word is a word processing software package. You can use it to type
letters,
reports, and other documents. This lesson introduces you to the Word window. You use the
Word window to interact with Microsoft Word.

Getting Started
Open Microsoft Word by clicking on Start > All Programs and selecting Microsoft Word
from the list. Word will open with a blank document. You will see the flashing cursor at the
top left corner of the page, ready for you to start typing.

If you hold your mouse over any of the icons in the menu bars or toolbars, you will see a
quick description of what those buttons do. If you can't see any of the toolbars mentioned in
this article (mainly the Standard and Formatting Toolbars), you may have to instruct Word to
show them by clicking View then Toolbars and putting a tick by any toolbars you want to
show.

TASK:
Create a MS-Word .doc file contain yours complete biodata.

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-5

AIM: Creation and operating of spreadsheet using MS-Excel.

THEORY:

MS-Excel:
Microsoft Excel is an electronic spreadsheet that runs on a personal computer. You can use it
to organize your data into rows and columns. You can also use it to perform mathematical
calculations quickly. This tutorial teaches Microsoft Excel basics. Although know ledge of
how to navigate in a Windows environment is helpful, this tutorial was created for the
computer novice. This lesson will introduce you to the Excel window. You use the window to
interact with Excel.
Getting Started

Click on the Start button, point to Programs, and following by Microsoft Office and click on
Microsoft Office Excel 2003
.
TASK:
Create a MS-Excel .xls file contain mark sheet of 10th and 12th Standards.

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-6

AIM: Creation and editing power-point slides using MS- power point.

THEORY:

MS-Power Point: PowerPoint is a presentation software package. With PowerPoint, you can
easily create slide shows. Trainers and other presenters use slide shows to illustrate their
presentations. This tutorial teaches PowerPoint basics. This lesson introduces you to the
PowerPoint window. You use the window to interact with PowerPoint. Microsoft
PowerPoint 2003 is a program designed to create and use a presentation. This Guide will take
you through getting started and building your confidence in the skills needed to use the
software productively

Getting Started
To start PowerPoint, go to the Start menu and select Programs ->Microsoft Office ->Power
Point.

TASK:
Create a MS-Power Point Presentation .ppt file Covers the topic “Computer Organization

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-7

AIM: Creation and manipulation of database table using SQL in MS-Access.

THEORY:

MS-Access: Microsoft Access 2003 is a powerful, yet easy to learn, relational database
application for Microsoft Windows. It introduces fundamental database concepts and
operations and illustrates how they are performed in Microsoft Access 2003. Access is a
pseudo-relational database management system from Microsoft that combines the relational
Microsoft Jet Database Engine with a graphical user interface and software-development
tools. It is a member of the Microsoft Office suite of applications

Getting Started

•Click the Windows Start button, located in the bottom-left corner of the screen.
•Use the mouse to move the pointer over the words All Programs.
•A menu pops up to the right of All Programs. The programs and menus you see listed
depend on the programs installed on your computer, so your menu will probably look
somewhat different from other users' menus.
•On the All Programs menu, move the pointer over the words Microsoft Office 2003, then
point to and click Microsoft Office Access 2003.

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-8

AIM: WAP to illustrate Arithmetic expressions

CODE:
#include<iostream>
main()
{
int a, b, c, d;

a = 15;
b = 10;
c = ++a - b;

printf("a = %d b = %d c = %d\n",a, b, c);

d = b++ +a;
printf("a = %d b = %d d = %d\n",a, b, d);

printf("a/b = %d\n", a/b);


printf("a%%b = %d\n", a%b);
printf("a *= b = %d\n", a*=b);
printf("%d\n", (c>d) ? 1 : 0);
printf("%d\n", (c<d) ? 1 : 0);
}

OUTPUT:

a = 16 b = 10 c = 6
a = 16 b = 11 d = 26
a/b = 1
a%b = 5
a *= b = 176
0
1

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-9

AIM: WAP to illustrate Arrays.


.
CODE:

#include <iostream>

using namespace std;

int main()

int arr[10],sum=0,i;

cout<<"Enter 10 numbers"<<endl;

for(i=0;i<10;i++)

cin>>arr[i];

sum = sum+arr[i];

cout<<"Sum = "<<sum;

OUTPUT:

Enter 10 numbers
13
52
4
-41
32
11
19
7
2
25
Sum = 124

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-10

AIM: WAP to illustrate functions.

CODE:

int max(int x, int y)


{
if (x > y)
return x;
else
return y;
}

// main function that doesn't receive any parameter and


// returns integer.
int main(void)
{
int a = 10, b = 20;

// Calling above function to find max of 'a' and 'b'


int m = max(a, b);

printf("m is %d", m);


return 0;
}

OUTPUT:
m is 20

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-11

AIM: WAP to illustrate constructor & Destructor.

CODE:

#include <iostream>
using namespace std;

class construct {
public:
int a, b;

// Default Constructor
construct()
{
a = 10;
b = 20;
}
};

int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 1;
}
OUTPUT:

a: 10
b: 20

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-12

AIM: WAP to illustrate Object and classes.

CODE:

#include <bits/stdc++.h>
using namespace std;
class Geeks
{
// Access specifier
public:

// Data Members
string geekname;

// Member Functions()
void printname()
{
cout << "Geekname is: " << geekname;
}
};

int main() {

// Declare an object of class geeks


Geeks obj1;

// accessing data member


[Link] = "Abhi";

// accessing member function


[Link]();
return 0;
}
OUTPUT:
Geekname is: Abhi

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-13

AIM: WAP to illustrate Operator overloading

CODE:

#include<iostream>
using namespace std;

class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {real = r; imag = i;}

// This is automatically called when '+' is used with


// between two Complex objects
Complex operator + (Complex const &obj) {
Complex res;
[Link] = real + [Link];
[Link] = imag + [Link];
return res;
}
void print() { cout << real << " + i" << imag << endl; }
};

int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2; // An example call to "operator+"
[Link]();
}
OUTPUT:

12 + i9

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-14

AIM: WAP to illustrate Function overloading

CODE:

#include <iostream>

using namespace std;

void print(int i) {

cout << " Here is int " << i << endl;

void print(double f) {

cout << " Here is float " << f << endl;

void print(char const *c) {

cout << " Here is char* " << c << endl;

int main() {

print(10);

print(10.10);

print("ten");

return 0;

OUTPUT:

Here is int 10
Here is float 10.1
Here is char* ten

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-15

AIM: WAP to illustrate Derived classes & Inheritance

CODE:
#include <bits/stdc++.h>
using namespace std;
class Parent

public:

int id_p;

};

class Child : public Parent

public:

int id_c;

};

int main()

{
Child obj1;
obj1.id_c = 7;

obj1.id_p = 91;

cout << "Child id is " << obj1.id_c << endl;

cout << "Parent id is " << obj1.id_p << endl;

return 0;
}

OUTPUT:

Child id is 7
Parent id is 91

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-16

AIM: WAP to insert and delete and element from the Stack

CODE:

#include <iostream>

#include <stack>

using namespace std;

int main()

// Empty stack

stack<int> mystack;

[Link](0);

[Link](1);

[Link](2);

// Printing content of stack

while (![Link]()) {

cout << ' ' << [Link]();

[Link]();

OUTPUT:

2 1 0

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-17

AIM: WAP to insert and delete and element from the Queue

CODE:

#include <iostream>
#include <queue>
using namespace std;

int main()
{
// Empty Queue
queue<int> myqueue;
[Link](0);
[Link](1);
[Link](2);

// Printing content of queue


while (![Link]()) {
cout << ' ' << [Link]();
[Link]();
}
}
OUTPUT:

0 1 2

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

EXPERIMENT-18

AIM: WAP to insert and delete and element from the Linked List

CODE:
#include <bits/stdc++.h>
using namespace std;

// A linked list node


class Node
{
public:
int data;
Node *next;
};

/* Given a reference (pointer to pointer)


to the head of a list and an int, inserts
a new node on the front of the list. */
void push(Node** head_ref, int new_data)
{
/* 1. allocate node */
Node* new_node = new Node();

/* 2. put in the data */


new_node->data = new_data;

/* 3. Make next of new node as head */


new_node->next = (*head_ref);

/* 4. move the head to point to the new node */


(*head_ref) = new_node;
}

/* Given a node prev_node, insert a new node after the given


prev_node */
void insertAfter(Node* prev_node, int new_data)
{
/*1. check if the given prev_node is NULL */
if (prev_node == NULL)
{
cout<<"the given previous node cannot be NULL";
return;
}

/* 2. allocate new node */


Node* new_node = new Node();

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

/* 3. put in the data */


new_node->data = new_data;

/* 4. Make next of new node as next of prev_node */


new_node->next = prev_node->next;

/* 5. move the next of prev_node as new_node */


prev_node->next = new_node;
}

/* Given a reference (pointer to pointer) to the head


of a list and an int, appends a new node at the end */
void append(Node** head_ref, int new_data)
{
/* 1. allocate node */
Node* new_node = new Node();

Node *last = *head_ref; /* used in step 5*/

/* 2. put in the data */


new_node->data = new_data;

/* 3. This new node is going to be


the last node, so make next of
it as NULL*/
new_node->next = NULL;

/* 4. If the Linked List is empty,


then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}

/* 5. Else traverse till the last node */


while (last->next != NULL)
last = last->next;

/* 6. Change the next of last node */


last->next = new_node;
return;
}

// This function prints contents of


// linked list starting from head
void printList(Node *node)
{
while (node != NULL)
{

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by
lOMoARcPSD|69108464

BASIC COMPUTER ENGINEERING,BT-205

cout<<" "<<node->data;
node = node->next;
}
}

/* Driver code*/
int main()
{
/* Start with the empty list */
Node* head = NULL;

// Insert 6. So linked list becomes 6->NULL


append(&head, 6);

// Insert 7 at the beginning.


// So linked list becomes 7->6->NULL
push(&head, 7);

// Insert 1 at the beginning.


// So linked list becomes 1->7->6->NULL
push(&head, 1);

// Insert 4 at the end. So


// linked list becomes 1->7->6->4->NULL
append(&head, 4);

// Insert 8, after 7. So linked


// list becomes 1->7->8->6->4->NULL
insertAfter(head->next, 8);

cout<<"Created Linked list is: ";


printList(head);

return 0;
}

Output:
Created Linked list is: 1 7 8 6 4

BHOPAL INSTITUTE OF TECHNOLOGY & SCIENCE,BHOPAL

messages.downloaded_by

You might also like