Important Questions On Linked List :::


1) What pointer type is used to implement the heterogeneous linked list in C ?
Ans :

The answer is the void pointer.
The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to connect them. Since we can’t use ordinary pointers for this, we use the void pointer. Void Pointer is a generic pointer type, and capable of storing pointer to any type.

2) Can we do a Binary Search on a LInked List ?
Ans :

We can get to the middle of the array just by saying array[middle].
Now, we cannot do the same with a linked list. We will have to write your own, possible inefficient algorithm to get the value of the middle node of a linked list. In a linked list, you lose the ability to get the value of any node in a constant time.

One solution to the inefficiency of getting the middle of the linked list during a binary search is to have the first node contain one additional pointer that points to he node in the middle. Decide at the first node if you need to check the first or the second half of the linked list. Continue doing that with each half list.

3) Whether Linked List is linear or Non Linear data structure ?

Ans :

According to Access strategies Linked LIst is a linear one.
According to Storage Linked List is a Non Linear One.

4) How can we search for data in a Linked List ?

Ans :

The only way to search a linked list is with a linear search, because the only way a linked list’s members cab be accessed is sequentially. Sometimes it is quicker to take the data from a linked list and store it in a different data structure so that searches can be more efficient.

5) What member function places a new node at the end of the Linked List ?

Ans:

The appendNode() member function places a new node at the end of the linked list. The appendNode() requires an integer representing the current data of the node.

6) What does the prototype of calloc() look like ?

Ans :  

void *calloc(size_t num, size_t size);

7) What is the lenth of the null() list ?

Ans :    1

Double Ended Queue ( DEQUE)


 

  It is a homogeneous list of elements in which insertion and deletion operations are performed from both the ends i.e, we can insert elements from the rear end or from the front ends. Hence, it is called double-ended queue. It is commonly referred to as deque.

There are two types of deques. These two types are due to the restrictions put to preform either the insertions or deletions only at the end. They are :

1) Input Restricted Deque
2) Output Restricted Deque

Four Operations :

=> Insertion of an element at the REAR end of the queue
=> Deletion of an element from the FRONT end of the queue
=> Insertion of an element at the FRONT end of the queue
=> Deletion of an element from the REAR end of the queue
For an input-restricted deque only the operations 1,2,3 & 4 are valid. And for an output-restricated deque only the operations specified in 1,2,3 are valid.

a) Insertion of an element at the REAR end of the queue

void dqinsert_rear(int q[10],int front,int rear,int item,int MAXSIZE)
{
if(rear == (MAXSIZE-1))
{
printf(” QUEUE is full “);
return;
}
rear=rear+1;
q[rear]=item;
}
b) Deletions of an element from the FRONT end of the queue

void dqdelete_front(int q[10],int front,int rear,int item)
{
if(front == rear)
{
printf(” QUEUE is empty “);
return;
}

front=front+1;
item=q[item];
}

3) Insertion of an element at the FRONT end of the queue

void dqinsert_front(int q[10], int front, int rear, int item)
{
if(front == 0)
{
printf(” QUEUE is full”);
return;
}
front=front-1;
q[front]=item;

}

d) Deletion of an element from the REAR end of the queue
void dqdelete_rear( int q[10], int front, int rear, int item)
{
if(front == rear)
{
printf(” QUEUE is empty “);
return;
}

rear=rear-1;
item=q[item];

}

e) Function to display the contents (status) of a QUEUE

void dq_display(int q[10], int front, int rear)
{
if(front <= rear)
{
printf(” Status of the Queue \n”);
for(i = front; i<=rear; i++)
printf(“%d”,dq[i]);

}
else
printf(” QUEUE is empty “);

}

C Program to Count chars,spaces,tabs and newlines in a file





#include<stdio.h>
#include<conio.h>
void main()
{
   FILE *fp;
   char ch;
   int nol=0,not=0,nos=0,noc=0;
   clrscr();
   fp=fopen(“animesh.txt”,”r”);
   while(1)
   {
      ch=fgetc(fp);
      if(ch==EOF)
      break;
      noc++;
      if(ch==’ ‘)
      nos++;
      if(ch==’\n’)
      nol++;
      if(ch==’\t’)
      not++;
   }
   fclose(fp);
   printf(“\n Number of characters = %d”,noc);
   printf(“\n Number of blanks = %d”,nos);
   printf(“\n Number of tabs = %d”,not);
   printf(“\n Number of lines = %d”,nol);
   getch();
}


Program in C for Decimal to Roman Number conversion


/* Program in C for Decimal to Roman Number conversion */

#include<stdio.h>
#include<conio.h>

