Вы находитесь на странице: 1из 33

IIIT NAYA RAIPUR

DATA STRUCTURES AND ALGORITHMS


PHASE – 1
LAB PROGRAMS

NAME - ARNAB ROY


ROLL NO. – 17101011
BRANCH – ECE
1. Static Implementations of Stacks and Queues
1. Program to demonstrate the stack operations by using the global variables.
//Program to implement stack

#include<stdio.h>
#include<stdlib.h>
#define SS 50

int top=-1,stack[SS];
void push() //Function to insert element in stack
{
int item;
if(top==SS-1) //Check overflow Condition
{
printf("\nOverflow\n");
}
else
{
printf("\nEnter element :");
scanf("%d",&item);
top=top+1;
stack[top]=item; // Element inserted in stack
}
}

void pop() //Function to delete item from the stack


{
if(top==-1) //Check underflow Condition
{
printf("\nStack is empty!\n");
}
else
{
printf("\nDeleted element is : %d",stack[top]);
top=top-1; // Delete element from queue
}
}

void display() //Function to display item of the stack


{
int i;
if(top==-1) // Check if stack is empty
{
printf("\nStack is empty!\n");
}
else
{
printf("\nThe elements in stack are :\n");
for(i=top; i>=0; --i)
printf("%d\n",stack[i]); // Display elements in stack
}
}

int main()
{ // Start of main()
int ch;
while(1)
{
printf("\nStack Implementation");
printf("\n\n1.Push\n2.Pop\n3.Display\n4.Quit");
printf("\n\nEnter your choice:");
scanf("%d",&ch);

switch(ch) // Select operations


{
case 1:
push(); // Call push() function
break;
case 2:
pop(); // Call pop() function
break;
case 3:
display(); // Cal display() function
break;
case 4:
exit(0);
default:
printf("\nInvalid Entry\n");
break;
}
}
return 0;
} // End of main()

2. Program to implement multiple stacks by passing the parameters


