0% found this document useful (0 votes)
92 views15 pages

Long Assignment

1. The document provides code for implementing various operations on a dynamic queue using a doubly linked list. 2. Functions are defined for insertion at the beginning, end, and middle of the queue as well as deletion from the beginning and end. 3. A menu-driven program is created to allow the user to select these queue operations and test the implementation.

Uploaded by

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

Long Assignment

1. The document provides code for implementing various operations on a dynamic queue using a doubly linked list. 2. Functions are defined for insertion at the beginning, end, and middle of the queue as well as deletion from the beginning and end. 3. A menu-driven program is created to allow the user to select these queue operations and test the implementation.

Uploaded by

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

Q#1: Write an algorithm printf("2.

dequeue value\n");
REVERSEQ that will printf("3.dispaly\n");
reverse all the elements in
a circular queue printf("4.exit\n");

implemented using an printf("enter choice: ");


array. Assume that the
scanf("%d",&c);
ADT and helper operations
for circular queue are if(c==1){
already available. if((front==rear+1) ||
(front==0 && rear==size-1))
#include<Stdio.h> printf("overflow");
#define size 5 else if(front==-1 && rear==-1){
void pus(int); for(i=0;i<size;i++){
void enqueue(int); printf("enter value no%d: ",i+1);
void dequeue(); scanf("%d",&no);
int queue[size]; enqueue(no);
int stack[size]; }
int top=-1; }
int rear=-1; else if(front<rear && rear!=size-
int front=-1; 1){

void main(){ for(i=rear;i<size-1;i++){

int i,no,c; printf("enter value no%d: ",i+1);

char choice='y'; scanf("%d",&no);

while(1){ enqueue(no);

printf("\n1.enqueue value\n"); }
} }

else if(rear==size-1){ else if(front>rear){

rear=-1; for(i=front;i<size;i++){

for(i=0;i<front;i++){ push(i);

printf("enter value no%d: ",i+1); }

scanf("%d",&no); for(i=0;i<=rear;i++){

enqueue(no); push(i);

} }

} }

else if(rear<front && rear!=size- printf("value in stack in reverse


1){ order: \n");

for(i=rear;i<front-1;i++){ for(i=top;i>=0;i--)

printf("enter value no%d: ",i+1); printf("%d ",stack[i]);

scanf("%d",&no); top=-1;

enqueue(no); }

} if(c==2){

} while(choice!='n'){

} if(front==-1){

if(c==3){ printf("queue is empty


first enquque: ");
if(front==-1)
break;
printf("first enqueue the
value: "); }

else if(front<=rear){ printf("\ndo you want to


delete a no(y/n): ");
for(i=front;i<=rear;i++){
choice=getche();
push(i);
if(front<rear && choice=='y'){
}
dequeue(); }

} void enqueue(int no){

else if(front>rear && if(front==-1)


choice=='y' && front!=size-1){
front++;
dequeue();
rear++;
}
queue[rear]=no;
else if(front==size-1 &&
}
choice=='y' && front!=rear){
void dequeue(){
dequeue();
front++;
front=0;
}
}
push(int i){
else if(front==rear &&
choice=='y'){ top++;
dequeue(); stack[top]=queue[i];
printf("\nunderflow\n"); }
front=-1;
Q#2: It is required to split a
rear=-1;
queue into two queues so
break; that all the elements in
} odd positions are in one
queue and those in even
}
positions are in another
} queue. Write a program
if(c==4) SPLITQ() to accomplish
this. Assume that the
exit(0); queue is maintained in an
choice='y'; array.

}
#include<Stdio.h>
getche();
#define size 11
void enqueue(int); }

void enqueue1(int); ///////////////////////////////////

void enqueue2(int); printf("\neven no queue: \n");

int queue[size]; for(i=front1;i<=rear1;i++)

int queue1[size]; printf("%d ",queue1[i]);

int queue2[size]; printf("\nodd no queue: \n");

int front=-1; for(i=front2;i<=rear2;i++)

int rear=-1; printf("%d ",queue2[i]);

int front1=-1; }

int rear1=-1; void enqueue(int value){

int front2=-1; if(front==-1)

int rear2=-1; front++;

void main(){ rear++;

int i,value; queue[rear]=value;

for(i=0;i<size;i++){ }

printf("enter no%d value: ",i); void enqueue1(int i){

scanf("%d",&value); if(front1==-1)

enqueue(value); front1++;

} rear1++;

//////////////////////////////////// queue1[rear1]=queue[i];

for(i=front;i<=rear;i++){ }

if(i%2==0){ void enqueue2(int i){

enqueue1(i); if(front2==-1)

} front2++;

else rear2++;

enqueue2(i); queue2[rear2]=queue[i];
} if(flag1==1)

printf("0.goto next menu or select any


Q#3: Implement dynamic above choice\n");
queue using a double printf("select choice: ");
ended singly-linked list.
scanf("%d",&choice);

if(choice==1){
#include<Stdio.h>
flag1=1;
#include<stdlib.h>
while(c!='n'){
void atend(int);
printf("\nenter a number: ");
void insertatbeg(int);
scanf("%d",&value);
void middleinsert(int);
atend(value);
void deleteatend();
printf("do you want to enter another
void deleteatstart();
value(y/n):");
void print();
c=getche();
struct node{
}
int data;
c='y';
struct node *next;
}
}*var,*head,*temp,*get,*temp1;
if(choice==2){
int main(){
flag1=1;
int choice,choice1,choice2;
while(c!='n'){
int value,loc;
printf("\nenter a value: ");
int flag=0;
scanf("%d",&value);
char c='y';
insertatbeg(value);
int flag1=0;
printf("do you want to insert another
while(1){ integer(y/n): ");

printf("\n1.insertion at ending\n"); c=getche();

printf("2.insertion at beginning\n"); }
c='y'; printf("\nenter a number: ");

} scanf("%d",&value);

printf("\n****************************** middleinsert(value);
**************\n");
flag=1;
printf("3.insertion in the middle\n");
if(flag==1)
printf("0.goto next menu or select any
above choice\n"); break;

printf("\n****************************** }
**************\n"); }
printf("select choice: "); if(flag==1)
scanf("%d",&choice); printf("\nnode successfully
if(choice==3){ inserted:\n");

get=head; if(flag==0)

printf("\nenter the value store in a printf("\nlocation not found:\n");


node to insert another node atfront: }
");
printf("********************************
scanf("%d",&loc); ***********\n");
while(1){ printf("4.deletion at ending\n");
if(get->data!=loc){ printf("5.deletion at beginning\n");
get=get->next; printf("0.goto next menu or select any
if(get==NULL) above choice\n");

break; printf("select choice: ");

} scanf("%d",&choice1);

else{ printf("\n******************************
*************\n");
if(get->next==NULL){
if(choice1==4){
printf("\nadd node in middle
not at end:\n"); while(c!='n'){

break; get=head;

} deleteatend();
printf("\ndo you want to delete }
another integer(y/n): ");
}
c=getche();
void atend(int value){
}
var=(struct node*)malloc(sizeof(struct
c='y'; node));

} var->data=value;

if(choice1==5){ if(head==NULL){

while(c!='n'){ var->next=NULL;

deleteatstart(); head=var;

printf("\ndo you want to delete temp=head;


another integer(y/n): ");
}
c=getche();
else{
}
var->next=NULL;
c='y';
temp->next=var;
}
temp=var;
printf("\n*********\n");
}
printf("6.display\n");
}
printf("7.exit\n");
void insertatbeg(int value){
printf("select choice: ");
var=(struct node*)malloc(sizeof(struct
scanf("%d",&choice2); node));

if(choice2==6 && (choice==0 || var->data=value;


choice==3 || choice1==4 ||
choice1==5)){ if(head==NULL){

temp1=head; var->next=NULL;

print(); head=var;

} temp=head;

if(choice2==7) }

exit(0); else{
var->next=head; get->next=NULL;

head=var; temp=get;

} printf("\nnode deleted
successfully:\n");
}
}
void print(){
deleteatstart(){
printf("\nvalue store in linklist:\n");
head=head->next;
while(temp1!=NULL){
printf("\nnode deleted at start:\n");
printf("%d\n",temp1->data);
if(head==NULL){
temp1=temp1->next;
printf("\nlinklist is empty: \n");
}
}
printf("pointer point at the end of
link list"); }

}
Q#4: Write the following
void middleinsert(int value){
algorithms for the deque
var=(struct node*)malloc(sizeof(struct [double ended queue]
node));
using circular array.
var->data=value;

var->next=get->next; #include<Stdio.h>

get->next=var; #define size 5

} void Insert_dq(int);

deleteatend(){ void Del_dq();

if(get==temp){ int deque[size];

printf("\nlinklist is empty: \n"); int front=-1;

} int rear=-1;

while(get->next!=temp){ int side=0;

get=get->next; void main(){

} int value,i,c;
char choice='y'; front=-1;

while(1){ rear=-1;

printf("\n1.enqueue\n"); }

printf("2.dequeue\n"); else{

printf("3.display\n"); while(choice!='n'){

printf("4.exit\n"); printf("\ndo you want to delete


another value(y/n): ");
printf("enter choice: ");
choice=getche();
scanf("%d",&c);
if(choice=='y')
if(c==1){
Del_dq();
if(rear==size-1)
if(front==rear+1){
printf("overflow");
printf("\nunderflow\n");
else{
front=-1;
for(i=(rear+1);i<size;i++){
rear=-1;
printf("enter no%d to insert: ",i+1);
choice='n';
scanf("%d",&value);
}
Insert_dq(value);
}
}
}
}
}
}
}
if(c==2){
if(c==3){
if(rear==-1)
if(rear==-1)
printf("first enqueue the value: ");
printf("first enqueue the value:
else{ ");
Del_dq(); else{
if(front==rear+1){ for(i=front;i<=rear;i++)
printf("\nunderflow\n"); printf("%d ",deque[i]);
} void print();
void searching(int);
} struct node{
int data;
if(c==4){
struct node *next;
exit(0); }*var,*head,*temp,*get,*storage,*sorting;
int main(){
} int loc;
int value;
choice='y'; char choice='y';
while(choice!='n'){
} printf("\nenter a number: ");
scanf("%d",&value);
}
atend(value);
void Insert_dq(value){ printf("do you want to enter another
value(y/n):");
if(front==-1) choice=getche();
}
front++; sorted();
get=head;
rear++; print();
deque[rear]=value;
searching(loc);
}
} void atend(int value){
var=(struct node*)malloc(sizeof(struct
void Del_dq(){ node));
var->data=value;
rear--; if(head==NULL){
var->next=NULL;
}
head=var;
temp=head;
}
Q#5: Perform the else{
following tasks on linked var->next=NULL;
lists using single linked list temp->next=var;
temp=var;
representation: }
a) A list containing unsorted data is }
known, make it as a sorted list. void sorted(){
b) Develop a function Search to find a get=head->next;
specific element in a singly Linked sorting=head;
list. while(1){
while(get!=NULL){
#include<Stdio.h> if(sorting->data>get->data){
#include<stdlib.h> storage=sorting->data;
void atend(int); sorting->data=get->data;
void sorted();
get->data=storage;
get=get->next;
}
else Q#6: Given two sorted
get=get->next;
} linked lists, L1 and L2,
sorting=sorting->next; write a routine to compute
get=sorting->next; L1 L2 using only the
if(get==NULL)
break; basic list operations.
}
}
#include<Stdio.h>
void print(){
printf("\nvalue store in linklist in sorted #include<stdlib.h>
order:\n");
while(get!=NULL){ void atend(int);
printf("%d\n",get->data);
get=get->next; void sorted();
}
printf("pointer point at the end of link void print();
list");
void atend1(int);
}
searching(int loc){ void sorted1();
int flag=0;
int count=1; void print1();
get=head;
printf("\nenter a value you want search in a void printed();
node: ");
scanf("%d",&loc); //////////////////////////////////////////////////////////
while(get!=NULL){ //////
if(get->data!=loc){
struct node{
count++;
get=get->next; int data;
}
else{ struct node *next;
printf("value found %d at node
%d",get->data,count); }
flag=1; *var,*head,*temp,*get,*storage,*sortin
break; g;
}
} //////////////////////////////////////////////////////////
if(flag==0) //////
printf("\nvalue not found: ");
} struct node1{

int data1;
struct node1 *next1; printf("do you want to enter another
value(y/n):");
}
*var1,*head1,*temp1,*get1,*storage1, choice1=getche();
*sorting1;
}
//////////////////////////////////////////////////////////
////// sorted1();

int main(){ get1=head1;

int value; print1();

char choice='y'; ///////////////////////////

while(choice!='n'){ get=head;

printf("\nenter a number store in get1=head1;


first link list: "); printed();
scanf("%d",&value); }
atend(value); void atend(int value){
printf("do you want to enter another var=(struct node*)malloc(sizeof(struct
value(y/n):"); node));
choice=getche(); var->data=value;
} if(head==NULL){
sorted(); var->next=NULL;
get=head; head=var;
print(); temp=head;
////////////////////////// }
int value1; else{
char choice1='y'; var->next=NULL;
while(choice1!='n'){ temp->next=var;
printf("\nenter a number store in temp=var;
second link list: ");
}
scanf("%d",&value1);
}
atend1(value1);
void sorted(){ head1=var1;

get=head->next; temp1=head1;

sorting=head; }

while(1){ else{

while(get!=NULL){ var1->next1=NULL;

if(sorting->data>get->data){ temp1->next1=var1;

storage=sorting->data; temp1=var1;

sorting->data=get->data; }

get->data=storage; }

get=get->next; void sorted1(){

} get1=head1->next1;

else sorting1=head1;

get=get->next; while(1){

} while(get1!=NULL){

sorting=sorting->next; if(sorting1->data1>get1->data1){

get=sorting->next; storage1=sorting1->data1;

if(get==NULL) sorting1->data1=get1->data1;

break; get1->data1=storage1;

} get1=get1->next1;

} }

//////////////////////////////////////////// else

void atend1(int value1){ get1=get1->next1;

var1=(struct }
node1*)malloc(sizeof(struct node1));
sorting1=sorting1->next1;
var1->data1=value1;
get1=sorting1->next1;
if(head1==NULL){
if(get1==NULL)
var1->next1=NULL;
break; while(get1!=NULL){

} if(get->data==get1->data1){

} printf("%d\n",get->data);

void print(){ flag=1;

printf("\nvalue store in first linklist }


in sorted order:\n");
get1=get1->next1;
while(get!=NULL){
}
printf("%d\n",get->data);
get=get->next;
get=get->next;
if(get==NULL){
}
if(flag==0)
printf("pointer point at the end of
first linklist"); printf("\nthere is no common
element in both lists:\n");
}
break;
void print1(){
}
printf("\nvalue store in second
linklist in sorted order:\n"); get1=head1;

while(get1!=NULL){ }

printf("%d\n",get1->data1); }

get1=get1->next1;
Q#7: Write a function
}
reverse_print which prints
printf("pointer point at the end of the elements of a linked
second linklist"); list in reverse order.
}

void printed(){ #include<Stdio.h>

int flag=0; #include<stdlib.h>

printf("\ncommom elements in both void atend(int);


lists are:\n"); void print();
while(1){ void reverse_print();
struct node{ temp=head;

int data; }

struct node *next; else{

}*var,*head,*temp,*temp1,*temp2; var->next=NULL;

int main(){ temp->next=var;

int value; temp=var;

char choice='y'; }

while(choice!='n'){ }

printf("\nenter a number: "); void reverse_print(){

scanf("%d",&value); printf("\n");

atend(value); printf("value in reverse order: \n");

printf("do you want to enter another temp1=temp;


value(y/n):");
temp2=head;
choice=getche();
while(temp1!=head){
}
while(temp2->next!=temp1){
reverse_print();
temp2=temp2->next;
}
}
void atend(int value){
printf("%d\n",temp1->data);
var=(struct node*)malloc(sizeof(struct
node)); temp1=temp2;

var->data=value; temp2=head;

if(head==NULL){ }

var->next=NULL; printf("%d\n",temp1->data);

head=var; }

You might also like