C Program For Single Linked List

#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
}*head;
void append(int num)
{
    struct node *temp,*right;
    temp= (struct node *)malloc(sizeof(struct node));
    temp->data=num;
    right=(struct node *)head;
    while(right->next != NULL)
    right=right->next;
    right->next =temp;
    right=temp;
    right->next=NULL;
}
void add( int num )
{
   struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if (head== NULL)
    {
    head=temp;
    head->next=NULL;
    }
    else
    {
    temp->next=head;
    head=temp;
    }
}
void addafter(int num, int loc)
{
    int i;
   struct node *temp,*left,*right;
    right=head;
    for(i=1;i<loc;i++)
    {
    left=right;
    right=right->next;
    }
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    left->next=temp;
    left=temp;
    left->next=right;
    return;
}
void insert(int num)
{
    int c=0;
    struct node *temp;
    temp=head;
    if(temp==NULL)
    {
    add(num);
    }
    else
    {
    while(temp!=NULL)
    {
if(temp->data<num)
c++;
temp=temp->next;
    }
    if(c==0)
add(num);
    else if(c<count())
addafter(num,++c);
    else
append(num);
    }
}
int delete(int num)
{
    struct node *temp, *prev;
    temp=head;
    while(temp!=NULL)
    {
    if(temp->data==num)
    {
if(temp==head)
{
head=temp->next;
free(temp);
return 1;
}
else
{
prev->next=temp->next;
free(temp);
return 1;
}
     }
     else
     {
prev=temp;
temp= temp->next;
     }
     }
     return 0;
 }
 void  display(struct node *r)
 {
     r=head;
     if(r==NULL)
     {
     return;
     }
     while(r!=NULL)
     {
     printf("%d ",r->data);
     r=r->next;
     }
     printf("\n");
 }
  int count()
 {
    struct node *n;
     int c=0;
     n=head;
     while(n!=NULL)
     {
     n=n->next;
     c++;
     }
     return c;
 }
 int  main()
 {
     int i,num;
     struct node *n;
     head=NULL;
     while(1)
     {
     printf("\nList Operations\n");
     printf("===============\n");
     printf("1.Insert\n");
     printf("2.Display\n");
     printf("3.Size\n");
     printf("4.Delete\n");
     printf("5.Exit\n");
     printf("Enter your choice : ");
     if(scanf("%d",&i)<=0){
printf("Enter only an Integer\n");
exit(0);
     } else {
switch(i)
{
case 1:      printf("Enter the number to insert : ");
     scanf("%d",&num);
    insert(num);
    break;
case 2:     if(head==NULL)
{
printf("List is Empty\n");
}
else
{
printf("Element(s) in the list are : ");
}
display(n);
break;
case 3:     printf("Size of the list is %d\n",count());
break;
case 4:     if(head==NULL)
printf("List is Empty\n");
else{
printf("Enter the number to delete : ");
scanf("%d",&num);
if(delete(num))
   printf("%d deleted successfully\n",num);
else
   printf("%d not found in the list\n",num);
}
break;
case 5:     return 0;
default:    printf("Invalid option\n");
}
    }
    }
    return 0;
}
 you can use either first one or the second one according to your need of code



/*Single Linked List*/

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int n;
struct node *next;
};
struct node *curr,*temp,*prev,*last;

struct node *create(struct node*);
void display(struct node*);
void insend(struct node*);
struct node* insbeg(struct node *);
void inspos(struct node *);
struct node* delbeg(struct node*);
struct node* delend(struct node*);
void delpos(struct node*);
void count(struct node*);
void search(struct node*);
void sort(struct node*);
void reverse(struct node*);
struct node* split(struct node*);
void merge(struct node*,struct node*);
void main(void)
{
struct node *s,*s1;
int ch,cch;
s=NULL;
s1=NULL;
clrscr();
do
{
printf("\n1.Create\n");
printf("2.Display\n");
printf("3.Insend\n");
printf("4.Insbeg\n");
printf("5.Inspos\n");
printf("6.count\n");
printf("7.Search\n");
printf("8.Delbeg\n");
printf("9.Delend\n");
printf("10.Delpos\n");
printf("11.Reverse\n");
printf("12.Split\n");
printf("13.Merge\n");
printf("14.Sort\n");
printf("15.Exit\n");
printf("Enter the choice");
scanf("%d",&ch);
 switch(ch)
 {
 case 1:s=create(s);
  break;
 case 2: printf("1.First list\n2.Second List\n");
  printf("Enter the choice");
  scanf("%d",&cch);
  if (cch==1)
  {
  display(s);
  }
  else if (cch==2)
  {
  display(s1);
  }
  break;
 case 3:insend(s);
  break;
 case 4:s=insbeg(s);
  break;
 case 5:inspos(s);
  break;
 case 6:count(s);
  break;
 case 7:search(s);
  break;
 case 8:s=delbeg(s);
  break;
 case 9:s=delend(s);
  break;
 case 10:delpos(s);
  break;
 case 11:reverse(s);
  break;
 case 12:s1=split(s);
  break;
 case 13:merge(s,s1);
  s1=NULL;
 case 14:sort(s);
 }
}while(ch!=15);
}


