double linked list.docx
TRANSCRIPT
-
7/29/2019 Double Linked List.docx
1/10
Double Linked List#include
#include
#include
#include
#define N 100
struct dlinklist
{
struct dlinklist *prev; /** Stores address of previous node **/
int roll_no; /** stores roll number **/
char name[N]; /** stores Name **/
float marks; /** stores Marks **/
struct dlinklist *next; /** stores address of next node **/
};
/** Redefining dlinklist as node **/
typedefstruct dlinklist node;
void init(node*); /** Input function **/
void ins_aft(node*); /** Function inserting before **/
node* ins_bef(node*); /** Function inserting after **/
node* del(node*); /** Function deleting a node **/
void search(node*); /** Function for searching node **/
void disp(node*); /** Function for displaying node **/
void rollsrch(node*); /** Function for searching node by roll number **/
void namesrch(node*); /** Function for searching node by name **/
void marksrch(node*); /** Function for searching node by marks **/
void main()
{node *head;
char ch; /* Choice inputing varible */
int opt; /* Option inputing variable*/
staticint flag=0; /* Unchanged after iniialization */
clrscr();
head=(node*)malloc(sizeof(node));
head->next=NULL;
head->prev=NULL;
do
{
again:
printf("\nEnter your option\n");
printf("\n1. Initialize the node\n");
printf("\n2. Insert before a specified node\n");
printf("\n3. Insert after a specified node\n");
printf("\n4. Delete a particular node\n");
printf("\n5. Search the nodes\n");
printf("\n6. Display all the nodes\n");
scanf("%d",&opt);
if(flag==0 && opt!=1)
http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html -
7/29/2019 Double Linked List.docx
2/10
{
printf("\nNo. You must first initialize at least one node\n");
goto again;
}
if(flag==1 && opt==1)
{
printf("\nInitialisation can occur only once.\n");printf("\nNow you can insert a node\n");
goto again;
}
if(opt==4 && head->next==NULL)
{
printf("\nYou cannot delete the one and only the single node\n");
goto again;
}
if(flag==0 && opt==1)
flag=1;
switch(opt)
{
case1:
init(head);
break;
case2:
head=ins_bef(head);
break;
case3:
ins_aft(head);
break;
case4:
head=del(head);
break;
case5:
search(head);
break;
case6:
disp(head);
break;
}
printf("\nDo you wish to continue[y/n]\n");
ch=getche();
}while(ch=='Y' || ch=='y');
printf("\nDone by \"SHABBIR\"\n");
printf("\nPress any key to exit\n");
getch();}
void init(node *current)
{
current->prev=NULL;
printf("\nEnter Roll number\n");
scanf("%d",¤t->roll_no);
printf("\nEnter the name\n");
http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html -
7/29/2019 Double Linked List.docx
3/10
fflush(stdin);
gets(current->name);
printf("\nEnter the marks\n");
scanf("%f",¤t->marks);
current->next=NULL;
}
void ins_aft(node *current)
{
int rno; /* Roll number for inserting a node*/
int flag=0;
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("\nEnter the roll number after which you want to insert a node\n");
scanf("%d",&rno);
init(newnode);
while(current->next!=NULL)
{
/*** Insertion checking for all nodes except last ***/
if(current->roll_no==rno)
{
newnode->next=current->next;
current->next->prev=newnode;
current->next=newnode;
newnode->prev=current;
flag=1;
}
current=current->next;
}
if(flag==0 && current->next==NULL && current->roll_no==rno)
{
/*** Insertion checking for last nodes ***/
newnode->next=current->next;
current->next=newnode;
flag=1;
}
elseif(flag==0 && current->next==NULL)
printf("\nNo match found\n");
}
node* ins_bef(node *current)
{
int rno; /* Roll number for inserting a node*/
node *newnode,*temp;newnode=(node*)malloc(sizeof(node));
printf("\nEnter the roll number before which you want to insert a node\n");
scanf("%d",&rno);
init(newnode);
if(current->roll_no==rno)
{
/*** Insertion checking for first node ***/
newnode->next=current;
http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html -
7/29/2019 Double Linked List.docx
4/10
current->prev=newnode;
current=newnode;
return(current);
}
temp=current;
while(temp->next!=NULL)
{/*** Insertion checking for all node except first ***/
if(temp->next->roll_no==rno)
{
newnode->next=temp->next;
temp->next->prev=newnode;
temp->next=newnode;
newnode->prev=temp;
return(current);
}
temp=temp->next;
}
/*
If the function does not return from any return statement.
There is no match to insert before the input roll number.
*/
printf("\nMatch not found\n");
return(current);
}
node* del(node *current)
{
int rno; /* Roll number for deleting a node*/
node *newnode,*temp;
printf("\nEnter the roll number whose node you want to delete\n");
scanf("%d",&rno);
newnode=current;
if(current->roll_no==rno)
{
/*** Checking condition for deletion of first node ***/
newnode=current; /* Unnecessary step */
current=current->next;
current->prev=NULL;
free(newnode);
return(current);
}
else
{while(newnode->next->next!=NULL)
{
/*** Checking condition for deletion of ***/
/*** all nodes except first and last node ***/
if(current->next->roll_no==rno)
{
newnode=current;
temp=current->next;
http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html -
7/29/2019 Double Linked List.docx
5/10
newnode->next=newnode->next->next;
newnode->next->prev=current;
free(temp);
return(current);
}
newnode=newnode->next;
}if(newnode->next->next==NULL && newnode->next->roll_no==rno)
{
/*** Checking condition for deletion of last node ***/
temp=newnode->next;
free(temp);
newnode->next=NULL;
return(current);
}
}
printf("\nMatch not found\n");
return(current);
}
void search(node *current)
{
int ch; /* Choice inputing variable */
printf("\nEnter the criteria for search\n");
printf("\n1. Roll number\n");
printf("\n2. Name\n");
printf("\n3. Marks\n");
scanf("%d",&ch);
switch(ch)
{
case1:
rollsrch(current);break;
case2:
namesrch(current);
break;
case3:
marksrch(current);
break;
default:
rollsrch(current);
}
}
void rollsrch(node *current)
{
int rno;
printf("\nEnter the roll number to search\n");
scanf("%d",&rno);
while(current->next!=NULL)
{
if(current->roll_no==rno)
http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html -
7/29/2019 Double Linked List.docx
6/10
printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current-
>marks);
current=current->next;
}
if(current->next==NULL && current->roll_no==rno)
printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
}
void namesrch(node *current)
{
char arr[20];
printf("\nEnter the name to search\n");
fflush(stdin);
gets(arr);
while(current->next!=NULL)
{
if(strcmp(current->name,arr)==NULL)
printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current-
>marks);
current=current->next;
}
if(current->next==NULL && strcmp(current->name,arr)==NULL)
printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
}
void marksrch(node *current)
{
float marks;
printf("\nEnter the marks to search\n");
scanf("%f",&marks);
while(current->next!=NULL)
{
if(current->marks==marks)
printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current-
>marks);
current=current->next;
}
if(current->next==NULL && current->marks==marks)
printf("\n%d\t%s\t%f\n",current->roll_no,current->name,current->marks);
}
void disp(node *current)
{
while(current!=NULL){
printf("\n%d\t%s\t%f",current->roll_no,current->name,current->marks);
current=current->next;
}
}
http://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.htmlhttp://www.opengroup.org/onlinepubs/009695399/functions/printf.html -
7/29/2019 Double Linked List.docx
7/10
#include #include structnode{
structnode *prev;intinfo;structnode *next;
}*start;main(){
intchoice,n,m,po,i;start=NULL;while(1){
printf("1.Create List\n");printf("2.Add at begining\n");printf("3.Add after\n");printf("4.Delete\n");printf("5.Display\n");
printf("6.Count\n");printf("7.Reverse\n");printf("8.exit\n");printf("Enter your choice : ");scanf("%d",&choice);switch(choice){case1:
printf("How many nodes you want : ");scanf("%d",&n);for(i=0;i
-
7/29/2019 Double Linked List.docx
8/10
display();break;
case6:count();break;
case7:rev();break;
case8:exit();
default:printf("Wrong choice\n");
}/*End of switch*/}/*End of while*/
}/*End of main()*/create_list(intnum){
structnode *q,*tmp;tmp= malloc(sizeof(structnode));tmp->info=num;
tmp->next=NULL;if(start==NULL){
tmp->prev=NULL;start->prev=tmp;start=tmp;
}else{
q=start;while(q->next!=NULL)
q=q->next;q->next=tmp;tmp->prev=q;
}}/*End of create_list()*/addatbeg(intnum){
structnode *tmp;tmp=malloc(sizeof(structnode));tmp->prev=NULL;tmp->info=num;tmp->next=start;start->prev=tmp;start=tmp;
}/*End of addatbeg()*/
addafter(intnum,intc){
structnode *tmp,*q;inti;q=start;for(i=0;inext;
-
7/29/2019 Double Linked List.docx
9/10
if(q==NULL){
printf("There are less than %d elements\n",c);return;
}}tmp=malloc(sizeof(structnode) );tmp->info=num;q->next->prev=tmp;tmp->next=q->next;tmp->prev=q;q->next=tmp;
}/*End of addafter() */del(intnum){
structnode *tmp,*q;if(start->info==num){
tmp=start;start=start->next; /*first element deleted*/
start->prev = NULL;free(tmp);return;
}q=start;while(q->next->next!=NULL){
if(q->next->info==num) /*Element deleted in between*/{
tmp=q->next;q->next=tmp->next;tmp->next->prev=q;free(tmp);return;
}q=q->next;
}if(q->next->info==num) /*last element deleted*/{ tmp=q->next;
free(tmp);q->next=NULL;return;
}printf("Element %d not found\n",num);
}/*End of del()*/
display()
{structnode *q;if(start==NULL){
printf("List is empty\n");return;
}q=start;printf("List is :\n");
-
7/29/2019 Double Linked List.docx
10/10
while(q!=NULL){
printf("%d ", q->info);q=q->next;
}printf("\n");
}/*End of display() */count(){ structnode *q=start;
intcnt=0;while(q!=NULL){
q=q->next;cnt++;
}printf("Number of elements are %d\n",cnt);
}/*End of count()*/rev(){
structnode *p1,*p2;p1=start;p2=p1->next;p1->next=NULL;p1->prev=p2;while(p2!=NULL){
p2->prev=p2->next;p2->next=p1;p1=p2;p2=p2->prev; /*next of p2 changed to prev */
}start=p1;
}/*End of rev()*/