0% found this document useful (0 votes)
34 views

Example Code

This code implements a singly linked list data structure in C++. It includes functions to create nodes with given data values, insert nodes into the sorted linked list, and display the sorted list. The main function takes user input to create multiple nodes, inserts them using the createnode function, and finally displays the sorted list using the display function.

Uploaded by

minichel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Example Code

This code implements a singly linked list data structure in C++. It includes functions to create nodes with given data values, insert nodes into the sorted linked list, and display the sorted list. The main function takes user input to create multiple nodes, inserts them using the createnode function, and finally displays the sorted list using the display function.

Uploaded by

minichel
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Example Code

#include<iostream>

using namespace std;

struct nod {

int d;

nod *n;

*p = NULL, *head = NULL, *q = NULL, *np = NULL;

int c = 0;

void createnode(int n) {

np = new nod;

np->d = n;

np->n = NULL;

if (c == 0) {

head = np;

p = head;

p->n = head;

c++;

} else if (c == 1) {

p = head;

q = p;

if (np->d < p->d) {

np->n = p;

head = np;

p->n = np;

} else if (np->d > p->d) {

p->n = np;

np->n = head;

c++;

} else {
p = head;

q = p;

if (np->d < p->d) {

np->n = p;

head = np;

do {

p = p->n;

while (p->n != q);

p->n = head;

} else if (np->d > p->d) {

while (p->n != head && q->d < np->d) {

q = p;

p = p->n;

if (p->n == head) {

p->n = np;

np->n = head;

} else if (np->d< p->d) {

q->n = np;

np->n = p;

break;

void display(int i) {

nod *t = head;

int c = 0;

while (c <= i ) {

cout<<t->d<<"\t";

t = t->n;
c++;

int main() {

int i = 0, n, a;

cout<<"enter the no of nodes\n";

cin>>n;

while (i < n) {

cout<<"\nenter value of node\n";

cin>>a;

createnode(a);

i++;

cout<<"sorted singly link list"<<endl;

display(n);

Output
enter the no of nodes
5
enter value of node
6
enter value of node
4
enter value of node
7
enter value of node
3
enter value of node
2
sorted singly link list
2 3 4 6 7 2

You might also like