SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Downloaden Sie, um offline zu lesen
/***** @author: Er. Ganesh Ram Suwal *****/
/***** Singly Linked List *****/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
//Global Variables
int data, position;
//Structure for Node
struct node
{
int info;
struct node *pnext;
};
struct node *pnew,*pfirst,*pthis,*ptemp;
//Function Prototype
void nifb();
void nibxp();
void niaxp();
void nife();
void ndfb();
void ndfsp();
void ndfe();
void display();
//Count the nodes of Linked List
int length()
{
int count = 0;
if(pfirst == NULL)
{
return 0;
}
else
{
pthis = pfirst;
count = 1;
while(pthis->pnext != NULL)
{
pthis = pthis->pnext;
count = count + 1;
}
return count;
}
}
void newNode()
{
pnew = (struct node*)malloc(sizeof(struct node));
printf("Data : ");
scanf("%d",&data);
pnew->info = data;
}
void invalidPosition()
{
printf("n********************************************************n");
printf(" Invalid Position");
printf("n********************************************************n");
}
void noNodeInLinkedList()
{
printf("n********************************************************n");
printf(" Sorry There is no Node In Linked List");
printf("n********************************************************n");
}
// Main function part
void main()
{
clrscr();
int choice;
start:
printf("n******* Singly Linked List ****************n");
printf("1: Node insert from the beginingn");
printf("2: Node insert from the endn");
printf("3: Node insert before Xth Positionn");
printf("4: Node insert after Xth Positionn");
printf("5: Node delete from the beginingn");
printf("6: Node delete from the endn");
printf("7: Node delete from the specified Positionn");
printf("8: Displayn");
printf("9: Exitn");
printf("Enter your choice :");
scanf("%d",&choice);
switch(choice)
{
case 1:nifb();
break;
case 2:nife();
break;
case 3:nibxp();
break;
case 4:niaxp();
break;
case 5:ndfb();
break;
case 6:ndfe();
break;
case 7:ndfsp();
break;
case 8:display();
break;
case 9:exit(0);
break;
default:printf("Invalid Choice");
break;
}
goto start;
}
// Node Insertion from the begining
void nifb()
{
newNode();
if(pfirst == NULL)
{
pnew->pnext = NULL;
pfirst = pnew;
}
else
{
pnew->pnext = pfirst;
pfirst = pnew;
}
}
//node insertion before Xth Position
void nibxp()
{
printf("Position : ");
scanf("%d",&position);
if(position > length() || position < 1)
{
invalidPosition();
}
else if(position == 1)
{
nifb();
}
else
{
newNode();
pthis = pfirst;
for(int i = 0; i < position-2; i++)
{
pthis = pthis->pnext;
}
ptemp = pthis->pnext;
pthis->pnext = pnew;
pnew->pnext = ptemp;
}
}
//Node Insertion After Xth Position
void niaxp()
{
printf("Position : ");
scanf("%d",&position);
if(position > length() || position < 1)
{
invalidPosition();
}
else if(position == length())
{
nife();
}
else
{
newNode();
pthis = pfirst;
for(int i = 0; i < position-1; i++)
{
pthis = pthis->pnext;
}
ptemp = pthis->pnext;
pthis->pnext = pnew;
pnew->pnext = ptemp;
}
}
// Node Insertion from the End
void nife()
{
newNode();
pnew->pnext = NULL;
if(pfirst == NULL)
{
pfirst = pnew;
}
else
{
pthis = pfirst;
while(pthis->pnext != NULL)
{
pthis = pthis->pnext;
}
pthis->pnext = pnew;
}
}
//Node deletion from the begining
void ndfb()
{
if(pfirst == NULL)
{
noNodeInLinkedList();
}
else
{
pthis = pfirst;
pfirst = pthis->pnext;
printf("n********************************************************n");
printf("The deleted Node is : %d",pthis->info);
printf("n********************************************************n");
free(pthis);
}
}
//Node deletion from the specified position
void ndfsp()
{
if(pfirst == NULL)
{
noNodeInLinkedList();
}
else
{
printf("Position :");
scanf("%d",&position);
if(position > length() || position < 1)
{
invalidPosition();
}
else if(position == 1)
{
ndfb();
}
else if(position == length())
{
ndfe();
}
else
{
pthis = pfirst;
for(int i = 0; i<position-2;i++)
{
pthis = pthis->pnext;
}
ptemp = pthis->pnext->pnext;
printf("n********************************************************n");
printf("The deleted Node is : %d",pthis->pnext->info);
printf("n********************************************************n");
free(pthis->pnext);
pthis->pnext = ptemp;
}
}
}
//Node deletion from the end
void ndfe()
{
if(pfirst == NULL)
{
noNodeInLinkedList();
}
else
{
pthis = pfirst;
if(pfirst->pnext == NULL)
{
printf("n********************************************************n");
printf("The deleted Node is : %d",pthis->info);
printf("n********************************************************n");
free(pthis);
pfirst = NULL;
}
else
{
while(pthis->pnext->pnext != NULL)
{
pthis = pthis->pnext;
}
printf("n********************************************************n");
printf("The deleted Node is : %d",pthis->pnext->info);
printf("n********************************************************n");
free(pthis->pnext);
pthis->pnext = NULL;
}
}
}
// Didsplay the Node of a Linked List
void display()
{
int length1;
length1 = length();
if(pfirst == NULL)
{
noNodeInLinkedList();
}
else
{
printf("n********************************************************n");
printf("data of SLL (length=%d): ",length1);
pthis = pfirst;
printf("%d ",pthis->info);
while(pthis->pnext != NULL)
{
pthis = pthis->pnext;
printf("%d ",pthis->info);
}
printf("n********************************************************n");
}
}
Singly Linked List
Singly Linked List

