Write a C program to perform some of the operation which
can be performed using Single linked list



Write a C program to perform some of the operation which can be performed using Single linked list..

Answer / vadivelt

Write a C program to perform some of the operation in
datastructure using Single linked list

Hi All,
I have covered almost all the possible operations which can
be performed with the Single linked list, with the
following code written by myself.

It is tested properly. Hope the code will work in all the
possible scenarios.

Compiler used: VS2005

Here in the code, almost all printf statements may be of two
lines, So if you are directly copiying the code, into ur
compiler u may get errors. So make all the printf to single
line then compile it and see the o/p.

Code is here.,

#include<stdio.h>
#include<conio.h>
#define ADDATBEG 1
#define ADDATEND 2
#define INSERT 3
#define DEL 4
#define SEARCH 5
#define SORT 6
#define REVERSE 7
#define NODEFROMEND 8

struct node
{
int data;
struct node *next;
};
struct node *BaseNode = '\0';

/*Function prototypes*/
void AddAtBeg(struct node **BaseNode, int data);
void AddAtEnd(struct node **BaseNode, int data);
void AddAtMid(struct node **BaseNode, int data, int pos);
void DelNode(struct node **BaseNode, int pos);
void DisList(struct node **BaseNode);
void SearchNode(struct node **BaseNode, int data);
void funchoice(int choice);
void SortList(struct node **BaseNode);
void NthNodeFrmEnd(struct node **BaseNode, int n);
void RevList(struct node **BaseNode);

