Untitled
unknown
plain_text
4 years ago
2.4 kB
6
Indexable
/defining a structure struct node { int item; struct node *next; }; //including header file #include<stdio.h> #include<conio.h> #include<stdlib.h> //A function for creating link list int createlinklist(struct node *start) { int item; printf("at the end node you can enter -20\n"); printf("enter your item\n"); scanf("%d",&item); if(start==NULL) printf("no linked list became created\n"); else { start->item=item; if(start->item==-20) { start->next=NULL; return(0); }; start->next=(struct node *)malloc(sizeof(struct node)); createlinklist(start->next); } } //A function for insertion an element in link list struct node* insert_At_first(struct node *start,int item) { struct node *temp; struct node *newnode=(struct node*)malloc(sizeof(struct node)); newnode->item=item; if(start==NULL) start=newnode; temp=start; start=newnode; newnode->next=temp; return(start); } //function for deleting the node struct node* delete_first(struct node *start) { if(!start) return(0); struct node *temp; temp=start; start=start->next; free(temp); return(start); } //A function for printing the list int printlist(struct node *start) { if(start==NULL) return(0); printf("%d\n",start->item); printlist(start->next); } //a function for searching an element int search(struct node *start,int item) { struct node *current=start; while(current!=NULL) { if(current->item==item) printf("item is present\n"); return(0); current=current->next; } printf("item is not present\n"); return(0); } //main function for using above function int main() { int item,choice; struct node *start=(struct node*)malloc(sizeof(struct node)); while(1) { printf("1.create linklist\n2.insert at first\n3.delete from the first\n4.print list\n5.search element\n6.exit\n"); printf("enter the number for choice\n"); scanf("%d",&choice); switch(choice) { case 1: createlinklist(start); break; case 2: printf("enter the element to which you want insert at first\n"); scanf("%d",&item); start=insert_At_first(start,item); break; case 3: start=delete_first(start); break; case 4: printlist(start); break; case 5: printf("enter an item to which you want to search\n"); scanf("%d",&item); search(start,item); break; case 6: exit(1); } } }
Editor is loading...