//Program to implement multiple stacks by passing parameters
#include<stdio.h>
#include<stdlib.h>
#define MSS 20 //Maximum stack size
#define NOS 5 //Maximum number of stacks that can be created
void push(int item,int stknum,int top[],int bndry[],int stk[]) //Function to insert
element in sub-stack
{
if(top[stknum]==bndry[stknum+1]) // Check overflow Condition
{
printf("\nStack %d is full\n",stknum);
return;
}
top[stknum]++;
stk[top[stknum]]=item; // Element inserted in a sub-stack
}
int pop(int stknum,int top[],int bndry[],int stk[]) //Function to delete element from a
sub-stack
{ int element;
if(top[stknum]==bndry[stknum]) //Check underflow Condition
return -1;
else
{
element=stk[top[stknum]];
top[stknum]--; //Element deleted from a sub-stack
return element;
}
}
void display(int stknum,int top[],int bndry[],int stk[]) //Function to display elements
of a sub-stack
{
int i;
if(top[stknum]==bndry[stknum]) //Check if sub-stack is empty
{
printf("Stack %d is empty!\n",stknum);
}
else
{
for(i=top[stknum]; i>=bndry[stknum]+1; i--)
printf("%d\n",stk[i]); //Display sub-stack elements
}
}
void main()
{ // Start of main()
int stk[MSS],bndry[NOS],top[NOS];
int choice,j,n,choice1,val,item;
printf("Enter the number of stacks to be created (<=5): ",NOS);
scanf("%d",&n);
for(j=0; j<n; j++) // Loop to determine top and boundary values
{
top[j]=MSS/n*j-1;
bndry[j]=MSS/n*j-1;
}
while(1)
{
printf("The Stacks formed are : \n");
for(j=0; j<n; j++)
printf("Stack %d\n",j);
printf("Enter the Stack Number for performing operations : ");
scanf("%d",&choice1);
if(choice1>n-1)
{ printf("No such stack available\n");
continue;
}
printf("Stack Menu:\n 1:Push\n 2:Pop\n 3:Display\n 4:Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice) // Choose operation to be performed
{
case 1:
printf("Enter item : ");
scanf("%d",&item);
push(item,choice1,top,bndry,stk); // Call push() function
break;
case 2:
val = pop(choice1,top,bndry,stk); // Call pop() function
if(val==-1)
{
printf("Stack %d is empty \n",choice1);
}
else
{
printf("The deleted item is : %d\n",val);
}
break;
case 3:
display(choice1,top,bndry,stk); // Call display() function
break;
case 4:
exit(0);
break;
default:
printf("\nInvalid Choice\n",val);
break;
}
}
} // End of main()

3. Program to demonstrate the queue operations by passing the parameters


//Program to implement queue by passing parameters
#include<stdio.h>
#include<stdlib.h>
#define qs 10 // qs is queue size
void Insert_rear(int item, int *rear, int q[]) //function to insert element from rear
{
if(*rear==qs-1) // Check overflow Condition
{
printf("Overflow!\n");
return;
}
*rear=*rear+1;
q[*rear]=item; // Element inserted in queue (rear end)
}
int Delete_front(int *front, int *rear, int q[]) //Function to delete element from
front
{
if(*front>*rear) //Check underflow Condition
return -1;
return q[(*front)++]; //Element deleted from queue (front end)
}
void Display(int front, int rear, int q[]) //Function to display items entered in the
queue
{
int j;
if(front>rear) //Check if queue is empty
{
printf("Queue is empty!\n");
return;
}
for(j=front; j<=rear; j++)
printf("%d ",q[j]); //Display queue elements
printf("\n");
}
void main()
{
int ch,item,front,rear,q[qs];
front=0;
rear=-1;
while(1)
{
printf("Queue Implementation : \n1. Insert \n2. Delete \n3. Display \n4.
Exit\n");
printf("Enter your choice : ");
scanf("%d", &ch);
switch(ch) //Select operation
{
case 1:
printf("Enter the item to be inserted : ");
scanf("%d",&item);
Insert_rear(item,&rear,q); //Call insert_rear function
break;
case 2:
item=Delete_front(&front,&rear,q); //Call Delete_front function
if(item==-1)
printf("Queue is empty! \n");
else
printf("Deleted item is : %d \n",item);
break;
case 3:
Display(front,rear,q); //Call Display function
break;
case 4:
exit(0);
default:
printf("Invalid Choice!!\n");
break;
}
}
}

4. Program to implement circular queue by passing the parameters


//Program to implement circular queue by passing parameters
#include<stdio.h>
#include<stdlib.h>
#define qs 5
void insert_queue(int item, int *rear, int *q, int *count) //Function to insert element
{
if(*count==qs) //Check overflow Condition
{
printf("Overflow!\n");
return;
}
*rear = (*rear+1) %qs; // Modulus operator for implementing circular queue
q[*rear] = item; // Element inserted in queue
(*count)++;
}
int delete_queue(int* front, int q[], int*count) //Function to delete element
{
int item;
if((*count)==0) //Check underflow Condition
return -1;
item=q[*front];
*front=(*front+1)%qs; // Modulus operator for implementing circular queue
*count=*count-1; // Element deleted in queue
return item;
}
void display(int front, int q[], int count) //Function to display queue
{
int i;
if(count==0) //Check if queue is empty
{
printf("Queue is empty!\n");
return;
}
for(i=1; i<=count; i++)
{
printf("%d ",q[front]); // Display items in queue
front=(front+1)%qs; // Modulus operator for implementing circular queue
}
}
int main()
{ // Start of main()
int rear=-1, front=0, q[qs], item, count=0;
int ch;
for(;;)
{
printf("\nCircular Queue Implementation : \n1. Insert \n2. Delete
\n3. Display \n4. Exit\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch) // Select operation
{
case 1:
printf("Enter element : ");
scanf("%d", &item);
insert_queue(item,&rear,q,&count); // Call insert_queue function
break;
case 2:
item=delete_queue(&front,q,&count); // Call delete_queue function
if(item==-1)
printf("Queue is Empty!");
else
printf("The deleted item is: %d\n",item);
break;
case 3:
display(front,q,count); // Call display function
break;
case 4:
exit(0);
default:
printf("Invalid Choice\n");
}
}
return 0;
} // End of main()

5. Program to implement Dequeue by passing the parameters


//Program to implement dequeue
#include<stdio.h>
#include<stdlib.h>
#define MQS 20
void insert_rear(int q[],int item,int * r) //Function to insert element from rear end
of the queue
{
if(*r==MQS-1) //Check overflow condition
{
printf("Overflow!\n");
}
else
{
printf("Enter the item to be inserted : ");
scanf("%d",&item);
*r=*r+1;
q[*r]=item; // Inserted element in queue from rear
}
}

void insert_front(int q[],int item,int *f, int *r) //Function to insert element from
front end of the queue
{
if(*f==0 & *r==-1)
{
*r=*r+1;
printf("Enter the item : ");
scanf("%d",&item);
q[*r]=item;
}
else if(*f!=0)
{
printf("Enter the item : ");
scanf("%d",&item);
*f=*f-1;
q[*f]=item;
}
else
{
printf("Insertion from front is not possible!\n");
}
}
void delete_front(int q[],int *f, int *r) //Function to delete element from front end
of the queue
{ int item;
if(*f>*r) //Underflow condition
{
printf("Queue is empty!\n");
}
else
{
item=q[*f];
*f=*f+1;
printf("Deleted item is %d\n",item);
}
}
void delete_rear(int q[],int *f, int *r) //Function to delete element from rear end of
the queue
{
if(*f>*r) //Underflow condition
printf("Queue is empty");
else
{
printf("Deleted item is %d\n",q[*r]);
*r=*r-1;
if(*f>*r)
{
*f=0;
*r=-1;
}
}
}
void display(int q[],int *f, int *r) //Function to display items in De-Queue
{ int i;
if(*f>*r) //Underflow condition
{
printf("Queue is empty!\n");
}
else
{
for(i=*f; i<=*r; i++)
{
printf("%d ",q[i]);
}
printf("\n");
}
}
int main()
{ int q[20],f=0,r=-1,item,i,ch;
while(1)
{
printf("De-Queue Implementation \n1.Insert from Rear \n2.Insert from
Front \n3.Delete from Front\n4.Delete from Rear\n5.Display\n6.Quit
\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_rear(q,item,&r);
break;
case 2:
insert_front(q,item,&f,&r);
break;
case 3:
delete_front(q,&f,&r);
break;
case 4:
delete_rear(q,&f,&r);
break;
case 5:
display(q,&f,&r);
break;
case 6:
exit(0);
break;
default:
printf("Wrong choice!\n");
break;
}
}
return 0;
}

2. Applications of Stacks and Queues


1. Find whether a given string is palindrome or not by using stack/queue
//C program to find whether a given string is palindrome or not
#include<stdio.h>
#define MSS 50 // MSS is maximum stack size
char stk[MSS];
int top=-1;
void push(char ele) //Function to insert element in stack
{
stk[top+1]=ele; // Element inserted
top++;
return;
}
char pop() //Function to pop element in stack
{
return stk[top--];
}
int main()
{ //Start of main()
int i,j;
char m,n,str[MSS];
printf("Enter the String (String Limit : %d characters): ",MSS-1);
scanf("%s",str);
for(i=0; str[i]!='\0'; i++) // Loop to insert elements of string in stack
{
m=str[i];
push(m);
}
for(j=0; j<i/2; j++) // Loop to check string is palindrome or not
{
n=pop();
if(n!=str[j])
{
printf("%s is not a Palindrome\n",stk);
return 0;
}
}
printf("%s is a Palindrome\n",stk);
} // End of main()

2. Find whether a given number is palindrome or not by using stack/queue


//C program to find whether a given number is palindrome or not
#include<stdio.h>
#define MSS 50 // Maximum Stack Size
int stk[MSS];
int top=-1;
void push(int ch) //Function to insert element from string into stack
{
stk[top+1]=ch; // Element inserted
top++;
return;
}
char pop() //Function to delete element from stack
{
return stk[top--];
}
int main()
{ // Start of main()
int i=0,j;
int n,m,num,num1,temp[MSS];
printf("Enter the Number : ");
scanf("%d",&num);
num1=num;
while(num1!=0)
{
n=num1%10;
push(n); // Call push function
temp[i]=n;
num1/=10;
i++;
}
for(j=0; j<i/2; j++) // Loop to check if string is palindrome
{
m=pop(); // Call pop function
if(m!=temp[j])
{
printf("%d is not a Palindrome\n",num);
return 0;
}
}
printf("%d is a Palindrome\n",num);
} // End of main()

3. Program to convert from infix expression into post-fix expression


//C program to convert infix-expression to post-fix expression
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MSS 25 // Maximum stack size
int sp(char symbol) //Function of stack precedence
{
switch(symbol)
{
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case '^':
case '$':
return 5;
case '(':
return 0;
case '#':
return -1;
default :
return 8;
}
}
int ip(char symbol) //Function of input precedence
{
switch(symbol)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 3;
case '^':
case '$':
return 6;
case '(':
return 9;
case ')':
return 0;
default :
return 7;
}
}
void infix_to_postfix(char infix[],char postfix[]) // Function to convert infix to
post-fix
{
int top, j, i;
char stk[MSS], symbol;
top=-1;
stk[++top]='#';
j=0;
for(i=0; i<strlen(infix); i++)
{
symbol=infix[i];
while(sp(stk[top])>ip(symbol))
postfix[j++]=stk[top--];
if(sp(stk[top]!=ip(symbol)))
stk[++top]=symbol;
else
top--;
}
while(stk[top]!='#')
postfix[j++]=stk[top--];
postfix[j]='\0';
}
int main()
{ // Start of main()
int i;
char infix[MSS], postfix[MSS];
printf("Enter an Infix Expression : ",MSS-1);
scanf("%s", infix);
infix_to_postfix(infix, postfix); // Call infix_to_postfix function
printf("The Postfix Expression is : ");
for(i=0; postfix[i]!='\0'; i++)
if(postfix[i]!='('&&postfix[i]!=')')
printf("%c",postfix[i]); // Display postfix expression
return 0;
} // End of main()

4. Program to convert from infix expression into prefix expression


// C program to convert infix expression to prefix expression
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MSS 25 // Maximum stack size
int sp(char symbol) // Function of stack precedence
{
switch(symbol)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 3;
case '^':
case '$':
return 6;
case')':
return 0;
case '#':
return -1;
default :
return 8;
}
}
int ip(char symbol) // Function of input precedence
{
switch(symbol)
{
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case '^':
case '$':
return 5;
case '(':
return 0;
case ')':
return 9;
default :
return 7;
}
}
void infix_to_prefix(char infix[],char prefix[]) // Function to convert infix to prefix
expression
{
int top, j, i;
char stk[MSS], symbol;
top=-1;
stk[++top]='#';
j=0;
strrev(infix);
for(i=0; i<strlen(infix); i++)
{
symbol=infix[i];
while(sp(stk[top])>ip(symbol))
prefix[j++]=stk[top--];
if(sp(stk[top]!=ip(symbol)))
stk[++top]=symbol;
else
top--;
}
while(stk[top]!='#')
prefix[j++]=stk[top--];
prefix[j]='\0';
strrev(prefix);
}
void main()
{ // Start of main()
int i;
char infix[MSS], prefix[MSS];
printf("Enter a valid Infix Expression ( String Limit : 24 characters ): ",MSS-1);
scanf("%s", infix);
infix_to_prefix(infix, prefix); // Call infix_to_prefix function
printf("The Prefix Expression is : ");
for(i=0; prefix[i]!='\0'; i++)
if(prefix[i]!='('&&prefix[i]!=')')
printf("%c",prefix[i]); // Display prefix expression
} // End of main()

