0% found this document useful (0 votes)
3 views

code snippet for SLL

Uploaded by

Aishu Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

code snippet for SLL

Uploaded by

Aishu Gowda
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

simple linked list

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int count=0;

struct node
{
int data;
struct node *next;
}*first=NULL,*last=NULL,*temp=NULL, *temp1;

void create()
{
int data;
temp=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the data");
scanf("%d",&temp->data);
temp->next=NULL;
count++;
}

void insert_atfirst()
{
if (first == NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
temp->next = first;
first = temp;
}
}

void insert_atlast()
{
if(first==NULL)
{
create();
first = temp;
last = first;
}
else
{
create();
last->next = temp;
last = temp;
}
}
void display()
{
temp1=first;
if(temp1 == NULL)
{
printf("List empty to display \n");
return;
}
printf("\n Linked list elements from begining : \n");
while (temp1!= NULL)
{
printf("%d\n", temp1->data);
temp1 = temp1->next;
}
printf("%d\n",temp1=temp1->data
printf(" No of nodes = %d ", count);
}

int deleteend()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
free(temp);
first=NULL;
}
else
{
while(temp->next!=last)
temp=temp->next;
printf("%d\t", last->data );
free(last);
temp->next=NULL;
last=temp;
}
count--;
return 0;
}

int deletefront()
{
struct node *temp;
temp=first;
if(temp->next==NULL)
{
free(temp);
first=NULL;
return 0;
}
else
{
first=temp->next;
printf("%d",temp->data);
free(temp);
}
count--;
return 0;
}

void delete_after(struct node *head, int key)


{
struct node *ptr1, *ptr2;
ptr1=head;
while(ptr1->next != NULL)
{
if(ptr1->data = = key)
{
ptr2 = ptr1->next;
ptr1->next = ptr2-->next;
free(ptr2);
break;
}
ptr1=ptr1->next;
}
return head;
}

void insert_after(struct node *first, int x, int val)


{
printf("enter value of x and val");
scanf("%d%d",&x,&val);
struct node *ptr, *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data = val;
temp->next=NULL;
ptr=first;
while(ptr->data!=x && ptr!=NULL)
{
ptr=ptr->next;
}
if(ptr->data = = x)
{
ptr->next = temp;
temp->next=last;
}
return first;
}

void insert-before()
{
newnode = (struct node*)malloc(sizeof(struct node));
newnode->data = 5;
//newnode->next=NULL;
while(ptr->data!=num)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next=newnode;
newnode-->next=ptr;
}

void main()
{
int ch,n,i;
first=NULL;
temp = temp1 = NULL;
printf("-----------------MENU----------------------\n");
printf("\n 1 create a SLL of n emp");
printf("\n 2 - Display from beginning");
printf("\n 3 - Insert at end");
printf("\n 4 - delete at end");
printf("\n 5 - Insert at beg");
printf("\n 6 - delete at beg");
printf("\n 7 - exit\n");
printf("-------------------------------------------\n");
while (1)
{
printf("\n Enter choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\n Enter no of nodes:");
scanf("%d", &n);
for(i=0;i<n;i++)
insert_atfirst();
break;
case 2: display();
break;
case 3: insert_atlast();
break;
case 4:deleteend();
break;
case 5: insert_atfirst();
break;
case 6: deletefront();
break;
case 7: exit(0);
default: printf("wrong choice\n");
}
}
}

You might also like