Weitere ähnliche Inhalte

Was ist angesagt?

Pointers and Array, pointer and String.pptx
Pointers and Array, pointer and String.pptxPointers and Array, pointer and String.pptx
Pointers and Array, pointer and String.pptx
Ananthi Palanisamy
 

Was ist angesagt? (20)

Add an interactive command line to your C++ application
Add an interactive command line to your C++ applicationAdd an interactive command line to your C++ application
Add an interactive command line to your C++ application
 
Graph.pptx
Graph.pptxGraph.pptx
Graph.pptx
 
11. Java Objects and classes
11. Java  Objects and classes11. Java  Objects and classes
11. Java Objects and classes
 
computer project code ''payroll'' (based on datafile handling)
computer project code ''payroll'' (based on datafile handling)computer project code ''payroll'' (based on datafile handling)
computer project code ''payroll'' (based on datafile handling)
 
Pointers and Array, pointer and String.pptx
Pointers and Array, pointer and String.pptxPointers and Array, pointer and String.pptx
Pointers and Array, pointer and String.pptx
 
2- Dimensional Arrays
2- Dimensional Arrays2- Dimensional Arrays
2- Dimensional Arrays
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
 
Rust Tutorial | Rust Programming Language Tutorial For Beginners | Rust Train...
Rust Tutorial | Rust Programming Language Tutorial For Beginners | Rust Train...Rust Tutorial | Rust Programming Language Tutorial For Beginners | Rust Train...
Rust Tutorial | Rust Programming Language Tutorial For Beginners | Rust Train...
 
Threaded binary tree
Threaded binary treeThreaded binary tree
Threaded binary tree
 
Processes
ProcessesProcesses
Processes
 
Void pointer in c
Void pointer in cVoid pointer in c
Void pointer in c
 
PHP Cookies and Sessions
PHP Cookies and SessionsPHP Cookies and Sessions
PHP Cookies and Sessions
 
binary tree
binary treebinary tree
binary tree
 
Linked lists
Linked listsLinked lists
Linked lists
 
Computer graphics lab manual
Computer graphics lab manualComputer graphics lab manual
Computer graphics lab manual
 