5. Program to evaluate the post-fix expression


//C program to evaluate post-fix expressions
#include<stdio.h>
#include<math.h>
int stack[20];
int top=-1;
void push(int x) //Function to insert each digit from the given string into stack
{
stack[++top]=x; // Element inserted
}
int pop() //Function to delete elements from stack
{
return stack[top--];
}
int main()
{ // Start main()
char exp[20];
char *e;
int n1,n2,n3,num;
printf("Enter the expression :: ");
scanf("%s",exp);
e=exp;
while(*e!='\0')
{
if(isdigit(*e)) //isdigit is used to let only digits to pass to stack
{
num=*e-48; // 48(ASCII value) is subtracted to convert the values of
string to numbers
push(num); // Call push() function
}
else
{
n1=pop(); // Call pop() function
n2=pop();
switch(*e) // Select operation
{
case '+':
{
n3=n1+n2; //for addition
break;
}
case '-':
{
n3=n1-n2; //for subtraction
break;
}
case '*':
{
n3=n1*n2; //for multiplication
break;
}
case '/':
{
n3=n1/n2; //for division
break;
}
case '^':
{
n3=pow(n2,n1); //for exponent
break;
}
}
push(n3); // Call push() function
}
e++;
}
printf("\nThe result of the expression is %s = %d\n\n",exp,pop());
return 0;
} // End of main()