int main()
{
int i, choice;
printf("ENTER THE CHOICE:\n");
printf("1.TO ADD THE NODE AT BEGINING - STACK \n2.TO ADD
THE NODE AT THE END - QUEUE\n");
printf("\nCHOICE:");
scanf("%d", &choice);
funchoice(choice);

printf("\n\nENTER CHOICE \n");
printf("3.TO INSERT NODE\n");
printf("4.TO DEL NODE\n");
printf("5.TO SEARCH GIVEN NODE IN THE LIST\n");
printf("6.TO SORT THE LIST\n");
printf("7.TO REVERSE THE LIST\n");
printf("8.VALUE OF 'N'th FROM LAST\n");
printf("\nCHOICE:");
scanf("%d", &choice);
funchoice(choice);

getch();
}

void funchoice(int choice)
{
int no = 0, element = 0,i;
switch(choice)
{
case ADDATBEG:
printf("\nENTER THE NO OF ELEMENTS TO BE ADDED\n");
scanf("%d", &no);
printf("\nENTER THE ELEMENTS TO BE ADDED IN THE
LIST\n");
for(i = 0; i<no; i++)
{
scanf("%d", &element);
AddAtBeg(&BaseNode, element);

}
DisList(&BaseNode);
break;

case ADDATEND:
printf("\nENTER THE NO OF ELEMENTS TO BE ADDED\n");
scanf("%d", &no);
printf("\nENTER THE ELEMENTS TO BE ADDED IN THE
LIST\n");
for(i = 0; i<no; i++)
{
scanf("%d", &element);
AddAtEnd(&BaseNode, element);
}
DisList(&BaseNode);
break;

case INSERT:
printf("\nENTER THE NODE POSTION TO THAT NEXT, NEW NODE
TO BE INSERTED\n");
scanf("%d", &no);
printf("\nENTER THE NO TO BE INSERTED\n");

scanf("%d", &element);
AddAtMid(&BaseNode, element, no);
DisList(&BaseNode);
break;

case DEL:
printf("\nENTER THE NODE POSTION OF THE NODE TO BE
DELETED\n");
scanf("%d", &no);
DelNode(&BaseNode, no);
DisList(&BaseNode);
break;

case SEARCH:
printf("\nENTER THE VALUE TO BE SEARCHED IN THE
LIST\n");
scanf("%d", &element);
SearchNode(&BaseNode, element);
break;

case SORT:
SortList(&BaseNode);
DisList(&BaseNode);
break;

case REVERSE:
RevList(&BaseNode);
DisList(&BaseNode);
break;

case NODEFROMEND:
printf("\nENTER THE NODE POSITION FROM END TO WHICH
THE VALUE HAS TO BE FOUND\n");
scanf("%d", &no);
NthNodeFrmEnd(&BaseNode, no);
break;

default:
exit(0);
break;

}
}
/*To print the data in the linked list*/
void DisList(struct node **BaseNode)
{
struct node *temp;
temp = *BaseNode;
printf("\nNODES IN THE LIST IS \n");
while(temp != '\0')
{
printf("%d ", temp->data);
temp = temp->next;
}
}


/*Stack Implementation*/
void AddAtBeg(struct node **BaseNode, int data)
{
struct node *temp;
temp = (struct node *)malloc(sizeof(struct node));
if(temp != '\0')
{
temp->data = data;
temp->next = *BaseNode;
*BaseNode = temp;
}
}
/*Queue Implementation*/
void AddAtEnd(struct node **BaseNode, int data)
{
struct node *temp, *temp1;
temp = *BaseNode;
if(temp == '\0')
{
/*List is empty - Create first node*/
temp1 = (struct node *)malloc(sizeof(struct node));
if(temp1 != '\0')
{
temp1->data = data;
temp1->next = '\0';
*BaseNode = temp1;
}
}
else
{
/*Node is existing already, So create another node*/
while(temp->next != '\0')
{
temp = temp->next;
}
temp1 = (struct node *)malloc(sizeof(struct node));
if(temp1 != '\0')
{
temp1->data = data;
temp1->next = '\0';
temp->next = temp1;
}
}
}

/*To insert a node between two nodes*/
void AddAtMid(struct node **BaseNode, int data, int pos)
{
/*To insert a node in between to the given node*/
struct node *temp, *temp1;
int count = 0;
temp = *BaseNode;
/*count, the no of nodes available in the list*/
while(temp->next != '\0')
{
count++;
temp = temp->next;
}
/*Check whether the enough nodes are available or not*/
if(pos < count+1)
{
temp = *BaseNode;
while(pos-1)
{
temp = temp->next;
pos--;
}
temp1 = (struct node *)malloc(sizeof(struct node));
if(temp1 != '\0')
{
temp1->data = data;
temp1->next = temp->next;
temp->next = temp1;
}
}
else
{
printf("NEW NODE CANT BE INSERTED:THERE IS NO ENOUGH
NODES IN THE LIST\n");
}
}

/*To delete a particular node in the list*/
void DelNode(struct node **BaseNode, int pos)
{
struct node *temp, *temp1;
int count = 0, pos1;
temp = *BaseNode;
pos1 = pos;

/*count, the no of nodes available in the list*/
while(temp->next != '\0')
{
count++;
temp = temp->next;
}
/*Check whether the enough nodes are available or not*/
if(pos <= (count+1))
{
temp = *BaseNode;
temp1 = temp->next;
while(pos-2)
{
temp = temp->next;
temp1 = temp1->next;
pos--;
}
temp->next = temp1->next;
printf("\n\nTHE DATA '%d' AT THE NODE '%d' IS
DELETED \n\n", temp1->data, pos1);
free(temp1);
}
else
{
printf("\n\nGIVEN NODE CANT BE DELETED:THERE IS NO
ENOUGH NODES IN THE LIST\n");
}
}

/*To search the given data in the linked list*/
void SearchNode(struct node **BaseNode, int data)
{
struct node *temp;
int count = 0;
temp = *BaseNode;
while(temp != '\0')
{
count++;
if(temp->data == data)
{
printf("\nTHE NODE WITH DATA '%d' FOUND AT
THE POSITION '%d'\n", data, count);
break;
}
else if(temp->next == '\0')
{
printf("\nTHE NODE IS NOT FOUND IN THE
LIST\n");
}
temp = temp->next;
}
}

/*To Sort the linked list in ascending order*/
void SortList(struct node **BaseNode)
{
struct node *temp, *temp1;
int localvar;
temp = *BaseNode;
temp1 = temp->next;
while(temp1 != '\0')
{
if(temp->data > temp1->data)
{
localvar = temp->data;
temp->data = temp1->data;
temp1->data = localvar;
}
temp = temp->next;
temp1 = temp1->next;
}
}

/*To find the 'n'th node from last node of linked list by
traversing the list only once*/
void NthNodeFrmEnd(struct node **BaseNode, int n)
{
struct node *temp, *temp1;
int count = 0;
temp1 = temp = *BaseNode;
while(temp1 != '\0')
{
count++;
temp1 = temp1->next;
}
if(n <= count)
{
count = 0;
temp1 = *BaseNode;
while(temp1->next != '\0')
{
count++;
if(count > n-1)
{
temp = temp->next;
}
temp1 = temp1->next;
}
printf("\nNODE %d FROM LAST, HOLD THE VALUE %d\n",
n, temp->data);
}
else
{
printf("\nTHERE IS NO ENOUGH NODES IN THE LIST\n");

}
}

/*To Reverse a given linked list*/
void RevList(struct node **BaseNode)
{
struct node *Fst, *Sec, *Thrd;
Sec = (*BaseNode)->next;
Thrd = Sec->next;
(*BaseNode)->next = '\0';

while(Thrd != '\0')
{
Sec->next = *BaseNode;
*BaseNode = Sec;
Sec = Thrd;
Thrd = Thrd->next;
}
Sec->next = *BaseNode;
*BaseNode = Sec;
}

Is This Answer Correct ?    11 Yes 2 No

Post New Answer

More C Interview Questions

Print all numbers which has a certain digit in a certain position eg: number=45687 1 number=4 2 number=5 etc

4 Answers  


there is two conditions , 1. while using for loop for printing 1 to 50 no's simulteneous 2. while using printf functios for printing 1 to 50 no's simulteneous with or without using variables who will take more time for compiling and execution? explain in details with reason?

1 Answers  


Write a program to maintain student’s record. Record should not be available to any unauthorized user. There are three (3) categories of users. Each user has its own type. It depends upon user’s type that which kind of operations user can perform. Their types and options are mentioned below: 1. Admin (Search Record [by Reg. No or Name], View All Records, Insert New Record, Modify Existing Record) 2. Super Admin (Search Record [by Reg. No or Name], View All Records, Insert New Record, Modify Existing Record, Delete Single Record) 3. Guest (Search Record [by Reg. No or Name], View All Records) When first time program runs, it asks to create accounts. Each user type has only 1 account (which means that there can be maximum 3 accounts). In account creation, following options are required: Login Name: <6-10 alphabets long, should be unique> Password: <6-10 alphabets long, should not display characters when user type> Confirm Password: <must match with the Password, should not display characters when user type> Account Type: <One character long, A or S or G where A for Admin, S for Super Admin, G for Guest> Login Name, Password and Account Type should be stored in a separate file in encrypted form. (Encryption means that actual information should be changed and Decryption means that Encrypted information is changed back to the actual information) If any of the above mentioned requirement(s) does not meet then point out mistake and ask user to specify information again. When Program is launched with already created accounts, it will ask for user name and password to authenticate. On successful authentication, give options according to the user’s type.

0 Answers  


Write a program to find the given number is odd or even without using any loops(if,for,do,while)

4 Answers   CNC, Gokul,


WAP to find that given no is small or capital

3 Answers  






diff. between *p and **p

3 Answers  


write a program that types this pattern: 12345678987654321 12345678 87654321 1234567 7654321 123456 654321 12345 54321 1234 4321 123 321 12 21 1 1

0 Answers  


What is the difference between exit() and _exit() function in c?

0 Answers  


how can i write a program that prints out a box such that whenever i press any key8(coordinate number) on the keyboard, the box moves.

0 Answers  


What is quick sort in c?

0 Answers  


what is recursion in C

0 Answers   Cap Gemini,


What oops means?

0 Answers  


Categories