From 9a79625cc37a8f2766fa34e75d3f11cd279a80f5 Mon Sep 17 00:00:00 2001 From: sharma-p Date: Mon, 1 Oct 2018 01:33:34 +0530 Subject: [PATCH] Created LinkeList.cpp Written a code in cC+ to create a linked list. --- CPP/data-structures/LinkedList.cpp | 53 ++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 CPP/data-structures/LinkedList.cpp diff --git a/CPP/data-structures/LinkedList.cpp b/CPP/data-structures/LinkedList.cpp new file mode 100644 index 0000000..6733474 --- /dev/null +++ b/CPP/data-structures/LinkedList.cpp @@ -0,0 +1,53 @@ +#include +#include +#include +using namespace std; +struct Node { + int info; + Node * next; +} *start, *ptr, *newptr, *save, *rear; +Node * NewNode(int); +void Insert(Node *); +void Traverse(Node* ); +int main(){ + start=rear=NULL; + int inf; + char ch='Y'; + while(ch=='Y'||ch=='y'){ + cout<<"Enter the information in new node\n"; + cin>>inf; + newptr=NewNode(inf); + if(newptr==NULL) { + cout<<"Can not create node\n"; + exit(1); + } + Insert(newptr); + cout<<"The list is\n"; + Traverse(start); + cout<<"Press Y to enter more node else any key to exit\n"; + cin>>ch; + } + return 0; +} + Node * NewNode(int n) { + ptr=new Node; + ptr->info=n; + ptr->next=NULL; + return ptr; + } + void Insert(Node * np) { + if(start==NULL) { + start=rear=np; + } + else { + rear->next=np; + rear=np; + } + } + void Traverse(Node *np) { + while(np!=NULL) { + cout<info<<"->"; + np=np->next; + } + cout<<"\n"; +}