3. Singly Linked Lists


1. Write a C program to implement stack operations using singly linked lists.
//C program to implement stack using singly linked list
#include<stdio.h>
#include<stdlib.h>
struct node //self-referential structures
{
int info;
struct node *link;
};
struct node *START=NULL;
struct node* createNode() //Function to create new node
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node)); //Dynamic memory allocation
return(n);
};
void insertNode() //Function to insert new node from rear
{
struct node *temp,*t;
t=START;
temp=createNode();
printf("Enter a Number : ");
scanf("%d",&temp->info);
temp->link=NULL;
if(START==NULL) // Check if stack is empty
START=temp;
else
{
while(t->link!=NULL)
{
t=t->link;
}
t->link=temp;
}
}
void deleteNode() //Function to delete a node from rear
{
struct node *r,*p;
if(START==NULL) // Check if stack is empty
printf("List Is Empty\n");
else
{
r=START;
p=START;
while(r->link!=NULL)
{
p=r;
r=r->link;
}
p->link=NULL;
free(r); // Element deleted

}
}
void viewList() //Function to display the list
{
struct node *t;
if(START==NULL) // Check if stack is empty
{
printf("List Is Empty\n");
}
else
{
t=START;
while(t!=NULL)
{
printf("%d ",t->info); // Display elements in stack
t=t->link;
}
}
}
int menu()
{
int ch;
printf("\nStack Implementation Using Linked Lists");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Exit");
printf("\n\n Enter your choice : ");
scanf("%d",&ch);
return(ch);
}
int main()
{ // Start of main()
while(1)
{
switch(menu()) // Select operation
{
case 1:insertNode(); // Call insertNode() function
break;
case 2:deleteNode(); // Call deleteNode() function
break;
case 3:viewList(); // Call viewList() Function
break;
case 4:exit(0);
default:printf("invalid entry");
}
}
return 0;
} // End of main()

