Caution/disclaimer: This post only depicts a idea for the programs, and are not fit for all the marginal cases like values above 10^6, inputs other than expected formats and some other things. So please take just the idea from the codes and try to correct this more. You can mail me better versions too.
linked list introduction:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//linked list program
// defining the type block with struct
typedef struct block
{
char transactions[100];
int hash;
struct block *next;
struct block *prev;
}block;
// setting head and tail as global
block *head=NULL;
block *tail=NULL;
// function to create a node with custom value
block *createnode(char *transactions, int hash)
{
block *newblock;
newblock=(block *)malloc(sizeof(block));
strcpy(newblock->transactions,transactions);
newblock->hash=hash;
newblock->prev=NULL;
newblock->next=NULL;
return newblock;
}
//function to add a node at any instance to the linked list
void addnode(char *transactions,int hash)
{
if(head==NULL)
{
head=createnode(transactions,hash);
tail=head;
}
else
{ block *blocknew;
blocknew=createnode(transactions,hash);
tail->next=blocknew;
blocknew->prev=tail;
tail=blocknew;
}
}
// function to print all the values stored in linked list
void printlist(block *n)
{
while(n!=NULL)
{
printf("%s,%d\n",n->transactions,n->hash);
n=n->next;
}
}
//function to print the value for specific hash
void searchlist(block *n, int value)
{ int count=0;
while(n!=NULL)
{
if(n->hash==value)
{
printf("%s\n",n->transactions);
count+=1;
}
n=n->next;
}
if(count==0)
{
printf("sorry! no such transactions");
}
}
// in main we take the values, create, show and search the linked list
int main(void) {
int N; int i;
//printf("what length?\n");
scanf("%d",&N);
for(i=0;i<N;i++)
{ char c[200]; int hash;
//printf("transaction and hash please\n");
scanf("%s %d",&c,&hash);
addnode(c,hash);
}
printlist(head);
searchlist(head,6);
return 0;
}
For applications of linked list; look here for queue using linked list. Also check out stack.
If you are serious about data structures and algorithms, I recommend you buying and reading this book below:
If you are serious about data structures and algorithms, I recommend you buying and reading this book below:
Comments
Post a Comment