Pointers in c language
Pointers in c languagePointers in c language
Pointers in c language
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
Sorting Methods.pptx
Sorting Methods.pptxSorting Methods.pptx
Sorting Methods.pptx
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 

Ähnlich wie Singly Linked List

C++ Program to Implement Singly Linked List #includei.pdf
  C++ Program to Implement Singly Linked List  #includei.pdf  C++ Program to Implement Singly Linked List  #includei.pdf
C++ Program to Implement Singly Linked List #includei.pdf
anupambedcovers
 
using set identitiesSolutionimport java.util.Scanner; c.pdf
using set identitiesSolutionimport java.util.Scanner;  c.pdfusing set identitiesSolutionimport java.util.Scanner;  c.pdf
using set identitiesSolutionimport java.util.Scanner; c.pdf
excellentmobilesabc
 
In C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdfIn C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf
fantoosh1
 
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdfAnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
anwarsadath111
 
C++ Program to Implement Singly Linked List #includeiostream.pdf
 C++ Program to Implement Singly Linked List #includeiostream.pdf C++ Program to Implement Singly Linked List #includeiostream.pdf
C++ Program to Implement Singly Linked List #includeiostream.pdf
angelsfashion1
 
I have to write a polynomial class linked list program and i do not .pdf
I have to write a polynomial class linked list program and i do not .pdfI have to write a polynomial class linked list program and i do not .pdf
I have to write a polynomial class linked list program and i do not .pdf
jibinsh
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
angelsfashion1
 
CODE#include stdlib.h #include stdio.hstruct task{proce.pdf
CODE#include stdlib.h #include stdio.hstruct task{proce.pdfCODE#include stdlib.h #include stdio.hstruct task{proce.pdf
CODE#include stdlib.h #include stdio.hstruct task{proce.pdf
fathimalinks
 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdf
amarndsons
 
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
odiliagilby
 
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdfincludestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
galagirishp
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
arrowmobile
 
#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf
ankitmobileshop235
 
Please correct my errors upvote Clears our entire .pdf
Please correct my errors upvote      Clears our entire .pdfPlease correct my errors upvote      Clears our entire .pdf
Please correct my errors upvote Clears our entire .pdf
kitty811
 

Ähnlich wie Singly Linked List (20)

C++ Program to Implement Singly Linked List #includei.pdf
  C++ Program to Implement Singly Linked List  #includei.pdf  C++ Program to Implement Singly Linked List  #includei.pdf
C++ Program to Implement Singly Linked List #includei.pdf
 
using set identitiesSolutionimport java.util.Scanner; c.pdf
using set identitiesSolutionimport java.util.Scanner;  c.pdfusing set identitiesSolutionimport java.util.Scanner;  c.pdf
using set identitiesSolutionimport java.util.Scanner; c.pdf
 
In C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdfIn C++ I need help with this method that Im trying to write fillLi.pdf
In C++ I need help with this method that Im trying to write fillLi.pdf
 
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdfAnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
AnswerNote LinkedList.cpp is written and driver program main.cpp.pdf
 
C++ Program to Implement Singly Linked List #includeiostream.pdf
 C++ Program to Implement Singly Linked List #includeiostream.pdf C++ Program to Implement Singly Linked List #includeiostream.pdf
C++ Program to Implement Singly Linked List #includeiostream.pdf
 
I have to write a polynomial class linked list program and i do not .pdf
I have to write a polynomial class linked list program and i do not .pdfI have to write a polynomial class linked list program and i do not .pdf
I have to write a polynomial class linked list program and i do not .pdf
 
Singly linked list.pptx
Singly linked list.pptxSingly linked list.pptx
Singly linked list.pptx
 
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf #ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
#ifndef LINKED_LIST_ #define LINKED_LIST_ templateclass It.pdf
 