2. Write a C program to implement queue operations using singly linked lists.


//C program to implement queue using singly linked list
#include<stdio.h>
#include<stdlib.h>
struct node //self-referential structures
{
int info;
struct node *link;
};
struct node *START=NULL;
struct node* createNode() //Function to create new node
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node)); //Dynamic memory allocation
return(n);
};
void insertNode() //Function to insert new node from front
{
struct node *temp,*t;
t=START;
temp=createNode();
printf("Enter a Number : ");
scanf("%d",&temp->info);
temp->link=NULL;
if(START==NULL)
START=temp;
else
{
temp->link=START;
START=temp;
}

}
void deleteNode() //Function to delete a node from rear
{
struct node *r,*p;
if(START==NULL) // Check if list is empty
printf("List Is Empty\n");
else
{
r=START;
p=START;
while(r->link!=NULL)
{
p=r;
r=r->link;
}
p->link=NULL;
printf("The popped element is : %d",r->info);
free(r); // Element deleted
}
}
void viewList() //Function to display the list
{
struct node *t;
if(START==NULL) // Check if list is empty
{
printf("List Is Empty\n");
}
else
{
t=START;
while(t!=NULL)
{
printf("%d ",t->info);
t=t->link;
}
}
}
int menu()
{
int ch;
printf("\nQueue Implementation Using Linked Lists");
printf("\n1. Push");
printf("\n2. Pop");
printf("\n3. Display");
printf("\n4. Exit");
printf("\n\n Enter your choice : ");
scanf("%d",&ch);
return(ch);
}
int main()
{ // Start of main()
while(1)
{
switch(menu()) // Select operation
{
case 1:insertNode(); // Call insertNode() function
break;
case 2:deleteNode(); // Call deleteNode() function
break;
case 3:viewList(); // Call viewList() function
break;
case 4:exit(0);
default:printf("invalid entry");
}
}
return 0;
} // End of main()