main()
{
int a,b,c,d,e;

printf(“Input a number (between 1-3000):”);
scanf(“%d”,&e);
while (e==0||e>3000)
{
printf (“ERROR: Invalid Input!”);
printf (“Enter the number again:”);
scanf (“%d”,&e);
}
if (e>3000)
printf(“Invalid”);
a = (e/1000)*1000;
b = ((e/100)%10)*100;
c = ((e/10)%10)*10;
d = ((e/1)%10)*1;

if (a ==1000)
printf(“M”);
else if (a ==2000)
printf(“MM”);
else if (a ==3000)
printf(“MMM”);

if (b == 100)
printf(“C”);
else if (b == 200)
printf(“CC”);
else if (b == 300)
printf(“CCC”);
else if (b == 400)
printf(“CD”);
else if (b ==500)
printf(“D”);
else if (b == 600)
printf(“DC”);
else if (b == 700)
printf(“DCC”);
else if (b ==800)
printf(“DCCC”);
else if (b == 900)
printf(“CM”);


if (c == 10)
printf(“X”);
else if (c == 20)
printf(“XX”);
else if (c == 30)
printf(“XXX”);
else if (c == 40)
printf(“XL”);
else if (c ==50)
printf(“L”);
else if (c == 60)
printf(“LX”);
else if (c == 70)
printf(“LXX”);
else if (c ==80)
printf(“LXXX”);
else if (c == 90)
printf(“XC”);

if (d == 1)
printf(“I”);
else if (d == 2)
printf(“II”);
else if (d == 3)
printf(“III”);
else if (d == 4)
printf(“IV”);
else if (d ==5)
printf(“V”);
else if (d == 6)
printf(“VI”);
else if (d == 7)
printf(“VII”);
else if (d ==8)
printf(“VIII”);
else if (d == 9)
printf(“IX”);
getch();
}

C PROGRAM TO PRINT ALL PERFECT NUMBERS BETWEEN 1 AND N AND THEN FIND THE SUM AND AVERAGE


#include <stdio.h>

#include<conio.h>
main()
{
    int n,sum = 0,s=0,c=0;
    float avg=0.0;
    printf(“\n Enter Limit :-\n”);
    scanf(“%d”,&n);
    printf(“\nTo print all the perfect no. between 1 and %d”,n);
    for(int num=1;num <=n;num++)
    {
            sum = 0;
            for(int i = 1; i < num; i++)
            {
            if(!(num%i))
            {
            sum+=i;
            }
            }
            if(sum == num)
            {
            s=s+sum;c++;       
            printf(“\n\n%d is a perfect number”,num);
            }
  }
  avg=(float)s/c;
  printf(“\n\nthe sum and average of the perfect numbers are=%d and %f”,s,avg);
  getch();
}

C Program to print a linked list in reverse


#include<stdio.h>
#include<conio.h>

typedef struct linked_list
{
int item;
struct linked_list *next;
}node;

node *create_list();
void print_list(node *);
void print_list_in_reverse(node *);

int main()
{
node *head=NULL;

printf("you can create a list by entering elements press -999 to end\n");
head=create_list();
printf("\n The list is\n");
print_list(head);
printf("\n The list in reverse order is");
print_list_in_reverse(head);
printf("\n");
return 0;
}

node *create_list()
{
int x;
node *temp=NULL;
scanf("%d",&x);
if(x!=-999)
{
temp=(node*)malloc(sizeof(node));
temp->item=x;
temp->next=NULL;
temp->next=create_list();
}
return temp;
}
void print_list(node *p)
{
if(p)
{
printf("%d ",p->item);
print_list(p->next);
}
else
printf("\n");
}

void print_list_in_reverse(node *p)
{
if(p)
{
print_list_in_reverse(p->next);
printf("%d ",p->item);
}
else
printf("\n");
}

C Program to draw SWASTIKA on center on the screen with blink effect


 #include<stdio.h> 

#include<conio.h> 

void main()

{

int i=0;

clrscr();

textcolor(RED+BLINK);

for(i=8;i<25;i++)

{

gotoxy(20,i); cprintf(“*”);

gotoxy(40,i); cprintf(“*”);

if(i<23)

gotoxy(20+2,i);

cprintf(“*”);

gotoxy(40+2,i+2); 

cprintf(“*”);

}

}


for(i=23;i<=40;i++)

{

if(i<39)

{

gotoxy(40,i);

cprintf(“*”);

gotoxy(60,i+2); 

cprintf(“*”);


}


gotoxy(40+2,i); 

cprintf(“*”);

gotoxy(60+2,i); c

printf(“*”);

}

for(i=20;i<42;i++)

{

gotoxy(i,25);

cprintf(“*”);

gotoxy(i,40); 

cprintf(“*”);

if(i<40)

{

gotoxy(i+2,25-2);

cprintf(“*”);

gotoxy(i,40-2); cprintf(“*”);

}

}

for(i=40;i<62;i++) 

{

if(i<60)

{

gotoxy(i+2,10);

cprintf(“*”);

gotoxy(i,25);

cprintf(“*”);

}

gotoxy(i,10-2); 

cprintf(“*”);

gotoxy(i,25-2); 

cprintfr*”);

getch();

}


OUTPUT IN C