Class 8 and Class 9 Demo Programs
Class 8 and Class 9 Demo Programs
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct candidate
{
char name [25];
int rank, age;
char addr[20];
struct candidate *next; // link part to store the address of next node.
};
NODE getnode()
{
NODE newn;
newn=(NODE)malloc (sizeof (struct candidate));
if(newn==NULL)
{
printf("Not created\n");
exit(0);
}
newn->next = NULL;
return newn;
}
NODE read_details()
{
NODE temp;
temp = getnode();
printf("Enter name, rank, age, address\n");
scanf("%s %d %d %s",temp->name,&temp->rank,&temp->age,temp->addr);
return temp;
}
return head;
}
return head;
}
return head;
}
int main()
{
NODE head=NULL;
int ch,cnt;
while(1)
{
printf("Menu -- 1. Insert End. 2. Insert Front 3. Insert before 4. Delete End 5. Delete Front 6.
Delete Specific ");
printf("7. Reverse List 8. Count 9. Display. 10. Exit.\nEnter choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: head = insert_end(head);
break;
case 2: head = insert_front(head);
break;
case 3: head=insert_before(head);
break;
case 4: head = delete_end(head);
break;
case 5: head=delete_front(head);
break;
case 6: head = delete_specific(head);
break;
case 7: head = reverse_list(head);
break;
case 8: count_nodes(head);
break;
case 9: display_list(head);
break;
case 10: exit(0);
break;
default: printf("Invalid choice\n");
}
}
return 0;
}
//MAKING SINGLY LINKED LIST OF ALTERNATE NODES.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int num;
struct node *next;
};
void insert()
{
N1 temp = (N1)malloc(sizeof(struct node));
printf("Enter number = ");
scanf("%d", &temp->num);
temp -> next = start1;
start1 = temp;
}
void alternate()
{
while(start1 != NULL)
{
N1 temp1 = (N1)malloc(sizeof(struct node));
temp1 ->num = start1 ->num;
temp1->next = start2;
start2 = temp1;
start1 = start1 -> next;
if(start1 != NULL)
{
N1 temp2 = (N1)malloc(sizeof(struct node));
temp2 ->num = start1 ->num;
temp2->next = start3;
start3 = temp2;
start1 = start1 -> next;
}
}
}
int main()
{
int n;
printf("Enter number of nodes:\n");
scanf("%d", &n);
while(n--)
insert();
alternate();
N1 temp = start2;
while(temp != NULL)
{
printf("%d ", temp ->num);
temp = temp ->next;
}
printf("\n");
temp = start3;
while(temp != NULL)
{
printf("%d ", temp ->num);
temp = temp ->next;
}
return 0;
}
// MAKING NEW SINGLY LINKED LISTS WITH RESPECT TO ODD DATA AND EVEN DATA.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node *NODE;
newn->data = data;
newn->next = NULL;
if(list == NULL)
{
list = newn;
return;
}
current = list;
while(current->next!=NULL)
current = current->next;
// Insert link at the end of the list
current->next = newn;
}
void split_list() {
// Allocate memory for new node;
NODE link;
NODE current;
while(list != NULL)
{
NODE link = (NODE) malloc(sizeof(struct node));
link->data = list->data;
link->next = NULL;
if(list->data%2 == 0)
{
if(even == NULL)
{
even = link;
list = list->next;
continue;
}
else
{
current = even;
while(current->next != NULL)
current = current->next;
// Insert link at the end of the list
current->next = link;
}
list = list->next;
}
else
{
if(odd == NULL)
{
odd = link;
list = list->next;
continue;
}
else
{
current = odd;
while(current->next!=NULL)
current = current->next;
// Insert link at the end of the list
current->next = link;
}
list = list->next;
}
}
}
int main()
{
int i,n, num;
printf("enter number of nodes to be inserted");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
printf("Enter number: ");
scanf("%d",&num);
insert(num);
}
split_list();
printf("\nOdd : \n");
display(odd);
printf("Even : \n");
display(even);
return 0;
}