diff --git a/C Programming Language/Linked Lists/Singly Linked lists/singlylinkedlist.cpp b/C Programming Language/Linked Lists/Singly Linked lists/singlylinkedlist.cpp new file mode 100644 index 0000000..9104dda --- /dev/null +++ b/C Programming Language/Linked Lists/Singly Linked lists/singlylinkedlist.cpp @@ -0,0 +1,335 @@ +//Program for singly linked list + +#include +using namespace std; +#include + +class node +{ +friend class list; +int rollno; +char name[10]; +int year; +node *next; + +public: +node(int d,int r,char s[10]) +{ + rollno=d; + year=r; + strcpy(name,s); + next=NULL; +} + +}; + +class list +{ + node *head; + + public: + list() + { + head=NULL; + } + void createmember() + { + int r,y; + char a; + char nm[10]; + node *temp,*ptr; + + do + { + + cout<<"Enter roll no, year and name: "<>r>>y>>nm; + + temp= new node(r,y,nm); + + if(head==NULL) + { + head=temp; + cout<<"Node is inserted"<next!=NULL) + { + ptr=ptr->next; + } + ptr->next=temp; + cout<<"Node is inserted"<>a; + }while(a=='y'||a=='Y'); + + } + + void display() + { + node *ptr; + + if(head==NULL) + { + cout<<"List is empty"<rollno<year<name<next; + } + } + } + int count() + { + node *ptr; + int count=0; + + if(head==NULL) + { + return 0; + } + else + { + ptr=head; + while(ptr!=NULL) + { + count++; + ptr=ptr->next; + } + return count; + } + } + void insert() + { + int r,y,i,pos; + char nm[10]; + node *temp,*ptr; + + cout<<"Enter roll no, year and name: "<>r>>y>>nm; + temp= new node(r,y,nm); + + cout<<"Enter the position you want to insert: "<>pos; + + if(pos==1) + { + temp->next=head; + head=temp; + cout<<"Node is inserted"<next; + } + temp->next=ptr->next; + ptr->next=temp; + cout<<"Node is inserted"<next!=NULL) +// ptr=ptr->next; +// ptr->next=temp; +// } + + } + void deletemember() + { + int data; + node *ptr,*prev; + int flag=0; + ptr=head; + + if(head==NULL) + { + cout<<"List is empty!"<>data; + if(ptr->rollno==data) + { + head=head->next; + delete ptr; + flag=1; + } + else + { + while((ptr!=NULL)&&(ptr->rollno!=data)) + { + prev=ptr; + ptr=ptr->next; + } + if(ptr==NULL) + flag=0; + else + { + prev->next=ptr->next; + ptr->next=NULL; + delete ptr; + flag=1; + } + } + if(flag==0) + cout<<"Roll no. not present!"<>ch; + switch(ch) + { + case 1: + l1.display(); + break; + + case 2: + cout<<"Total number of nodes: "<