this is my post on data structure :- singly linked list :-
first we create shared library file:-
-------------------------------------
in code blocks file-> new -> projects -> shared library -> select c or c++ (I chosen c)


then give full of your project path where you want to crearte shared lib file.
give project title :- this will be your final shared lilbrary file name.remove main.c from project.
file -> new -> file -> c/c++ header file -> give file name with full path.
open file in text editor. and type following code :-
------------------------------------------------------------
#ifndef SINGLYLL_H_INCLUDED
#define SINGLYLL_H_INCLUDED
struct node
{
int data;
struct node * link;
};
struct node *start, *p;
int count;
void display( struct node * start);
void search( struct node *start, int data);
struct node * addatbegin( struct node *start, int data);
struct node * addatend( struct node*start, int data);
struct node * addafter( struct node * start, int data, int item);
struct node * addbefore( struct node * start, int data, int item);
struct node * createlist( struct node * start, int n);
struct node * del( struct node* start, int data);
struct node * reverse( struct node *start);
#endif // SINGLYLL_H_INCLUDED
then file-> new -> file -> c/c++ source file -> select c -> full path and name of source file.
( Be sure to check mark in Debug and Release )
(i chosen singlyllc.c)
open source file in text editor. and type following code :-
#include "singlyll.h"
#include <stdio.h>
#include <stdlib.h>
void display(struct node* start)
{
if(start == NULL)
{
printf("list is empty\n");
return;
}
printf("List : ");
count = 0;
for(p = start; p != NULL; p = p->link)
{
printf("%d \t", p->data);
count++;
}
printf("\n\n");
}
void search(struct node* start, int item)
{
struct node* p = start;
int pos = 1;
for(;p != NULL; p = p->link)
{
if(p->data == item )
{
printf( "item %d found at position %d \n", item, pos);
return;
}
pos++;
}
printf("item %d not found in list\n", item);
}
struct node* addatbegin( struct node* start, int item)
{
struct node *temp = (struct node*) malloc(sizeof(struct node));
temp->data = item;
temp->link = start;
start = temp;
count++;
return start;
}
struct node* addatend(struct node* start, int item)
{
struct node* p = start, *temp;
temp = (struct node*) malloc(sizeof(struct node));
temp->link = NULL;
temp->data = item;
while(p->link!= NULL)
{
p = p->link;
}
p->link = temp;
count++;
return start;
}
struct node* addafter(struct node* start,int data, int item)
{
p = start;
struct node *temp;
while( p != NULL)
{
printf("p->data = %d\n",p->data );
if(p->data == item)
{
temp = (struct node*) malloc(sizeof(struct node));
temp->data = data;
temp->link = p->link;
p->link = temp;
count++;
return start;
}
p = p->link;
}
printf(" not present in the list\n");
return start;
}
struct node* addbefore(struct node* start, int data, int item)
{
struct node*temp;
if(start == NULL)
{
printf("list is empty\n");
return start;
}
//insert before the first struct node
if(item == start->data)
{
temp = (struct node*) malloc(sizeof(struct node));
temp->data = data;
temp->link = start;
start = temp;
count++;
}
p = start;
while(p->link != NULL)
{
if(p->link->data == item)
{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->link = p->link;
p->link = temp;
count++;
return start;
}
p = p->link;
}
printf("%d not present in the list\n", item);
return start;
}
struct node* createlist(struct node* start, int n)
{
int i, data;
count = 0;
start = NULL;
if( n == 0)
{
return start;
}
printf("Element to be inserted : ");
scanf("%d", &data);
start = addatbegin(start, data);
for( i = 2; i <= n; i++ )
{
printf("Element to be inserted : ");
scanf("%d",&data);
start = addatend(start, data);
}
return start;
}
struct node* del(struct node* start, int data)
{
struct node*p, *temp;
if(start == NULL)
{
printf("The list is empty \n");
count = 0;
return start;
}
if(start->data == data)
{
temp= start;
start = start->link;
free(temp);
count--;
return start;
}
p = start;
while(p->link != NULL)
{
if( p->link->data == data)
{
temp = p->link;
p->link = temp->link;
free(temp);
count--;
return start;
}
p = p->link;
}
printf("Element %d not found\n", data);
return start;
}
struct node* reverse(struct node* start)
{
struct node*prev, *ptr, *next;
prev = NULL;
ptr = start;
while(ptr != NULL)
{
next = ptr->link;
ptr->link = prev;
prev = ptr;
ptr = next;
}
start = prev;
return start;
}
------------------------------------------------------------
#ifndef SINGLYLL_H_INCLUDED
#define SINGLYLL_H_INCLUDED
struct node
{
int data;
struct node * link;
};
struct node *start, *p;
int count;
void display( struct node * start);
void search( struct node *start, int data);
struct node * addatbegin( struct node *start, int data);
struct node * addatend( struct node*start, int data);
struct node * addafter( struct node * start, int data, int item);
struct node * addbefore( struct node * start, int data, int item);
struct node * createlist( struct node * start, int n);
struct node * del( struct node* start, int data);
struct node * reverse( struct node *start);
#endif // SINGLYLL_H_INCLUDED
then file-> new -> file -> c/c++ source file -> select c -> full path and name of source file.
( Be sure to check mark in Debug and Release )
(i chosen singlyllc.c)
open source file in text editor. and type following code :-
#include "singlyll.h"
#include <stdio.h>
#include <stdlib.h>
void display(struct node* start)
{
if(start == NULL)
{
printf("list is empty\n");
return;
}
printf("List : ");
count = 0;
for(p = start; p != NULL; p = p->link)
{
printf("%d \t", p->data);
count++;
}
printf("\n\n");
}
void search(struct node* start, int item)
{
struct node* p = start;
int pos = 1;
for(;p != NULL; p = p->link)
{
if(p->data == item )
{
printf( "item %d found at position %d \n", item, pos);
return;
}
pos++;
}
printf("item %d not found in list\n", item);
}
struct node* addatbegin( struct node* start, int item)
{
struct node *temp = (struct node*) malloc(sizeof(struct node));
temp->data = item;
temp->link = start;
start = temp;
count++;
return start;
}
struct node* addatend(struct node* start, int item)
{
struct node* p = start, *temp;
temp = (struct node*) malloc(sizeof(struct node));
temp->link = NULL;
temp->data = item;
while(p->link!= NULL)
{
p = p->link;
}
p->link = temp;
count++;
return start;
}
struct node* addafter(struct node* start,int data, int item)
{
p = start;
struct node *temp;
while( p != NULL)
{
printf("p->data = %d\n",p->data );
if(p->data == item)
{
temp = (struct node*) malloc(sizeof(struct node));
temp->data = data;
temp->link = p->link;
p->link = temp;
count++;
return start;
}
p = p->link;
}
printf(" not present in the list\n");
return start;
}
struct node* addbefore(struct node* start, int data, int item)
{
struct node*temp;
if(start == NULL)
{
printf("list is empty\n");
return start;
}
//insert before the first struct node
if(item == start->data)
{
temp = (struct node*) malloc(sizeof(struct node));
temp->data = data;
temp->link = start;
start = temp;
count++;
}
p = start;
while(p->link != NULL)
{
if(p->link->data == item)
{
temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->link = p->link;
p->link = temp;
count++;
return start;
}
p = p->link;
}
printf("%d not present in the list\n", item);
return start;
}
struct node* createlist(struct node* start, int n)
{
int i, data;
count = 0;
start = NULL;
if( n == 0)
{
return start;
}
printf("Element to be inserted : ");
scanf("%d", &data);
start = addatbegin(start, data);
for( i = 2; i <= n; i++ )
{
printf("Element to be inserted : ");
scanf("%d",&data);
start = addatend(start, data);
}
return start;
}
struct node* del(struct node* start, int data)
{
struct node*p, *temp;
if(start == NULL)
{
printf("The list is empty \n");
count = 0;
return start;
}
if(start->data == data)
{
temp= start;
start = start->link;
free(temp);
count--;
return start;
}
p = start;
while(p->link != NULL)
{
if( p->link->data == data)
{
temp = p->link;
p->link = temp->link;
free(temp);
count--;
return start;
}
p = p->link;
}
printf("Element %d not found\n", data);
return start;
}
struct node* reverse(struct node* start)
{
struct node*prev, *ptr, *next;
prev = NULL;
ptr = start;
while(ptr != NULL)
{
next = ptr->link;
ptr->link = prev;
prev = ptr;
ptr = next;
}
start = prev;
return start;
}
if header file singlyll.h is not in intellisense, then reparse project.
now go to project -> build options -> in compiler flags check mark on :-
now go to project -> build options -> in compiler flags check mark on :-

