0% found this document useful (0 votes)
32 views11 pages

WEEK14: Linked List

This document contains code for linked list operations: 1. It defines a node struct with data and next pointer fields. 2. Functions are created to insert at front and end, delete at front and end, display, and search a linked list. 3. The main function contains a menu to call these functions and manipulate a linked list pointed to by the head pointer.

Uploaded by

VARUN KUMAR KS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views11 pages

WEEK14: Linked List

This document contains code for linked list operations: 1. It defines a node struct with data and next pointer fields. 2. Functions are created to insert at front and end, delete at front and end, display, and search a linked list. 3. The main function contains a menu to call these functions and manipulate a linked list pointed to by the head pointer.

Uploaded by

VARUN KUMAR KS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

WEEK14

Question 1
Linked List
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * next;
};
void disp(struct node * head){
while (head != NULL) {
printf("%d--->", head->data);
head = head->next; }
printf("NULL\n"); }
void insertAtFront(struct node ** head){
struct node * temp = (struct node *)malloc(sizeof(struct
node));
printf("Enter data: ");
scanf("%d", &temp->data);
if(*head==NULL){
*head = temp;
return; }
temp->next = *head;
*head = temp; }
void insertAtEnd(struct node ** head){
struct node * temp = (struct node *)malloc(sizeof(struct
node));
printf("Enter data: ");
scanf("%d", &temp->data);
if(*head == NULL){ *head =temp} ;
return;
struct node * tail = *head;
while(tail->next!=NULL){ tail = tail->next;
}
tail->next = temp;
temp->next = NULL;
}
void sum(struct node * head){
int sum = 0;
while (head != NULL) {
sum+=head->data; head=head->next;
}
printf("The sum is %d\n", sum); }
int altsum(struct node * head){
int sum =0;
for(int i =0;head != NULL;i++) {
if(i%2==0)
sum+=head->data;
head=head->next;
}
return sum;
}
void oddeven(struct node * head){
printf("The sum of even entries is %d and odd entries is
%d\n",altsum(head),altsum(head->next)); }
int main(){
int choice = 1;
struct node * head = NULL;
while (choice != 7) {
printf("\n*********Menu************\n");
printf( "1.Insert at front\n"
"2.Insert at end\n"
"3.Sum of all\n"
"4.Sum of alternate\n"
"5.Sum of odd, even\n"
"6.Display\n"
"7.Exit\n");
scanf("%d", &choice); switch (choice) {
case 1:insertAtFront(&head);break;
case 2:insertAtEnd(&head);break;
case 3:sum(head);break;
case 4:printf("The sum is
%d\n",altsum(head));break;
case 5:oddeven(head);break;
case 6:disp(head); break; }}
return 0; }
OUTPUT
Question 2
1) Create a linked list. Write functions for the
following. i)delete at the front
ii)delete at the end
iii)display
iv)Search an element
ii)Product of the nodes of a linked list which are
divisible by a given number.

#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node * next;
};
void disp(struct node * head){ while (head != NULL) {
printf("%d--->", head->data);
head = head->next; }
printf("NULL\n"); }
void insertAtFront(struct node ** head){
struct node * temp = (struct node *)malloc(sizeof(struct
node)); printf("Enter data: ");
scanf("%d", &temp->data);
if(*head==NULL){
*head = temp;
return; }
temp->next = *head;
*head = temp; }
void insertAtEnd(struct node ** head){
struct node * temp = (struct node *)malloc(sizeof(struct
node));
printf("Enter data: ");
scanf("%d",&temp->data);
if(*head == NULL){*head =temp; return;
}
struct node * tail = *head; while(tail->next!=NULL){
tail = tail->next; }
tail->next = temp;
temp->next = NULL; }
void delFront(struct node ** head){ *head = (*head)-
>next;
}
void delEnd(struct node ** head){ if((*head)->next ==
NULL){ *head = (*head)->next;
return; }
struct node * tail = *head; while(tail->next != NULL){
if(tail->next->next==NULL) { tail->next = NULL;
return;
}
tail = tail->next; }
}
void search(struct node * head){ printf("Enter element to
search: "); int n;
scanf("%d", &n);
while(head != NULL){
if(head->data == n){ printf("Found!..\n"); return;
}
head = head->next; }
printf("%s\n","Not found"); }
int main(){
int choice = 1;
struct node * head = NULL; while (choice != 7) {
printf("\n*********Menu************\n");
printf( "1.Insert at front\n"
"2.Insert at end\n" "3.Delete at front\n" "4.Delete at
end\n" "5.Search\n" "6.Display\n" "7.Exit\n");
scanf("%d", &choice); switch (choice) {
case 1:insertAtFront(&head);break;
case 2:insertAtEnd(&head);break;
case 3:delFront(&head);break;
case 4:delEnd(&head);break;
case 5:search(head);break;
case 6:disp(head); break;
}
}
return 0; }

OUTPUT

You might also like