CODE#include stdlib.h #include stdio.hstruct task{proce.pdf
CODE#include stdlib.h #include stdio.hstruct task{proce.pdfCODE#include stdlib.h #include stdio.hstruct task{proce.pdf
CODE#include stdlib.h #include stdio.hstruct task{proce.pdf
 
Please teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdfPlease teach me how to fix the errors and where should be modified. .pdf
Please teach me how to fix the errors and where should be modified. .pdf
 
Lab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Lab Week 2 Game Programming.docx
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
 
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
__MACOSX._assign3assign3.DS_Store__MACOSXassign3._.D.docx
 
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdfincludestdio.h #includestdlib.h int enqueue(struct node ,.pdf
includestdio.h #includestdlib.h int enqueue(struct node ,.pdf
 
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdfHere is the editable codeSolutionimport java.util.NoSuchEleme.pdf
Here is the editable codeSolutionimport java.util.NoSuchEleme.pdf
 
#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf#includeiostream struct node {    char value;    struct no.pdf
#includeiostream struct node {    char value;    struct no.pdf
 
Please correct my errors upvote Clears our entire .pdf
Please correct my errors upvote      Clears our entire .pdfPlease correct my errors upvote      Clears our entire .pdf
Please correct my errors upvote Clears our entire .pdf
 
Binary Tree in C++ coding in the data structure
Binary Tree in C++ coding in the data structureBinary Tree in C++ coding in the data structure
Binary Tree in C++ coding in the data structure
 
Change the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdfChange the driver file (the main .cpp) so that it asks the user to e.pdf
Change the driver file (the main .cpp) so that it asks the user to e.pdf
 
week-13x
week-13xweek-13x
week-13x
 

Mehr von Er. Ganesh Ram Suwal (9)

Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
Tower of HANOI
Tower of HANOITower of HANOI
Tower of HANOI
 
STACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LISTSTACK IMPLEMENTATION USING SINGLY LINKED LIST
STACK IMPLEMENTATION USING SINGLY LINKED LIST
 
Stack Data Structure
Stack Data StructureStack Data Structure
Stack Data Structure
 
Circular queue
Circular queueCircular queue
Circular queue
 
Linear queue
Linear queueLinear queue
Linear queue
 
Proposal Writing
Proposal WritingProposal Writing
Proposal Writing
 
DSA chapter 1
DSA chapter 1DSA chapter 1
DSA chapter 1
 
Web Technology And Its Scope in NEPAL.pptx
Web Technology And Its Scope in NEPAL.pptxWeb Technology And Its Scope in NEPAL.pptx
Web Technology And Its Scope in NEPAL.pptx
 

Kürzlich hochgeladen

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 

Kürzlich hochgeladen (20)

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 

