Second, third and final year of department can be granted membership on request. Similarly one may cancel the membership of club. First node is reserved for president of club and last
node is reserved for secretary of club. Write C++ program to maintain club member‘s
information using singly linked list. Store student PRN and Name. Write functions to
a)Add and delete the members as well as president or even secretary.
b)Compute total number of members of club
c)Display members
d)Display list in reverse order using recursion
e)Two linked lists exists for two divisions. Concatenate two lists.
#include<iostream>
OUTPUT FOR THE PROGRAM |
using namespace std;
struct Node
{
int Roll;
char Name[10];
struct Node *Next;
}*head,*head2;
void create_SLL()
{
int i, nodes;
struct Node *temp, *prev;
head = NULL;
cout<<"\n\n How many nodes: ";
cin>>nodes;
for(i=0; i<nodes; i++)
{
temp = new(struct Node); //Step 1: Allocate Memory
cout<<"\n\t Enter Roll No: "; //Step 2: Store data and address
cin>>temp->Roll;
cout<<"\n\t Enter Name: ";
cin>>temp->Name;
temp->Next = NULL;
if(head == NULL) //Step 3: Attach node in linked List
{
head = temp;
prev = head;
}
else
{
prev->Next = temp;
prev = prev->Next;
}
}
}
void display_SLL()
{
struct Node *p;
p = head;
cout<<"\n\n Head";
while(p != NULL)
{
cout<<"---->|"<<p->Roll<<"|"<<p->Name<<"|";
p = p->Next;
}
cout<<" ---->NULL";
}
void count_mem()
{
int cnt = 0;
struct Node *p;
p = head;
while(p!=NULL)
{
cnt ++;
p=p->Next;
}
cout<<"\n No of members : "<<cnt;
}
void rev_SLL()
{
struct Node *p, *temp;
p = head;
head2 = NULL;
while(p != NULL)
{
temp = new(struct Node);
temp->Roll=p->Roll;
strcpy(temp->Name,p->Name);
temp->Next = NULL;
if(head2==NULL)
head2=temp;
else
{
temp->Next = head2;
head2 = temp;
}
p=p->Next;
}
p=head2;
cout<<"\n\n Head";
while(p != NULL)
{
cout<<"---->|"<<p->Roll<<"|"<<p->Name<<"|";
p = p->Next;
}
cout<<" ---->NULL";
}
void join_SLL()
{
struct Node *p;
p = head;
while(p->Next!=NULL)
p = p->Next;
p->Next = head2;
display_SLL();
}
void delete_SLL()
{
struct Node *p, *q;
int rno;
cout<<"Enter roll no. to delete";
cin>>rno;
p = q = head;
while(p != NULL)
{
if(p->Roll == rno)
{
if(p == head)
{
head = head->Next;
delete p;
}
else
{
q->Next = p->Next;
delete p;
}
break;
}
else
{
q = p;
p = p->Next;
}
}
if(p == NULL)
cout<<"\n\n\t Not Found.....!!!";
}
int main()
{
cout<<"\n\n ---------*** Operations on Single Linked List ***---------";
cout<<"\n\n ---------1. Create SLL";
create_SLL();
cout<<"\n\n ---------2. Display SLL";
display_SLL();
cout<<"\n\n ---------3. Count SLL";
count_mem();
cout<<"\n\n ---------4. Delete SLL";
delete_SLL();
display_SLL();
cout<<"\n\n";
cout<<"\n\n ---------5. Reverse SLL";
rev_SLL();
cout<<"\n\n ---------6. Join SLL";
join_SLL();
cout<<"\n\n";
return 0;
}
Tags
CODING