Data Structure Problems :
Singly Linked List :
Singly linked list has following features .
It contains int data value and next pointer which points to same kind of linked list node.
It is an extensive use of Dynamic Memory Allocation & structure.
Structure defined in following way :
struct snode{
int val;
struct snode *next;
};
Declaring a snode variable
struct snode node;
node has following variable to access
val and ptr
A C program on singly linked list.
functions used :
int create_snode(int val,struct snode **node); // To create node
int insert_snode(struct snode **head,struct snode **node );// To insert node
struct snode *find_snode(struct snode **head,int val); // To find node
int delete_snode(struct snode **head,struct snode **node); // To delete node from list
void print_sll(struct snode **head); // To print list
void reverse_sll(struct snode **head); // To reverse list
struct snode* findnthnode(struct snode **head,int lpo);// To find nth node from last in list
Code :
/* Singly Linked List Code by Codingstreet.com */
/* Following Features are provided */
/*
Singly Linked List Menu :
[1] Insert Node:
[2] Delete Node:
[3] Print Node:
[4] Reverse List:
[5] Find Nth node from Last :
[6] Exit:
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
/* Singly Linked List code by Codingstreet.com */ #include<stdio.h> #include<stdlib.h> struct snode{ int val; struct snode *next; }; int create_snode(int val,struct snode **node){ struct snode *tmp; tmp=malloc(sizeof(struct snode)); if(tmp==NULL) return 0; tmp->val=val; tmp->next=NULL; *node=tmp; return 1; } int insert_snode(struct snode **head,struct snode **node ){ if((*head)==NULL){ (*head)=(*node); (*head)->next=NULL; } else { struct snode *root; root=(*head); struct snode *next,*prev,*tmp; next=root->next; prev=tmp=root; while(next!=NULL){ prev=tmp; tmp=tmp->next; next=tmp; } prev->next=(*node); next=prev->next; next->next=NULL; } return 1; } struct snode *find_snode(struct snode **head,int val){ struct snode *root; root=*head; if(root==NULL){ return NULL; } else if(root->val==val){ return root; } else { struct snode *tmp; tmp=root; while(tmp!=NULL){ if(tmp->val==val){ return tmp; } tmp=tmp->next; } return tmp; } } int delete_snode(struct snode **head,struct snode **node){ struct snode *root,*tmp,*next,*prev,*dlnode; root=*head; dlnode=*node; if(root==NULL){ return 0; } else if(root==dlnode){ next=root->next; root->next=NULL; free(root); root=next; *head=root; return 1; } else { tmp=*head; next=tmp->next; prev=tmp; while(tmp!=NULL){ if(tmp==dlnode){ prev->next=tmp->next; tmp->next=NULL; free(tmp); return 1; } prev=tmp; tmp=tmp->next; } return 0; } } void print_sll(struct snode **head){ struct snode *tmp; tmp=(*head); while(tmp!=NULL){ printf(" %d ",tmp->val); tmp=tmp->next; } } void reverse_sll(struct snode **head){ if((*head)==NULL) return; if((*head)->next==NULL) return; struct snode *next,*prev,*curnt; curnt=(*head)->next; prev=*head; prev->next=NULL; next=curnt->next; while(next!=NULL){ curnt->next=prev; prev=curnt; curnt=next; next=next->next; } curnt->next=prev; (*head)=curnt; } struct snode* findnthnode(struct snode **head,int lpo){ struct snode *tmp=(*head),*ptmp; ptmp=(*head); int i=0; while(tmp!=NULL){ i++; tmp=tmp->next; if(i>lpo) ptmp=ptmp->next; } if(lpo>i) return NULL; return ptmp; } int main(){ struct snode *head=NULL,*node=NULL; int val,ival; do { printf("\n Singly Linked List Menu : \n"); printf("[1] Insert Node: \n"); printf("[2] Delete Node: \n"); printf("[3] Print Node: \n"); printf("[4] Reverse List:\n"); printf("[5] Find Nth node from Last : \n"); printf("[6] Exit: \n"); printf("Enter the input:"); scanf("%d",&val); switch(val){ case 1: printf("\n Enter the Val :"); scanf("%d",&ival); if(create_snode(ival,&node)){ insert_snode(&head,&node); } else printf("!!!@@@ System is outof Memory @@@!!!\n"); break; case 2: printf("\n Delete the Val :"); scanf("%d",&ival); node=find_snode(&head,ival); if(node!=NULL){ delete_snode(&head,&node); } else printf(" The Node Value not found \n"); break; case 3: printf("\n The Singly List is : \n"); print_sll(&head); break; case 4: reverse_sll(&head); break; case 5: node=NULL; printf("\n Enter the position fro last : "); scanf("%d",&ival); node=findnthnode(&head,ival); if(node==NULL) printf("\n Wrong Input position "); else printf("\n %d ",node->val); break; case 6: break; default: printf("===> [ Enter the Correct input] <==== \n"); break; } }while(val!=6); printf(" \n[Thank you!! for using codingstreet.com 's Data Structure Solutions !!] \n\n"); return 0; } |