Singly Linked List
CODE
๐
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct node
- {
- int info;
- struct node *next;
- }NODE;
- NODE* createlist(NODE *list);
- void Display (NODE *list);
- void search (NODE *list);
- NODE* insertbeg(NODE *list);
- NODE* insertbetween(NODE *list);
- NODE* insertlast(NODE *list);
- NODE* Delpos(NODE *list);
- NODE* Delvalue(NODE *list);
- void main ()
- {
- NODE*list=NULL, *temp;
- int ch,n,pos;
- list=createlist (list);
- Display (list);
- search (list);
- list=insertbeg (list);
- Display (list);
- list=insertbetween (list);
- Display (list);
- list=insertlast (list);
- Display (list);
- list=Delpos (list);
- Display (list);
- list=Delvalue (list);
- Display (list);
- }
- NODE* createlist (NODE *list)
- {
- int n,count;
- NODE *temp, *newnode;
- printf ("How many nodes you want to enter ? \n");
- scanf("%d" ,&n);
- for(count=1 ; count<=n; count++)
- {
- newnode=(NODE*)malloc(sizeof(NODE));
- newnode->next=NULL;
- printf( "Enter the node data:- " );
- scanf ("%d",&newnode->info);
- if (list==NULL)
- {
- list=temp=newnode;
- }
- else
- {
- temp->next=newnode;
- temp=newnode;
- }
- }
- return list;
- }
- void Display (NODE *list)
- {
- NODE *temp=list;
- while(temp!=NULL)
- {
- printf ("%d",temp->info);
- printf("-->");
- temp=temp->next ;
- }
- printf("NULL \n");
- }
- NODE * insertbeg(NODE *list)
- {
- int n;
- printf("Enter the node you want to insertat first position:");
- scanf("%d", &n);
- NODE *newnode;
- newnode=(NODE*)malloc(sizeof(NODE));
- newnode->info=n;
- newnode->next=list;
- list=newnode;
- return list;
- }
- NODE * insertbetween(NODE *list)
- {
- NODE *newnode, *temp=list;
- int n,i,pos;
- printf("Enter the node data and position you want to insert between the node: \t");
- scanf("\n%d", &n);
- scanf("\n%d", &pos);
- newnode=(NODE*)malloc(sizeof(NODE));
- newnode->next=NULL;
- newnode->info=n;
- for(i=1; i<pos-1&&temp->next!=NULL; i++)
- temp=temp->next;
- newnode->next=temp->next;
- temp->next=newnode;
- return list;
- }
- NODE * insertlast(NODE *list)
- {
- int n;
- printf("Enter the node data you want to insert at last position: \t");
- scanf("%d", &n);
- NODE *newnode, *temp;
- newnode=(NODE*)malloc(sizeof(NODE));
- newnode->info=n;
- newnode->next=NULL;
- for(temp=list; temp->next!=NULL; temp=temp->next);
- temp->next=newnode;
- return list;
- }
- void search (NODE *list)
- {
- int num, flag=0;
- NODE *temp;
- printf("Enter the element to be search:");
- scanf("%d", &num);
- for(temp=list; temp!=NULL; temp=temp->next)
- {
- if(temp->info==num)
- {
- printf(">>%d is Found<<\n",num);
- flag=1;
- exit;
- }
- }
- if(flag==0)
- printf(">>%d is not found<<\n", num);
- }
- NODE * Delpos(NODE *list)
- {
- NODE *temp=list, *temp1;
- int i, pos;
- printf("Enter node position to delete the node: \t");
- scanf("%d", &pos);
- if(pos==1)
- {
- list=temp->next;
- free (temp);
- return list;
- }
- for(i=1, temp=list; i<=pos-1 && temp!=NULL; i++)
- temp=temp->next;
- if(temp==NULL)
- {
- printf(">>position is out of range<<");
- return list;
- }
- temp1=temp->next;
- temp->next=temp1->next;
- free (temp1);
- return list;
- }
- NODE* Delvalue(NODE *list)
- {
- NODE *temp=list, *temp1;
- int num;
- printf("Enter node data to delete that node \t");
- scanf("%d", &num);
- if(list->info==num)
- {
- list=list->next;
- free (temp);
- return list;
- exit;
- }
- for(temp=list; temp->next!=NULL; temp=temp->next)
- if(temp->next->info==num)
- {
- temp1=temp->next;
- temp->next=temp1->next;
- free (temp1);
- return list;
- exit;
- }
- printf(">>Element is not found<<\n");
- return list;
- }
๐Execute๐
//Output
/*
How many nodes you want to enter ?
5
Enter the node data:- 9
Enter the node data:- 1
Enter the node data:- 4
Enter the node data:- 2
Enter the node data:- 3
9-->1-->4-->2-->3-->NULL
Enter the element to be search:5
>>5 is not found<<
Enter the node you want to insertat first position:7
7-->9-->1-->4-->2-->3-->NULL
Enter the node data and position you want to insert between the node: 5 4
7-->9-->1-->5-->4-->2-->3-->NULL
Enter the node data you want to insert at last position: 8
7-->9-->1-->5-->4-->2-->3-->8-->NULL
Enter node position to delete the node: 2
7-->9-->5-->4-->2-->3-->8-->NULL
Enter node data to delete that node 3
7-->9-->5-->4-->2-->8-->NULL
*/
//ThE ProFessoR
Comments
Post a Comment