3. Write a C program to implement De-queue operations using singly linked lists.


//C program to implement De-Queue using singly linked list
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
int item;
struct node //self-referential structures
{
int info;
struct node *link;
};
struct node *first;
struct node *getnode() //Function to create new node
{
struct node *n;
n=(struct node *)malloc(sizeof(struct node));
if(n==NULL) // Check if memory is full
{
printf("Out of memory");
exit(0);
}
return n;
}
struct node *insert_front(struct node *first,int item) //Function to insert node from
front
{
struct node * temp;
temp=getnode();
temp->info=item;
temp->link=first;
return temp;
}
struct node * insert_rear(struct node * first,int item) //Function to insert node from
rear
{
struct node * temp,*cur;
temp=getnode();
temp->info=item;
temp->link=NULL;
if(first==NULL)
return temp;
else
{
cur=first;
while(cur->link!=NULL)
cur=cur->link;
cur->link=temp;
return first;
}
}
struct node * display(struct node * first) //Function to display items in list
{
struct node * cur;
cur=first;
if(first==NULL)
{
printf("List is empty\n");
return NULL;
}
else
{
printf("The items are : \n");
while(cur->link!=NULL)
{
printf("%d\n",cur->info);
cur=cur->link;
}
printf("%d\n",cur->info);
return first;
}
}
struct node * delete_front(struct node * first) //Function to delete node from front
{
struct node * cur;
cur=first;
if(first==NULL) // Check if list is empty
{
printf("List is empty\n");
return NULL;
}
else
{
printf("Deleted element : %d\n",first->info);
cur=cur->link;
free(first); // Element deleted
return cur;
}
}
struct node *delete_rear(struct node * first) //Function to delete node from rear
{
struct node *cur,*prev;
cur=first;
prev=NULL;
if(first==NULL) // Check if list is empty
{
printf("The list is empty\n");
return NULL;
}
else if(first->link==NULL)
{
printf("Deleted Element : %d\n",first->info);
free(first); // Element deleted
return NULL;
}
else
{
while(cur->link!=NULL)
{
prev=cur;
cur=cur->link;
}
printf("The deleted item is : %d\n",cur->info);
free(cur); // Element deleted
prev->link=NULL;
return first;
}
}

