Merge pull request #113 from Rupeshiya/master

fulls source code for insertion and deletion in singly linked list
This commit is contained in:
Christian Bender 2018-02-02 21:32:58 +01:00 committed by GitHub
commit 8329055745
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 103 additions and 85 deletions

View File

@ -1,38 +1,25 @@
/*********decimal number to binary number conversion*****************/
#include <stdio.h>
void decimal2Binary(long num);
int main(){
long num;
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)
#include<stdio.h>
int main()
{
int n,re,a[10000],j;
printf("\nenter the no ");
scanf("%d",&n);
int i=0;
while(n>0)
{
remainder = num % 2;
if (remainder == 1)
{
no_of_1s++;
re=n%2;
a[i]=re;
n=n/2;
i++;
}
binary = binary + remainder * base;
num = num / 2;
base = base * 10;}
printf("Its binary equivalent is = %ld\n", binary);
int k;
k=i-1;
printf("\n the number in binary is: ");
for(j=k;j>=0;j--)
{
printf("%d",a[j]);
}
return(0);
}

View File

@ -3,65 +3,96 @@
function can be modified according to the data type, easily.
deleteNode deletes a node when passed with a key of the node.
*/
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
#include<stdio.h>
struct node
{int info;
struct node *link;
};
void push(struct Node** head_ref, int new_data)
struct node *start=NULL;
///////////////////////////////////////////////////////////
struct node * createnode()//function to create node
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
struct node *t;
t=(struct node*)malloc(sizeof(struct node));
return(t);
}
void deleteNode(struct Node **head_ref, int key)
////////////////////////////////////////////////////////
void insert()//function to insert at first location
{
struct Node* temp = *head_ref, *prev;
if (temp != NULL && temp->data == key)
struct node *p;
p=createnode();
printf("\nenter the number to insert");
scanf("%d",&p->info);
p->link=NULL;
if(start==NULL)
{
*head_ref = temp->next;
free(temp);
return;
start=p;
}
while (temp != NULL && temp->data != key)
else
{
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
free(temp);
}
void printList(struct Node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
p->link=start;
start=p;
}
}
///////////////////////////////////////////////////////////
void deleteion()//function to delete from first position
{
struct node *t;
if(start==NULL)
{
printf("\nlist is empty");
}
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()
{
/* new node can be created here as :-
struct Node* head = NULL;
push(&head, data);
and a node can be delete by using
deleteNode(&head, key);
*/
return 0;
int n;
while(1)
{
printf("\n1.add value at first location");
printf("\n2.delete value from first location");
printf("\n3.view value");
printf("\nenter your choice");
scanf("%d",&n);
switch(n)
{
case 1:
insert();
break;
case 2:
deleteion();
break;
case 3:
viewlist();
break;
default:
printf("\ninvalid choice");
}
}
return(0);
}