Professional Documents
Culture Documents
Regulations 2013
Sl.No. Description
3. List of experiments
4. Requirements
6. Lab Manual
Objective and Outcome of the Course
OBJECTIVES:
The student should be made to:
● Be familiar with C programming
● Be exposed to implementing abstract data types
● Learn to use files
● Learn to implement sorting and searching algorithms
● Design and implement C programs for implementing stacks, queues, linked lists.
● Apply good programming design methods for program development.
● Apply the different data structures for implementing solutions to practical problems.
● Develop searching and sorting programs.
● Understand the basic data structures
Syllabus
TOTAL: 45 PERIODS
List of Experiments
1. C Program to Create a Linked List & Display the Elements in the List
2. C Program to Illustrate the Operations of Singly Linked List
3. C Program to Search for an Element in the Linked List using Recursion
4. C Program to Display the Nodes of a Linked List in Reverse using Recursion
5. C Program Count the Number of Occurrences of an Element in the Linked List using Recursion
6. C Program to Display all the Nodes in a Linked List using Recursion
7. C Program find the Length of the Linked List using Recursion
8. C Program to Print the Alternate Nodes in a Linked List using Recursion
9. C Program to Print the Alternate Nodes in a Linked List without using Recursion
10. C Program find the Length of the Linked List without using Recursion
11. C Program Count the Number of Occurrences of an Element in the Linked List without using
Recursion
12. C Program to Display all the Nodes in a Linked List without using Recursion
13. C Program to Display the Nodes of a Linked List in Reverse without using Recursion
14. C Program to Search for an Element in the Linked List without using Recursion
15. C Program to Implement a Doubly Linked List & provide Insertion, Deletion & Display
Operations
16. C Program to Add Corresponding Positioned Elements of 2 Linked Lists
17. C Program to Check whether 2 Lists are Same
18. C Program to Check whether a Singly Linked List is a Palindrome
19. C Program to Convert a given Singly Linked List to a Circular List
20. C Program to Find Number of Occurences of All Elements in a Linked List
21. C Program to Find the first Common Element between the 2 given Linked Lists
22. C Program to Find the Largest Element in a Doubly Linked List
23. C Program to Print Middle most Node of a Linked List
24. C Program to Read a Linked List in Reverse
25. C Program to Remove Duplicates from a Linked List
26. C Program to Reverse a Linked List
27. C Program to Reverse only First N Elements of a Linked List
28. C Program to Implement Circular Doubly Linked List
29. C Program to Demonstrate Circular Single Linked List
30. C Program to Implement Singly Linked List using Dynamic Memory Allocation
31. C Program to Find Intersection & Union of 2 Linked Lists
32. C Program to Implement Doubly Linked List using Singly Linked List
33. C Program to Interchange the two Adjacent Nodes given a circular Linked List
34. C Program to Interchange two Elements of the List without touching the Key Field
35. C Program to Modify the Linked List such that All Even Numbers appear before all the Odd
Numbers in the Modified Linked List
36. C Program to Print Nth Node from the last of a Linked List
37. C Program to Read a Linked List in Reverse
C Programming Stacks & Queues
● The manual contains the programs for all the model exercises that has been executed on DEV
C++ IDE
● The programs are given here for your quick reference and understanding purposes only. This
is not the actual exercises to be done in the laboratory classes.
● The actual exercises will be given to you in the laboratory classes based on the similar
concepts explained in the model exercises.
● All the students are instructed to maintain a hard copy of the manual, separate notebook and
file folder for this course.
● When you come to lab, you have to write the pseudo code for your exercises on spot and then
implement the same on above mentioned IDE. The pseudo code must be prepared on the note
book and all the observations of your implementation must be recorded in the note book, once
you finished your exercises. At the end of the laboratory classes, the note book must be
signed by the faculty in charge and assessment will be done.
● When you are coming for the laboratory classes, the completed record sheets for the previous
class exercises must be produced for signature of faculty in charge except for first class of the
course.
● All the exercises need to be completed in the respective classes itself. The deadline will not be
extended at any circumstances. All the assessment will be done in the same class itself.
● You are encouraged to do all the necessary preparation before you come to the lab. Treat
every lab class as lab examination.
Numbers perfectly divisible by 2 are known even numbers and numbers which are not divisible by
2 are called odd numbers.
This program takes an integer from user and checks whether that number is even or odd and
displays the result.
Step2: The remainder is found when that number is divided by 2 and checked whether remainder
is 0 or not.
2[a].If remainder is 0 then, that number is even otherwise that number is odd.
Step3: This task is performed using if...else statement in C programming and the result is
displayed accordingly.
#include <stdio.h>
int main(){
int num;
scanf("%d",&num);
printf("%d is even.",num);
else
printf("%d is odd.",num);
return 0;
Output 1
25 is odd.
Output 2
12 is even.
This program asks user to enter a character and checks whether that character is a vowel or not.
This task can be performed in C programming using if...else statement.
Step2:Then, this character is checked, whether it is any one of these ten characters a, A, e, E, i, I,
o, O, u and U using logical OR operator ||.
Step3: If that character is any one of these ten characters, that alphabet is a vowel if not that
alphabet is a consonant.
#include <stdio.h>
int main(){
char c;
scanf("%c",&c);
if(c=='a'||c=='A'||c=='e'||c=='E'||c=='i'||c=='I'||c=='o'||c=='O'||c=='u'||c=='U')
printf("%c is a vowel.",c);
else
printf("%c is a consonant.",c);
return 0;
}
Output 1
Enter an alphabet: i
i is a vowel.
Output 2
Enter an alphabet: G
G is a consonant
Source Code 1
#include <stdio.h>
int main(){
float a, b, c;
return 0;
Source Code 2
#include <stdio.h>
int main(){
float a, b, c;
if (a>=b)
if(a>=c)
else
else
if(b>=c)
else
return 0;
Source Code 3
#include <stdio.h>
int main(){
float a, b, c;
else
return 0;
Output 1
13.452
10.193
#include <stdio.h>
int main()
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
}
else if (determinant==0)
r1 = r2 = -b/(2*a);
else
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
return 0;
Output 1
5.6
Output 2
In this program, all numbers between two intervals is passed to function int check_prime(int
num) using for loop. This function checks whether a number is prime or not. If the number is
prime it returns 1 if not it return 0.
#include<stdio.h>
int main(){
int n1,n2,i,flag;
for(i=n1+1;i<n2;++i)
flag=check_prime(i);
if(flag==0)
printf("%d ",i);
return 0;
int j,flag=0;
for(j=2;j<=num/2;++j){
if(num%j==0){
flag=1;
break;
return flag;
Output:
Enter two numbers(intervals): 10
30
int main()
char c;
int n,temp=0;
scanf("%d",&n);
c=getche();
if(c=='p'|| c=='P')
temp=prime(n);
if(temp==1)
else
if(c=='a'|| c=='A')
temp=armstrong(n);
if(temp==1)
else
return0;
int prime(int n)
int i, flag=1;
for(i=2; i<=n/2;++i)
if(n%i==0)
flag=0;
break;
return flag;
int armstrong(int n)
temp=n;
while(n!=0)
num+=(n%10)*(n%10)*(n%10);
n/=10;
}
if(num==temp)
flag=1;
return flag;
}
Output:
Eneter a positive integer: 371
int main()
int n, i, flag=0;
scanf("%d",&n);
for(i=2; i<=n/2;++i)
if(prime(i)!=0)
if( prime(n-i)!=0)
flag=1;
}
}
if(flag==0)
return0;
int i, flag=1;
for(i=2; i<=n/2;++i)
if(n%i==0)
flag=0;
return flag;
}
Output:
Enter a positive integer: 34
34 = 3 + 31
34 = 5 + 29
34 = 11 + 23
34 = 17 + 17
C program to Find Sum of Natural Numbers using Recursion.
In this program, user is asked to enter a positive integer and sum of natural numbers up to that
integer is displayed by this program. Suppose, user enters 5 then,
Sum will be equal to 1+2+3+4+5 = 15
#include<stdio.h>
int main()
int n;
scanf("%d",&n);
printf("Sum = %d",add(n));
return0;
int add(int n)
if(n!=0)
}
Output:
Enter an positive integer: 10
Sum = 210
C program to Calculate Factorial of a Number Using Recursion
This program takes a positive integer from user and calculates the factorial of that number. Instead
of loops to calculate factorial, this program uses recursive function to calculate the factorial of a
number.
#include<stdio.h>
int main()
int n;
scanf("%d",&n);
return0;
int factorial(int n)
if(n!=1)
return n*factorial(n-1);
}
Output:
Enter an positive integer: 6
Factorial of 6 = 720
C program to Reverse a Sentence Using Recusrion
This program takes a sentence from user and reverses that sentence using recursion. This program
does not use string to reverse the sentence or store the sentence.
This program prints "Enter a sentence: " then, Reverse() function is called. This function stores the
first letter entered by user and stores in variable c. If that variable is other than '\n' [ enter
character] then, again Reverse()function is called. Don't assume this Reverse() function and
the Reverse() function before is same although they both have same name. Also, the variables are
also different, i.e., c variable in both functions are also different. Then, the second character is
stored in variable c of second Reverse function. This process goes on until user enters '\n'. When,
user enters '\n', the last function Reverse() function returns to second last Reverse() function and
prints the last character. Second last Reverse() function returns to the third last Reverse() function and
prints second last character. This process goes on and the final output will be the reversed
sentence.
#include<stdio.h>
voidReverse();
int main()
Reverse();
return0;
voidReverse()
char c;
scanf("%c",&c);
if( c !='\n')
Reverse();
printf("%c",c);
}
Output:
Enter a sentence: margorp emosewa
awesome program
C program to Calculate the Power of a Number Using Recursion
This program takes two integers from user ( base number and a exponent) and calculates the
power. Instead of using loops to calculate power, this program uses recursion to calculate the
power of a number.
#include<stdio.h>
int main()
intbase, exp;
scanf("%d",&base);
scanf("%d",&exp);
return0;
if( exp!=1)
return(base*power(base,exp-1));
}
Output:
Enter base number: 3
3^3 = 27
C Program to Calculate Average Using Arrays
This program takes n number of element from user(where, n is specified by user), stores data in an
array and calculates the average of those numbers.
#include<stdio.h>
int main(){
int n, i;
float num[100], sum=0.0, average;
scanf("%d",&n);
while(n>100|| n<=0)
scanf("%d",&n);
for(i=0; i<n;++i)
scanf("%f",&num[i]);
sum+=num[i];
average=sum/n;
printf("Average = %.2f",average);
return0;
}
Output
int main(){
int i,n;
float arr[100];
scanf("%d",&n);
printf("\n");
scanf("%f",&arr[i]);
arr[0]=arr[i];
return0;
Output
C Program to Create a Linked List & Display the Elements in the List
This C Program creates a linked list &display the elements in the list. Linked list is an ordered set
of data elements, each containing a link to its successor. This program is to create a linked list and
display all the elements present in the created list.
Here is source code of the C program to create a linked list & display the elements in the list. The
C program is successfully compiled and run on a Linux system. The program output is also shown
below.
/*
C program to create a linked list and display the elements in the list
*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
void main()
ruct node
t num;
t count =0;
t choice =1;
first =0;
hile(choice)
head =(NODE *)malloc(sizeof(NODE));
anf("%d",&head-> num);
(first !=0)
temp->ptr = head;
temp = head;
se
lush(stdin);
anf("%d",&choice);
temp->ptr =0;
temp = first;
hile(temp !=0)
rintf("%d=>", temp->num);
count++;
rintf("NULL\n");
rintf("No. of nodes in the list = %d\n", count);
Output
5=>9=>3=>NULL
This C Program illustrates the operations of singly linked list. The program does insertion and
removal of elements from any position in the sequence.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 30
struct emp_data
{
int empno;
char empName[MAX];
char designation[MAX];
struct emp_data *next;
};
struct emp_data *insert(struct emp_data *front,int id,char name[],
char desg[])
{
struct emp_data *newnode;
newnode =(struct emp_data*)malloc(sizeof(struct emp_data));
if(newnode == NULL)
{
printf("\n Allocation failed \n");
exit(2);
}
newnode->empno = id;
strcpy(newnode->empName, name);
strcpy(newnode->designation, desg);
newnode->next = front;
front = newnode;
return(front);
}
void printNode(struct emp_data *p)
{
printf("\n Employee Details...\n");
printf("\n Emp No : %d", p->empno);
printf("\n Name : %s", p->empName);
printf("\n Designation : %s\n", p->designation);
printf("-------------------------------------\n");
}
struct emp_data* deleteNode(struct emp_data *front,int id)
{
struct emp_data *ptr;
struct emp_data *bptr;
if(front->empno == id)
{
ptr = front;
printf("\n Node deleted:");
printNode(front);
front = front->next;
free(ptr);
return(front);
}
for(ptr = front->next, bptr = front; ptr != NULL; ptr = ptr->next,
bptr = bptr->next)
{
if(ptr->empno == id)
{
printf("\n Node deleted:");
printNode(ptr);
bptr->next = ptr->next;
free(ptr);
return(front);
}
}
printf("\n Employee Number %d not found ", id);
return(front);
}
void search(struct emp_data *front,int key)
{
struct emp_data *ptr;
for(ptr = front; ptr != NULL; ptr = ptr -> next)
{
if(ptr->empno == key)
{
printf("\n Key found:");
printNode(ptr);
return;
}
}
printf("\n Employee Number %d not found ", key);
}
void display(struct emp_data *front)
{
struct emp_data *ptr;
for(ptr = front; ptr != NULL; ptr = ptr->next)
{
printNode(ptr);
}
}
void menu()
{
printf("---------------------------------------------\n");
printf("Press 1 to INSERT a node into the list \n");
printf("Press 2 to DELETE a node from the list \n");
printf("Press 3 to DISPLAY the list \n");
printf("Press 4 to SEARCH the list \n");
printf("Press 5 to EXIT \n");
printf("---------------------------------------------\n");
}
char option()
{
char choice;
printf("\n\n>> Enter your choice: ");
switch(choice=getche())
{
case'1':
case'2':
case'3':
case'4':
case'5':return(choice);
default:printf("\n Invalid choice.");
}
return choice;
}
void main()
{
struct emp_data *linkList;
char name[21], desig[51];
char choice;
int eno;
linkList = NULL;
printf("\n Welcome to demonstration of singly linked list \n");
menu();
do
{
/* choose oeration to be performed */
choice = option();
switch(choice)
{
case'1':
printf("\n Enter the Employee Number : ");scanf("%d",&eno);
printf("Enter the Employee name : ");fflush(stdin);
gets(name);
printf("Enter the Employee Designation : ");gets(desig);
linkList = insert(linkList, eno, name, desig);break;
case'2':
printf("\n\n Enter the employee number to be deleted: ");scanf("%d",&eno);
linkList = deleteNode(linkList, eno);break;
case'3':
if(linkList == NULL)
{
printf("\n List empty.");
break;
}
display(linkList);
break;
case'4':
printf("\n\n Enter the employee number to be searched: ");
scanf("%d",&eno);
search(linkList, eno);
break;
case'5':break;
}
}while(choice !='5');
}
Output:
---------------------------------------------
Press 1 to INSERT a node into the list
Press 5 to EXIT
---------------------------------------------
Employee Details...
Emp No : 12
Name : ram
Designation : HR
-------------------------------------
Invalid choice.
Key found:
Employee Details...
Emp No : 12
Name : ram
Designation : HR
-------------------------------------
Invalid choice.
Node deleted:
Employee Details...
Emp No : 12
Name : ram
Designation : HR
-------------------------------------
Invalid choice.
Invalid choice.
This C Program uses recursive function & search for an element in a linked list. A linked list is an
ordered set of data elements, each containing a link to its successor.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int a;
};
int main()
scanf("%d",&num);
generate(&head, num);
scanf("%d",&key);
delete(&head);
int i;
if(*head == NULL)
*head = temp;
(*head)->next = NULL;
else
temp->next =*head;
*head = temp;
}}}
if(head->a == key)
if(head->next == NULL)
return;
while(*head != NULL)
{
temp =*head;
*head =(*head)->next;
free(temp);
}}
Output:
This C Program implement a stack using linked list. Stack is a type of queue that in practice is
implemented as an area of memory that holds all local variables and parameters used by any
function, and remembers the order in which functions are called so that function returns occur
correctly. Each time a function is called, its local variables and parameters are “pushed onto” the
stack. When the function returns,these locals and parameters are “popped.” Because of this, the
size of a program’s stack fluctuates constantly as the program is running, but it has some
maximum size. stack is as a last in, first out (LIFO) abstract data type and linear data structure.
Linked list is a data structure consisting of a group of nodes which together represent a sequence.
Here we need to apply the application of linkedlist to perform basic operations of stack.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node *ptr;
}*top,*top1,*temp;
int topelement();
void push(int data);
void pop();
void empty();
void display();
void destroy();
void stack_count();
void create();
int count =0;
void main()
{
int no, ch, e;
printf("\n 1 - Push");
printf("\n 2 - Pop");
printf("\n 3 - Top");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Dipslay");
printf("\n 7 - Stack Count");
printf("\n 8 - Destroy stack");
create();
while(1)
{
printf("\n Enter choice : ");
scanf("%d",&ch);
switch(ch)
{
case1:
printf("Enter data : ");
scanf("%d",&no);
push(no);
break;
case2:
pop();
break;
case3:
if(top == NULL)
printf("No elements in stack");
else
{
e = topelement();
printf("\n Top element : %d", e);
}
break;
case4:
empty();
break;
case5:
exit(0);
case6:
display();
break;
case7:
stack_count();
break;
case8:
destroy();
break;
default:
printf(" Wrong choice, Please enter correct choice ");
break;
}
}
}
/* Create empty stack */
void create()
{
top = NULL;
}
/* Count stack elements */
void stack_count()
{
printf("\n No. of elements in stack : %d", count);
}
/* Push data into stack */
void push(int data)
{
if(top == NULL)
{
top =(struct node *)malloc(1*sizeof(struct node));
top->ptr = NULL;
top->info = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->ptr = top;
temp->info = data;
top = temp;
}
count++;
}
/* Display stack elements */
void display()
{
top1 = top;
if(top1 == NULL)
{
printf("Stack is empty");
return;
}
while(top1 != NULL)
{
printf("%d ", top1->info);
top1 = top1->ptr;
}
}
/* Pop Operation on stack */
void pop()
{
top1 = top;
if(top1 == NULL)
{
printf("\n Error : Trying to pop from empty stack");
return;
}
else
top1 = top1->ptr;
printf("\n Popped value : %d", top->info);
free(top);
top = top1;
count--;
}
/* Return top element */
int topelement()
{
return(top->info);
}
/* Check if stack is empty or not */
void empty()
{
if(top == NULL)
printf("\n Stack is empty");
else
printf("\n Stack is not empty with %d elements", count);
}
/* Destroy entire stack */
void destroy()
{
top1 = top;
while(top1 != NULL)
{
top1 = top->ptr;
free(top);
top = top1;
top1 = top1->ptr;
}
free(top1);
top = NULL;
printf("\n All stack elements destroyed");
count =0;
}
Output:
1 - Push
2 - Pop
3 - Top
4 - Empty
5 - Exit
6 - Dipslay
7 - Stack Count
8 - Destroy stack
Enter choice : 1
Enter data : 56
Enter choice : 1
Enter data : 80
Enter choice : 2
Popped value : 80
Enter choice : 3
Top element : 56
Enter choice : 1
Enter data : 78
Enter choice : 1
Enter data : 90
Enter choice : 6
907856
Enter choice : 7
Enter choice : 8
Stack is empty
Enter choice : 5
C Program Count the Number of Occurrences of an Element in the Linked List using
Recursion
This C Program uses recursive function & finds the occurrence for an element in an unsorted list. The
user enters the element need to be counted.
#include <stdio.h>
void occur(int[],int,int,int,int*);
int main()
int list[20];
int i;
scanf("%d",&size);
list[i]=rand()% size;
scanf("%d",&key);
return0;
}
void occur(int list[],int size,int index,int key,int*count)
if(size == index)
return;
if(list[index]== key)
*count +=1;
Output:
1 4 2 5 1 3 3
This C Program implements queue using linked list. Queue is a particular kind of abstract data
type or collection in which the entities in the collection are kept in order and the principal (or only)
operations on the collection are the addition of entities to the rear terminal position, known as
enqueue, and removal of entities from the front terminal position, known as dequeue. This makes
the queue a First-In-First-Out (FIFO) data structure. Linked list is a data structure consisting of a
group of nodes which together represent a sequence. Here we need to apply the application of
linkedlist to perform basic operations of queue.
#include <stdio.h>
#include <stdlib.h>
struct node
int info;
}*front,*rear,*temp,*front1;
int frontelement();
void deq();
void empty();
void display();
void create();
void queuesize();
int count = 0;
void main()
printf("\n 1 - Enque");
printf("\n 2 - Deque");
printf("\n 4 - Empty");
printf("\n 5 - Exit");
printf("\n 6 - Display");
while (1)
scanf("%d", &ch);
switch (ch)
case 1:
scanf("%d", &no);
enq(no);
break;
case 2:
deq();
break;
case 3:
e = frontelement();
if (e != 0)
else
break;
case 4:
empty();
break;
case 5:
exit(0);
case 6:
display();
break;
case 7:
queuesize();
break;
default:
break;
void create()
void queuesize()
if (rear == NULL)
{
rear->ptr = NULL;
rear->info = data;
front = rear;
else
rear->ptr = temp;
temp->info = data;
temp->ptr = NULL;
rear = temp;
count++;
void display()
front1 = front;
printf("Queue is empty");
return;
}
while (front1 != rear)
front1 = front1->ptr;
if (front1 == rear)
printf("%d", front1->info);
void deq()
front1 = front;
if (front1 == NULL)
return;
else
if (front1->ptr != NULL)
front1 = front1->ptr;
free(front);
front = front1;
else
{
free(front);
front = NULL;
rear = NULL;
count--;
int frontelement()
return(front->info);
else
return 0;
void empty()
else
Output:
1 - Enque
2 - Deque
3 - Front element
4 - Empty
5 - Exit
6 - Display
7 - Queue size
Enter choice : 1
Enter data : 14
Enter choice : 1
Enter data : 85
Enter choice : 1
Enter data : 38
Enter choice : 3
Front element : 14
Enter choice : 6
14 85 38
Enter choice : 7
Queue size : 3
Enter choice : 2
Dequed value : 14
Enter choice : 6
85 38
Enter choice : 7
Queue size : 2
Enter choice : 4
Enter choice : 5
C Program to Implement a Doubly Linked List & provide Insertion, Deletion & Display
Operations
This C Program implement a doubly linked list & provide insertion, deletion & display operations.
Doubly linked list is a linked data structure that consists of a set of sequentially linked records
called nodes. Each node contains two fields, called links, that are references to the previous and to
the next node in the sequence of nodes. The beginning and ending nodes’ previous and next links,
respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate
traversal of the list. Here we need to preform insertion, deletion and display all the modes of given
doubly linked list.
#include <stdio.h>
#include <stdlib.h>
struct node
{
struct node *prev;
int n;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void insert1();
void insert2();
void insert3();
void traversebeg();
void traverseend(int);
void sort();
void search();
void update();
void delete();
int count =0;
void main()
{
int ch;
h = NULL;
temp = temp1 = NULL;
printf("\n 1 - Insert at beginning");
printf("\n 2 - Insert at end");
printf("\n 3 - Insert at position i");
printf("\n 4 - Delete at i");
printf("\n 5 - Display from beginning");
printf("\n 6 - Display from end");
printf("\n 7 - Search for element");
printf("\n 8 - Sort the list");
printf("\n 9 - Update an element");
printf("\n 10 - Exit");
while(1)
{
printf("\n Enter choice : ");
scanf("%d",&ch);
switch(ch)
{
case1:
insert1();
break;
case2:
insert2();
break;
case3:
insert3();
break;
case4:
delete();
break;
case5:
traversebeg();
break;
case6:
temp2 = h;
if(temp2 == NULL)
printf("\n Error : List empty to display ");
else
{
printf("\n Reverse order of linked list is : ");
traverseend(temp2->n);
}
break;
case7:
search();
break;
case8:
sort();
break;
case9:
update();
break;
case10:
exit(0);
default:
printf("\n Wrong choice menu");
}
}
}
/* TO create an empty node */
void create()
{
int data;
temp =(struct node *)malloc(1*sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
printf("\n Enter value to node : ");
scanf("%d",&data);
temp->n = data;
count++;
}
/* TO insert at beginning */
void insert1()
{
if(h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
/* To insert at end */
void insert2()
{
if(h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
/* To insert at any position */
void insert3()
{
int pos, i =2;
printf("\n Enter position to be inserted : ");
scanf("%d",&pos);
temp2 = h;
if((pos <1)||(pos >= count +1))
{
printf("\n Position out of range to insert");
return;
}
if((h == NULL)&&(pos !=1))
{
printf("\n Empty list cannot insert other than 1st position");
return;
}
if((h == NULL)&&(pos ==1))
{
create();
h = temp;
temp1 = h;
return;
}
else
{
while(i < pos)
{
temp2 = temp2->next;
i++;
}
create();
temp->prev = temp2;
temp->next = temp2->next;
temp2->next->prev = temp;
temp2->next = temp;
}
}
/* To delete an element */
void delete()
{
int i =1, pos;
printf("\n Enter position to be deleted : ");
scanf("%d",&pos);
temp2 = h;
if((pos <1)||(pos >= count +1))
{
printf("\n Error : Position out of range to delete");
return;
}
if(h == NULL)
{
printf("\n Error : Empty list no elements to delete");
return;
}
else
{
while(i < pos)
{
temp2 = temp2->next;
i++;
}
if(i ==1)
{
if(temp2->next == NULL)
{
printf("Node deleted from list");
free(temp2);
temp2 = h = NULL;
return;
}
}
if(temp2->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
printf("Node deleted from list");
return;
}
temp2->next->prev = temp2->prev;
if(i !=1)
temp2->prev->next = temp2->next;/* Might not need this statement if i == 1 check */
if(i ==1)
h = temp2->next;
printf("\n Node deleted");
free(temp2);
}
count--;
}
/* Traverse from beginning */
void traversebeg()
{
temp2 = h;
if(temp2 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : ");
while(temp2->next != NULL)
{
printf(" %d ", temp2->n);
temp2 = temp2->next;
}
printf(" %d ", temp2->n);
}
/* To traverse from end recursively */
void traverseend(int i)
{
if(temp2 != NULL)
{
i = temp2->n;
temp2 = temp2->next;
traverseend(i);
printf(" %d ", i);
}
}
/* To search for an element in the list */
void search()
{
int data, count =0;
temp2 = h;
if(temp2 == NULL)
{
printf("\n Error : List empty to search for data");
return;
}
printf("\n Enter value to search : ");
scanf("%d",&data);
while(temp2 != NULL)
{
if(temp2->n == data)
{
printf("\n Data found in %d position",count +1);
return;
}
else
temp2 = temp2->next;
count++;
}
printf("\n Error : %d not found in list", data);
}
/* To update a node value in the list */
void update()
{
int data, data1;
printf("\n Enter node data to be updated : ");
scanf("%d",&data);
printf("\n Enter new data : ");
scanf("%d",&data1);
temp2 = h;
if(temp2 == NULL)
{
printf("\n Error : List empty no node to update");
return;
}
while(temp2 != NULL)
{
if(temp2->n == data)
{
temp2->n = data1;
traversebeg();
return;
}
else
temp2 = temp2->next;
}
printf("\n Error : %d not found in list to update", data);
}
/* To sort the linked list */
void sort()
{
int i, j, x;
temp2 = h;
temp4 = h;
if(temp2 == NULL)
{
printf("\n List empty to sort");
return;
}
for(temp2 = h; temp2 != NULL; temp2 = temp2->next)
{
for(temp4 = temp2->next; temp4 != NULL; temp4 = temp4->next)
{
if(temp2->n > temp4->n)
{
x = temp2->n;
temp2->n = temp4->n;
temp4->n = x;
}
}
}
traversebeg();
}
Output:
1 - Insert at beginning
2 - Insert at end
3 - Insert at position i
4 - Delete at i
9 - Update an element
10 - Exit
Enter choice : 1
Enter choice : 2
Enter choice : 4
Node deleted
Enter choice : 1
Enter choice : 3
Enter choice : 4
Enter choice : 1
Enter choice : 1
Enter choice : 3
Enter choice : 4
Node deleted
Enter choice : 7
Enter choice : 8
Enter choice : 9
Enter choice : 9
Enter choice : 5
Linked list elements from begining : 1334349067
Enter choice : 6
Enter choice : 7
Enter choice : 8
Enter choice : 7
Enter choice : 9
Enter choice : 10
This C Program adds the corresponding positioned elements of 2 linked lists and display.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
struct node
int num;
};
int main()
pcount = feednumber(&p);
qcount = feednumber(&q);
display(p);
display(q);
display(res);
release(&p);
release(&q);
release(&res);
return0;
ch =getchar();
while(ch !='\n')
dig =atoi(&ch);
temp->num = dig;
temp->next = NULL;
count++;
if((*head)== NULL)
*head = temp;
rear = temp;
else
{
rear->next = temp;
rear = rear->next;
ch =getchar();
return count;
while(head != NULL)
printf("%d", head->num);
head = head->next;
printf("\n");
while((*head)!= NULL)
(*head)=(*head)->next;
free(temp);
temp =*head;
}
}
/*Function to add the list of numbers and store them in 3rd list*/
ptemp = p;
qtemp = q;
ptemp = ptemp->next;
qtemp = qtemp->next;
temp->next = result;
result = temp;
pcount--;
qcount--;
}
/*both or one of the 2 lists have been read completely by now*/
while(pcount !=0)
ptemp = p;
ptemp = ptemp->next;
temp->next = result;
result = temp;
pcount--;
while(qcount !=0)
qtemp = q;
qtemp = qtemp->next;
temp->next = result;
result = temp;
qcount--;
return result;
Output:
int main()
{
struct node *p = NULL;
struct node *q = NULL;
int result;
return 0;
}
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = *head;
*head = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
}while (ch != 0);
printf("\n");
}