int main()
{ // Start of main()
int ch;
while(1)
{
printf("De-Queue Implementation Using Linked Lists
:\n1.Enqueue from rear\n2.Enqueue from front\n3.
Dequeue from rear\n4.Dequeue from front\n5.Display
the queue\n6.Quit\n\nEnter your choice : ");
scanf("%d",&ch);
switch(ch) // Select operation
{
case 1:
printf("Insert the item you want to insert in the list : ");
scanf("%d",&item);
first=insert_rear(first,item); // Call insert_rear function
break;
case 2:
printf("Insert the item you want
to insert in the list : ");
scanf("%d",&item);
first=insert_front(first,item); // Call insert_front function
break;
case 3:
first=delete_rear(first); // Call delete_rear function
break;
case 4:
first=delete_front(first); // Call delete_front function
break;
case 5:
first=display(first); // Call display function
break;
case 6:
exit(0);
break;
default:
printf("\nInvalid Entry!!\n");
break;
}
}
return 0;
} // End of main()

4. Write C functions to perform the following operations on singly linked list


with a header node:
• Insert front
• Insert rear
• Delete front
• Delete rear
• Display

//C program to implement all functions of linked list including header node
#include<stdio.h>
#include<stdlib.h>
struct node //self-referential structures
{
int info;
struct node *link;
};
int item;
struct node *first=NULL;
struct node *header;
struct node *getnode() //Function to create new node
{
struct node *n;
n=(struct node *)malloc(sizeof(struct node)); // Dynamically memory allocation
if(n==NULL) // Check if memory is full
{
printf("Out of memory");
exit(0);
}
return n;
}
struct node *insert_front(struct node *header) //Function to insert node from front
{
int item;
struct node *temp;
temp=getnode();
printf("Enter The Number : ");
scanf("%d",&item);
temp->info=item;
first=header->link;
temp->link=first;
header->link=temp;
return header;
}
struct node *insert_rear(struct node *header) //Function to insert node from rear
{
struct node *temp,*cur;
temp=getnode();
printf("Enter The Number : ");
scanf("%d",&item);
temp->info=item;
temp->link=NULL;
if(header==NULL)
return temp;
else
{
cur=header;
while(cur->link!=NULL)
cur=cur->link;
cur->link=temp;
return header;
}
}
struct node *delete_front(struct node *header) //Function to delete node from front
{
struct node *first,*second;
if(header->link==NULL) // Check if list is empty
{
printf("The list is empty\n");
return NULL;
}
first=header->link;
second=first->link;
header->link=second;
printf("Item deleted is : %d\n",first->info);
free(first); // Element deleted
return header;
}
struct node *delete_rear(struct node *header) //Function to delete node from rear
{
struct node *cur,*prev;
if(header->link == NULL) // Check if list is empty
{
printf("The list is empty\n");
return header;
}
prev = header;
cur = header->link;
while(cur -> link != NULL)
{
prev = cur;
cur = cur ->link;
}
printf("The deleted item is : %d\n", cur-> info);
free(cur); // Element deleted
prev -> link = NULL;
return header;
}
struct node *display(struct node *header) //Function to display items in the list
{
struct node *cur;
cur=header->link;
if(header->link==NULL) // Check if list is empty
{
printf("The list is empty\n");
return NULL;
}
else
{
printf("The items in the list are : \n");
while(cur->link!=NULL)
{
printf("%d\n",cur->info);
cur=cur->link;
}
printf("%d\n",cur->info); // Display elements of list
return header;
}
}

int menu()
{
int ch;
printf("\n\n Singly Linked List Using Header Node");
printf("\n1. Insert From Front");
printf("\n2. Insert From Rear");
printf("\n3. Delete From Front");
printf("\n4. Delete From Rear");
printf("\n5. Display");
printf("\n6. Quit");
printf("\n\n Enter your choice : ");
scanf("%d",&ch);
return(ch);
}
int main()
{ // Start of main()
header=getnode();
header->link=first;
while(1)
{
switch(menu()) // Select operation
{
case 1:insert_front(header); // Call insert_front function
break;
case 2:insert_rear(header); // Call insert_rear function
break;
case 3:delete_front(header); // Call delete_front function
break;
case 4:delete_rear(header); // Call delete_rear function
break;
case 5:display(header); // Call display function
break;
case 6:exit(1);
default:printf("invalid entry");
}
}
return 0;
} // End of main()

5. Write a menu driven program for implementing the following in C.


• C function to concatenate two lists into a single list.
• C function to find the length of the singly linked list
• C function to reverse a given singly linked list without creating new nodes.
• C function to delete a node whose info field is specified.
• C function to insert a node whose info field is specified after specified node.
• C function to create an ordered linked list.

//Menu Driven C program


#include<stdio.h>
#include<stdlib.h>
int choice,i,ch;
struct node //self-referential structures
{
int info;
struct node *link;
};
struct node *START1=NULL;
struct node *START2=NULL;
struct node *START=NULL;
int count=0;
struct node* createNode() //Function to create new node
{
struct node *n;
n=(struct node*)malloc(sizeof(struct node)); // Dynamically Memory Allocation
return(n);
}
void insertNode1() //Function to insert new node in Linked list 1
{
struct node *temp,*t;
t=START1;
temp=createNode();
printf("Enter a Number : ");
scanf("%d",&temp->info);
temp->link=NULL;
if(START1==NULL)
START1=temp;
else
{
while(t->link!=NULL)
{
t=t->link;
}
t->link=temp;
}
}
void insertNode2() //Function to insert new node in Linked List 2
{
struct node *temp,*t;
t=START2;
temp=createNode();
printf("Enter a Number : ");
scanf("%d",&temp->info);
temp->link=NULL;
if(START2==NULL)
START2=temp;
else
{
while(t->link!=NULL)
{
t=t->link;
}
t->link=temp;
}
}
struct node *concatenate() //Function to concatenate 2 Linked Lists
{
struct node *t;
if(START1==NULL)
{
printf("The first linked list is empty");
return(START2);
}
if(START2==NULL)
{
printf("The second linked list is empty");
return(START1);
}
else
{
t=START1;
while(t->link!=NULL)
{
t=t->link;
}
t->link=START2;
return(START1);
}
}
void deleteNode() //Function to delete a node whose info part is specified
{
struct node *r,*p;
if(START1==NULL) // Check if list is empty
printf("List Is Empty\n");
else
{
int item;
r=START1;
p=START1;
printf("Enter an item : ");
scanf("%d",&item);
while(r->info!=item)
{
p=r;
r=r->link;
}
p->link=r->link;
free(r); // Element deleted

}
}
void viewList1() //Function to display items in Linked List 1
{
struct node *t;
if(START1==NULL) // Check if list is empty
{
printf("List Is Empty\n");
}
else
{
t=START1;
while(t!=NULL)
{
printf("%d ",t->info);
t=t->link;
}
}
}
void viewList2() //Function to display items in Ordered Linked List
{
struct node *t;
if(START==NULL) // Check if list is empty
{
printf("List Is Empty\n");
}
else
{
t=START;
while(t!=NULL)
{
printf("%d ",t->info);
t=t->link;
}
}
}
int length() //Function to calculate length of the linked list
{
struct node *t;
t=START1;
while(t->link!=NULL)
{
t=t->link;
count++;
}
return(count);
}
int reverseList() //Function to reverse the linked list
{
struct node *p,*t,*r;
p=t=NULL;
r=START1;
while(r!=NULL)
{
t=r->link;
r->link=p;
p=r;
r=t;
}
START1=p;
}
void insertNode3() //Function to insert a node whose info part is specified
{
struct node *temp,*t;
t=START1;
temp=createNode();
printf("Enter a Number To be Inserted : ");
scanf("%d",&temp->info);
temp->link=NULL;
if(START1==NULL) // Check if list is empty
printf("List is empty");
else
{
int item,flag=0;
printf("Enter the item (where to be inserted) : ");
scanf("%d",&item);
while(t->info!=item && flag==0)
{
t=t->link;
if(t->link==NULL && t->info!=item)
{
printf("\nThe item specified is not in list\n");
flag=1;
}
}
if(flag==0)
{
temp->link=t->link;
t->link=temp;
}
}
}
void InsertOrdered() //Function to create an ordered linked list
{
struct node *p,*temp;
temp=createNode();
printf(" Enter a number : ");
scanf("%d",&temp->info);
if(START==NULL || START->info >= temp->info)
{
temp->link=START;
START=temp;
}
else
{
struct node *t;
t=START;
while(t->link!=NULL && t->link->info < temp->info)
{
t=t->link;
}
temp->link=t->link;
t->link=temp;
}
}
int menu()
{
int ch;
printf("\n\n1. Insert Node");
printf("\n2. Concatenate");
printf("\n3. Length Of Linked List");
printf("\n4. Reverse List");
printf("\n5. Delete A Node (Specify Info Part)");
printf("\n6. Insert A Node (Specify Info Part)");
printf("\n7. Ordered Linked List");
printf("\n8. Display");
printf("\n9. Quit");
printf("\n\n Enter your choice : ");
scanf("%d",&ch);
return(ch);
}
int main()
{ // Start of main()
int k;
while(1)
{
switch(menu()) //Select operations
{
case 1:insertNode1(); // Call insertNode1() function
break;
case 2:
while(1)
{
printf("1. Insert Node On 2nd Linked List\n");
printf("2. Concatenate\n");
printf("\nEnter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:insertNode2(); // Call insertNode2() function
break;
case 2:concatenate(); // Call concatenate() function
i=2;
break;
}
if(i==2)
break;
}
break;
case 3:printf("%d",length()+1); // Call length() function
break;
case 4:reverseList(); // Call reverseList() function
break;
case 5:deleteNode(); // Call deleteNode() function
break;
case 6:insertNode3(); // Call insertNode3() function
break;
case 7:InsertOrdered(); // Call InsertOrdered() function
break;
case 8:
printf("\nChoice\n1. Display 1st Linked List\n2. Display Ordered
Linked List\n");
scanf("%d",&k);
if(k==1)
viewList1(); // Call viewList1() function
else
viewList2(); // Call viewList2() function
break;
case 9:exit(0);
break;
default:printf("invalid entry");
}
}
return 0;
} // End of main()

Вам также может понравиться