mirror of
https://github.com/TheAlgorithms/C
synced 2025-02-17 22:14:05 +03:00
Merge pull request #113 from Rupeshiya/master
fulls source code for insertion and deletion in singly linked list
This commit is contained in:
commit
8329055745
@ -1,38 +1,25 @@
|
|||||||
/*********decimal number to binary number conversion*****************/
|
#include<stdio.h>
|
||||||
#include <stdio.h>
|
int main()
|
||||||
void decimal2Binary(long num);
|
{
|
||||||
|
int n,re,a[10000],j;
|
||||||
int main(){
|
printf("\nenter the no ");
|
||||||
|
scanf("%d",&n);
|
||||||
long num;
|
int i=0;
|
||||||
|
while(n>0)
|
||||||
printf("Enter a decimal integer \n");
|
|
||||||
scanf("%ld", &num);
|
|
||||||
decimal2Binary(num);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/***function for convert decimal numbers to binary numbers****************/
|
|
||||||
void decimal2Binary(long num){
|
|
||||||
|
|
||||||
long decimal_num, remainder, base, binary, no_of_1s;
|
|
||||||
|
|
||||||
base = 1;
|
|
||||||
binary = 0;
|
|
||||||
no_of_1s = 0;
|
|
||||||
|
|
||||||
while (num > 0)
|
|
||||||
{
|
{
|
||||||
remainder = num % 2;
|
|
||||||
|
re=n%2;
|
||||||
if (remainder == 1)
|
a[i]=re;
|
||||||
{
|
n=n/2;
|
||||||
no_of_1s++;
|
i++;
|
||||||
}
|
}
|
||||||
binary = binary + remainder * base;
|
int k;
|
||||||
num = num / 2;
|
k=i-1;
|
||||||
base = base * 10;}
|
printf("\n the number in binary is: ");
|
||||||
|
for(j=k;j>=0;j--)
|
||||||
printf("Its binary equivalent is = %ld\n", binary);
|
{
|
||||||
|
printf("%d",a[j]);
|
||||||
|
}
|
||||||
|
return(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,65 +3,96 @@
|
|||||||
function can be modified according to the data type, easily.
|
function can be modified according to the data type, easily.
|
||||||
deleteNode deletes a node when passed with a key of the node.
|
deleteNode deletes a node when passed with a key of the node.
|
||||||
*/
|
*/
|
||||||
#include <stdio.h>
|
#include<stdio.h>
|
||||||
#include <stdlib.h>
|
struct node
|
||||||
|
{int info;
|
||||||
struct Node
|
struct node *link;
|
||||||
{
|
|
||||||
int data;
|
|
||||||
struct Node *next;
|
|
||||||
};
|
};
|
||||||
|
struct node *start=NULL;
|
||||||
void push(struct Node** head_ref, int new_data)
|
///////////////////////////////////////////////////////////
|
||||||
|
struct node * createnode()//function to create node
|
||||||
{
|
{
|
||||||
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
|
struct node *t;
|
||||||
new_node->data = new_data;
|
t=(struct node*)malloc(sizeof(struct node));
|
||||||
new_node->next = (*head_ref);
|
return(t);
|
||||||
(*head_ref) = new_node;
|
|
||||||
}
|
}
|
||||||
|
////////////////////////////////////////////////////////
|
||||||
void deleteNode(struct Node **head_ref, int key)
|
void insert()//function to insert at first location
|
||||||
{
|
{
|
||||||
struct Node* temp = *head_ref, *prev;
|
struct node *p;
|
||||||
|
p=createnode();
|
||||||
if (temp != NULL && temp->data == key)
|
printf("\nenter the number to insert");
|
||||||
{
|
scanf("%d",&p->info);
|
||||||
*head_ref = temp->next;
|
p->link=NULL;
|
||||||
free(temp);
|
if(start==NULL)
|
||||||
return;
|
{
|
||||||
}
|
start=p;
|
||||||
|
}
|
||||||
|
else
|
||||||
while (temp != NULL && temp->data != key)
|
{
|
||||||
{
|
p->link=start;
|
||||||
prev = temp;
|
start=p;
|
||||||
temp = temp->next;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (temp == NULL) return;
|
|
||||||
|
|
||||||
prev->next = temp->next;
|
|
||||||
|
|
||||||
free(temp);
|
|
||||||
}
|
}
|
||||||
|
///////////////////////////////////////////////////////////
|
||||||
void printList(struct Node *node)
|
void deleteion()//function to delete from first position
|
||||||
{
|
{
|
||||||
while (node != NULL)
|
struct node *t;
|
||||||
|
if(start==NULL)
|
||||||
{
|
{
|
||||||
printf(" %d ", node->data);
|
printf("\nlist is empty");
|
||||||
node = node->next;
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
struct node *p;
|
||||||
|
p=start;
|
||||||
|
start=start->link;
|
||||||
|
free(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
///////////////////////////////////////////////////////
|
||||||
|
void viewlist()//function to display values
|
||||||
|
{
|
||||||
|
struct node *p;
|
||||||
|
if(start==NULL)
|
||||||
|
{
|
||||||
|
printf("\nlist is empty");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{ p=start;
|
||||||
|
while(p!=NULL)
|
||||||
|
{
|
||||||
|
printf("%d ",p->info);
|
||||||
|
p=p->link;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
/* new node can be created here as :-
|
int n;
|
||||||
struct Node* head = NULL;
|
while(1)
|
||||||
push(&head, data);
|
{
|
||||||
|
printf("\n1.add value at first location");
|
||||||
and a node can be delete by using
|
printf("\n2.delete value from first location");
|
||||||
deleteNode(&head, key);
|
printf("\n3.view value");
|
||||||
*/
|
printf("\nenter your choice");
|
||||||
return 0;
|
scanf("%d",&n);
|
||||||
|
switch(n)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
insert();
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
deleteion();
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
viewlist();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("\ninvalid choice");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return(0);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user