struct node* create(struct node *x)
{
 if(x==NULL)
 {
 x=(struct node *)malloc(sizeof(struct node));
 printf("Enter the number");
 scanf("%d",&x->n);
 x->next=NULL;
 return x;
 }
 else
 {
 printf("The node already created\n");
 return x;
 }
}

void display(struct node *x)
{
curr=x;
 while(curr!=NULL)
 {
 printf("%d->",curr->n);
 curr=curr->next;
 }
}


void insend(struct node *x)
{
curr=x;
 while(curr->next!=NULL)
 {
 curr=curr->next;
 }

temp=(struct node *)malloc(sizeof(struct node));
printf("Enter the number");
scanf("%d",&temp->n);
temp->next=NULL;
curr->next=temp;
}

struct node *insbeg(struct node *x)
{
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter the number");
scanf("%d",&temp->n);
temp->next=x;
return temp;
}


void inspos(struct node *x)
{
int pos,c=1;
curr=x;
printf("Enter the pos to be inserted\n");
scanf("%d",&pos);

 while(curr->next!=NULL)
 {
 c++;
  if (c==pos)
  {
  temp=(struct node*)malloc(sizeof(struct node));
  printf("Enter the number");
  scanf("%d",&temp->n);
  temp->next=curr->next;
  curr->next=temp;
  break;
  }
 curr=curr->next;
 }
}

void count(struct node *x)
{
int c=0;
curr=x;
 while(curr!=NULL)
 {
 curr=curr->next;
 c++;
 }
printf("The count of the nodes are %d\n",c);
}


void search(struct node *x)
{
int se;
curr=x;
printf("Enter the searching element");
scanf("%d",&se);

 while(curr!=NULL)
 {
  if (curr->n==se)
  {
  printf("Element found\n");
  break;
  }
 curr=curr->next;
 }

if (curr==NULL)
{
printf("The element not found\n");
}
}

struct node *delbeg(struct node *x)
{
curr=x;
 if (curr==NULL)
 {
 printf("No node to be deleted\n");
 return curr;
 }
 else if (curr->next==NULL)
 {
 printf("U r deleting one and only node\n");
 free(curr);
 x=NULL;
 return x;
 }
 else
 {
 x=x->next;
 free(curr);
 return x;
 }
}


struct node * delend(struct node *x)
{
curr=x;
 if (curr==NULL)
 {
 printf("NO node to be deleted\n");
 return x;
 }
 else if (curr->next==NULL)
 {
 printf("U r deleting  final node\n");
 free(curr);
 x=NULL;
 return x;
 }
 else
 {
  while(curr->next!=NULL)
  {
  prev=curr;
  curr=curr->next;
  }
 free(curr);
 prev->next=NULL;
 return x;
 }
}


void delpos(struct node *x)
{
int pos,c=1;
curr=x;
printf("Enter the pos to be deleted\n");
scanf("%d",&pos);

 while(curr->next!=NULL)
 {
 c++;
  if (c==pos)
  {
  temp=curr->next;
  curr->next=curr->next->next;
  free(temp);
  }
 curr=curr->next;
 }
}


void reverse(struct node *x)
{
curr=x;
 while(curr->next!=NULL)
 {
 curr=curr->next;
 }
printf("%d->",curr->n);
last=curr;

 do
 {
 curr=x;
  while(curr->next!=last)
  {
  curr=curr->next;
  }
 last=curr;
 printf("%d->",curr->n);
 }while(last!=x);
}


struct node * split(struct node *x)
{
int pos,c=1;
curr=x;
printf("Enter the pos to  split\n");
scanf("%d",&pos);
 while(curr->next!=NULL)
 {
 c++;
  if (c==pos)
  {
  temp=curr->next;
  curr->next=NULL;
  break;
  }
 curr=curr->next;
 }
return temp;
}

void merge(struct node *x,struct node *y)
{
curr=x;
 while(curr->next!=NULL)
 {
 curr=curr->next;
 }
curr->next=y;
}
void sort(struct node *x)
{
int c=0,i,j,te;
curr=x;
 while(curr!=NULL)
 {
 curr=curr->next;
 c++;
 }

 for(i=0;i<c;i++)
 {
 curr=x;
  for(j=0;j<c-i-1;j++)
  {
   if (curr->n > curr->next->n)
   {
   te=curr->n;
   curr->n=curr->next->n;
   curr->next->n=te;
   }
  curr=curr->next;
  }
 }
}




Bhanu Namikaze

Bhanu Namikaze is an Ethical Hacker, Security Analyst, Blogger, Web Developer and a Mechanical Engineer. He Enjoys writing articles, Blogging, Debugging Errors and Capture the Flags. Enjoy Learning; There is Nothing Like Absolute Defeat - Try and try until you Succeed.

No comments:

Post a Comment