Singly Linked List

  • 1. /***** @author: Er. Ganesh Ram Suwal *****/ /***** Singly Linked List *****/ #include<stdio.h> #include<conio.h> #include<alloc.h> #include<process.h> //Global Variables int data, position; //Structure for Node struct node { int info; struct node *pnext; }; struct node *pnew,*pfirst,*pthis,*ptemp; //Function Prototype void nifb(); void nibxp(); void niaxp(); void nife(); void ndfb(); void ndfsp(); void ndfe(); void display(); //Count the nodes of Linked List int length() { int count = 0; if(pfirst == NULL) { return 0; } else { pthis = pfirst; count = 1; while(pthis->pnext != NULL) { pthis = pthis->pnext; count = count + 1; } return count; } } void newNode() { pnew = (struct node*)malloc(sizeof(struct node)); printf("Data : "); scanf("%d",&data); pnew->info = data; }
  • 2. void invalidPosition() { printf("n********************************************************n"); printf(" Invalid Position"); printf("n********************************************************n"); } void noNodeInLinkedList() { printf("n********************************************************n"); printf(" Sorry There is no Node In Linked List"); printf("n********************************************************n"); } // Main function part void main() { clrscr(); int choice; start: printf("n******* Singly Linked List ****************n"); printf("1: Node insert from the beginingn"); printf("2: Node insert from the endn"); printf("3: Node insert before Xth Positionn"); printf("4: Node insert after Xth Positionn"); printf("5: Node delete from the beginingn"); printf("6: Node delete from the endn"); printf("7: Node delete from the specified Positionn"); printf("8: Displayn"); printf("9: Exitn"); printf("Enter your choice :"); scanf("%d",&choice); switch(choice) { case 1:nifb(); break; case 2:nife(); break; case 3:nibxp(); break; case 4:niaxp(); break; case 5:ndfb(); break; case 6:ndfe(); break; case 7:ndfsp(); break; case 8:display(); break; case 9:exit(0); break; default:printf("Invalid Choice"); break; } goto start;
  • 3. } // Node Insertion from the begining void nifb() { newNode(); if(pfirst == NULL) { pnew->pnext = NULL; pfirst = pnew; } else { pnew->pnext = pfirst; pfirst = pnew; } } //node insertion before Xth Position void nibxp() { printf("Position : "); scanf("%d",&position); if(position > length() || position < 1) { invalidPosition(); } else if(position == 1) { nifb(); } else { newNode(); pthis = pfirst; for(int i = 0; i < position-2; i++) { pthis = pthis->pnext; } ptemp = pthis->pnext; pthis->pnext = pnew; pnew->pnext = ptemp; } } //Node Insertion After Xth Position void niaxp() { printf("Position : "); scanf("%d",&position); if(position > length() || position < 1) { invalidPosition(); } else if(position == length()) { nife();
  • 4. } else { newNode(); pthis = pfirst; for(int i = 0; i < position-1; i++) { pthis = pthis->pnext; } ptemp = pthis->pnext; pthis->pnext = pnew; pnew->pnext = ptemp; } } // Node Insertion from the End void nife() { newNode(); pnew->pnext = NULL; if(pfirst == NULL) { pfirst = pnew; } else { pthis = pfirst; while(pthis->pnext != NULL) { pthis = pthis->pnext; } pthis->pnext = pnew; } } //Node deletion from the begining void ndfb() { if(pfirst == NULL) { noNodeInLinkedList(); } else { pthis = pfirst; pfirst = pthis->pnext; printf("n********************************************************n"); printf("The deleted Node is : %d",pthis->info); printf("n********************************************************n"); free(pthis); } } //Node deletion from the specified position void ndfsp() { if(pfirst == NULL)
  • 5. { noNodeInLinkedList(); } else { printf("Position :"); scanf("%d",&position); if(position > length() || position < 1) { invalidPosition(); } else if(position == 1) { ndfb(); } else if(position == length()) { ndfe(); } else { pthis = pfirst; for(int i = 0; i<position-2;i++) { pthis = pthis->pnext; } ptemp = pthis->pnext->pnext; printf("n********************************************************n"); printf("The deleted Node is : %d",pthis->pnext->info); printf("n********************************************************n"); free(pthis->pnext); pthis->pnext = ptemp; } } } //Node deletion from the end void ndfe() { if(pfirst == NULL) { noNodeInLinkedList(); } else { pthis = pfirst; if(pfirst->pnext == NULL) { printf("n********************************************************n"); printf("The deleted Node is : %d",pthis->info); printf("n********************************************************n"); free(pthis); pfirst = NULL; } else { while(pthis->pnext->pnext != NULL)
  • 6. { pthis = pthis->pnext; } printf("n********************************************************n"); printf("The deleted Node is : %d",pthis->pnext->info); printf("n********************************************************n"); free(pthis->pnext); pthis->pnext = NULL; } } } // Didsplay the Node of a Linked List void display() { int length1; length1 = length(); if(pfirst == NULL) { noNodeInLinkedList(); } else { printf("n********************************************************n"); printf("data of SLL (length=%d): ",length1); pthis = pfirst; printf("%d ",pthis->info); while(pthis->pnext != NULL) { pthis = pthis->pnext; printf("%d ",pthis->info); } printf("n********************************************************n"); } }