then go to project -> properties -> buld targets -> left pan -> select release -> rename from liblibfilename.so to libfilename.so and from Debug also.

then compile project.
this will produce "libsinglyll_c.so" (in my case ). now this is our shared lib file.
this will produce "libsinglyll_c.so" (in my case ). now this is our shared lib file.
Note to add ".so" file in include directive, add path of ".so" file in LD_LIBRARY_PATH environment variable in file "/etc/profile" for global recognition of all users in that OS or in home directory file"~/.bashrc" or "~/.bash_profile" for only in current user or in "/usr/lib64/" system directory for all applications.
--------------------------------------------------------------------
Now open file -> new -> project -> console project -> select C -> give project name and full path of your project. ( image 8.png, image 9.png) -> next -> finish.
give this project name :- linklist
now in main.c type following code :-
------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <singlyll.h>
int main()
{
start = p = NULL;
int choice = 0, data, item, n;
while(1)
{
printf("1. Create List\n"
"2. Display List\n"
"3. Count\n"
"4. Search in List\n"
"5. Add at beginning / Add at empty List\n"
"6. Add at end of List\n"
"7. Add after node in List \n"
"8. Add before node in List\n"
"9. Delete in List\n"
"10. Reverse Linked List\n"
"11. Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter number of nodes : ");
scanf("%d", &n);
start = createlist(start, n);
break;
case 2: display(start);
break;
case 3: printf("\n number of nodes are : %d\n", count);
break;
case 4: printf("Enter element to be searched : ");
scanf("%d", &data);
search( start, data);
break;
case 5: printf("Enter element to be inserted : ");
scanf("%d", &data);
start = addatbegin(start, data);
break;
case 6: printf("Enter element to be inserted : ");
scanf("%d", &data);
start = addatend(start, data);
break;
case 7: printf("Enter element to be inserted : ");
scanf("%d", &data);
printf("Enter element after which to be inserted : ");
scanf("%d", &item);
start = addafter( start, data, item);
break;
case 8: printf("Enter element to be inserted : ");
scanf("%d", &data);
printf("Enter element before which to be inserted : ");
scanf("%d", &item);
start = addbefore(start, data, item);
break;
case 9: printf("Enter element to be deleted : ");
scanf("%d", &item);
start = del(start, item);
break;
case 10: start = reverse(start);
break;
case 11: exit(0);
default : printf("\nWrong choice\n");
}
}
return 0;
}
Now goto project -> build options -> Left pane -> Release and in right pane -> linker settings ->
add following path ( path of your shared lib file "libsinglyll_c.so" ) :-
"/opt/projects/code blocks/C/data structures/shared lib files/singlyll_c/bin/Release/libsinglyll_c.so" or you may copy libsinglyll_c.so file to /usr/lib/ for universal use ( that means for all ).
then in search directories (header file path)->
"/opt/projects/code blocks/C/data structures/shared lib files/singlyll_c" or copy in
/usr/include/ for universal use.
then compile your project.
this will create executable file. then run this in terminal.
--------------------------------------------------------------
FINISHED your project.
--------------------------------------------------------------------
Now open file -> new -> project -> console project -> select C -> give project name and full path of your project. ( image 8.png, image 9.png) -> next -> finish.
give this project name :- linklist
now in main.c type following code :-
------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <singlyll.h>
int main()
{
start = p = NULL;
int choice = 0, data, item, n;
while(1)
{
printf("1. Create List\n"
"2. Display List\n"
"3. Count\n"
"4. Search in List\n"
"5. Add at beginning / Add at empty List\n"
"6. Add at end of List\n"
"7. Add after node in List \n"
"8. Add before node in List\n"
"9. Delete in List\n"
"10. Reverse Linked List\n"
"11. Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter number of nodes : ");
scanf("%d", &n);
start = createlist(start, n);
break;
case 2: display(start);
break;
case 3: printf("\n number of nodes are : %d\n", count);
break;
case 4: printf("Enter element to be searched : ");
scanf("%d", &data);
search( start, data);
break;
case 5: printf("Enter element to be inserted : ");
scanf("%d", &data);
start = addatbegin(start, data);
break;
case 6: printf("Enter element to be inserted : ");
scanf("%d", &data);
start = addatend(start, data);
break;
case 7: printf("Enter element to be inserted : ");
scanf("%d", &data);
printf("Enter element after which to be inserted : ");
scanf("%d", &item);
start = addafter( start, data, item);
break;
case 8: printf("Enter element to be inserted : ");
scanf("%d", &data);
printf("Enter element before which to be inserted : ");
scanf("%d", &item);
start = addbefore(start, data, item);
break;
case 9: printf("Enter element to be deleted : ");
scanf("%d", &item);
start = del(start, item);
break;
case 10: start = reverse(start);
break;
case 11: exit(0);
default : printf("\nWrong choice\n");
}
}
return 0;
}
Now goto project -> build options -> Left pane -> Release and in right pane -> linker settings ->
add following path ( path of your shared lib file "libsinglyll_c.so" ) :-
"/opt/projects/code blocks/C/data structures/shared lib files/singlyll_c/bin/Release/libsinglyll_c.so" or you may copy libsinglyll_c.so file to /usr/lib/ for universal use ( that means for all ).
then in search directories (header file path)->
"/opt/projects/code blocks/C/data structures/shared lib files/singlyll_c" or copy in
/usr/include/ for universal use.
then compile your project.
this will create executable file. then run this in terminal.
--------------------------------------------------------------
FINISHED your project.
No comments:
Post a Comment