Академический Документы
Профессиональный Документы
Культура Документы
#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
}
}
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);
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;
}
}
}
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()
}
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()
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()
//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()
}
}
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()