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

Class 8 and Class 9 Demo Programs

Uploaded by

Manik B Pujari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Class 8 and Class 9 Demo Programs

Uploaded by

Manik B Pujari
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CLASS 8 AND CLASS 9 – SINGLY LINKED LIST DEMO PROGRAMS

//BASIC OPEARTIONS ON SINGLY LINKED LIST.

#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.
};

typedef struct candidate *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;
}

NODE insert_end(NODE head)


{
NODE newn=NULL, cur=NULL;
newn = read_details();
if(head == NULL)
{
return newn;
}
else
{
cur=head;
while(cur->next != NULL)
cur = cur->next;
cur->next = newn;
}
return head;
}

void display_list(NODE head)


{
NODE cur=NULL;
if(head == NULL)
{
printf("Candidate List is Empty\n");
}
else
{
cur=head;
printf("candidate details\n");
printf("Name Rank Age Address\n");
printf("head-> ");
while(cur != NULL)
{
printf("%s %d %d %s -> ", cur->name, cur->rank, cur->age, cur->addr);
cur = cur->next;
}
printf("NULL\n");
}
}

void count_nodes(NODE head)


{
NODE cur=NULL;
int count=0;
if(head == NULL)
{
printf("Candidate List is Empty\n");
}
else
{
cur=head;
while(cur != NULL)
{
count++;
cur = cur->next;
}
printf("\nCount of nodes = %d\n",count);
}
}

NODE insert_front(NODE head)


{
NODE newn;
newn = read_details();
if(head == NULL)
{
return newn;
}
newn->next = head;
head = newn;
return head;
}

NODE insert_before(NODE head)


{
char nm[20];
NODE cur = head, prev = NULL;
NODE newn = read_details();
if(head == NULL)
{
return newn;
}
printf("Enter name of candidate before which you need to enter the details\n");
scanf("%s", nm);
while (cur -> next != NULL)
{
if(strcmp(cur->name, nm)==0)
{
break;
}
else
{
prev = cur;
cur = cur->next;
}
}
prev->next = newn;
newn->next = cur;

return head;
}

NODE delete_end(NODE head)


{
NODE prev = NULL, cur = head;
if(head == NULL)
{
printf("Candidate List is Empty\n");
}
if(head->next == NULL)
{
printf("Deleted = %s\n",head->name);
free(head);
return NULL;
}
while(cur-> next != NULL)
{
prev = cur;
cur = cur->next;
}
printf("Deleted = %s\n",cur->name);
prev->next = NULL;
free(cur);

return head;
}

NODE delete_front(NODE head)


{
NODE cur=head;
if(head == NULL)
{
printf("Candidate List is Empty\n");
}
head = head->next;
printf("Deleted = %s\n", cur->name);
cur->next = NULL;
free(cur);

return head;
}

NODE delete_specific(NODE head)


{
NODE prev=NULL, cur=head;
char nm[20];
printf("Enter name of student whose details has to be deleted?\n");
scanf("%s",nm);
if(head == NULL)
{
printf("Candidates List is Empty\n");
}
if(head->next == NULL && strcmp(head->name, nm)==0)
{
printf("Deleted candidate = %s\n", head->name);
free(head);
return NULL;
}
if(strcmp(head->name, nm)==0)
{
cur=head;
head = head->next;
free(cur);
return head;
}
while(cur->next != NULL)
{
if(strcmp(cur->name, nm)==0)
{
break;
}
else
{
prev= cur;
cur= cur->next;
}
}
prev->next = cur->next;
printf("Deleted Candidate = %s\n", cur->name);
free(cur);
return head;
}

NODE reverse_list(NODE head)


{
NODE next_node, cur=head, prev=NULL;
while(cur !=NULL)
{
next_node = cur->next;
cur->next = prev; // reverse connect the link.
prev = cur;
cur = next_node;
}
return prev;
}

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;
};

typedef struct node *N1;


N1 start1 = NULL, start2 = NULL, start3 = NULL;

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;

NODE even = NULL;


NODE odd = NULL;
NODE list = NULL;

//Create Linked List


void insert(int data)
{
// Allocate memory for new node;
NODE newn = (NODE) malloc(sizeof(struct node));
NODE current;

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 display(NODE head)


{
NODE ptr = head;
if(head==NULL)
{
printf("List is Empty\n");
}
else
{
printf("head -> ");
//start from the beginning
while(ptr != NULL)
{
printf(" %d -> ",ptr->data);
ptr = ptr->next;
}
printf(" NULL\n");
}
}

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);
}

printf("Complete list: \n");


display(list);

split_list();

printf("\nOdd : \n");
display(odd);

printf("Even : \n");
display(even);

return 0;
}

You might also like