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

Program Linked List

The document contains a C program that implements a singly linked list with various operations such as insertion at the beginning, end, and at a specific position, as well as deletion from the beginning, end, and a specific position. It also includes a function to traverse and display the list. The main function provides a menu-driven interface for users to interact with the linked list operations.

Uploaded by

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

Program Linked List

The document contains a C program that implements a singly linked list with various operations such as insertion at the beginning, end, and at a specific position, as well as deletion from the beginning, end, and a specific position. It also includes a function to traverse and display the list. The main function provides a menu-driven interface for users to interact with the linked list operations.

Uploaded by

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

Program:

// implemets single linked list


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

struct node
{
int data;
struct node *next;
};
struct node *head =NULL;

void insertBeg(int ele);


void insertEnd(int ele);
void insertAtSpec(int ele,int pos);

void deleteBeg();
void deleteEnd();
void deleteAtSpec(int pos);

void travese();

void insertBeg(int ele)


{
//node memoery allocation
struct node *newnode=(struct node*)malloc(sizeof(struct node));
if(head == NULL)
{
newnode->data = ele;
newnode->next = NULL;
head = newnode;
printf("insert successfully at beginning\n");
}
else
{
newnode->data = ele;
newnode->next = head;
head = newnode;
printf("insert successfully at beginning\n");
}
}

void insertEnd(int ele)


{

//node memoery allocation


struct node *newnode=(struct node*)malloc(sizeof(struct node));

struct node *temp;


temp= head;
if(head == NULL)
{
newnode->data = ele;
newnode->next = NULL;
head = newnode;
printf("insert successfully at ending\n");
}
else
{
while(temp->next!=NULL) // MOVE TO LAST NODE
{
temp=temp->next;
}
newnode->data=ele;
newnode->next=NULL;
temp->next=newnode;
printf("insert successfully at ending\n");

}
}
void insertAtSpec(int ele,int pos)
{
int i;
//node memoery allocation

struct node *newnode=(struct node*)malloc(sizeof(struct node));


struct node *temp;
temp=head;
for(i=1;i<pos;++i)
{
temp=temp->next;

}
if(temp==NULL)
{
printf("Given node is not found in the list!! insertion not possible\n");
}
else
{
newnode->data = ele;
newnode->next=temp->next;
temp->next=newnode;
printf("insertion successfully at location : %d\n",pos);
}

}
void deleteBeg()
{
struct node *temp;
temp = head;
if(head==NULL)
{
printf("list is empty!! delection not possible..\n");
}
else if(temp->next==NULL)
{
printf("deleted element is:%d\n",temp->data);
temp->next=NULL;
head=NULL;
}
else
{
printf("deleted element is:%d\n",temp->data);
head=temp->next;
free(temp);
}
}
void deleteEnd()
{
struct node *temp1,*temp2;
temp1 = head;
if(head==NULL)
{
printf("list is empty!! delection not possible..\n");
}
else if(temp1->next==NULL)
{
printf("deleted element is:%d\n",temp1->data);
temp1->next=NULL;
head=NULL;
}
else
{
while(temp1->next!=NULL)
{
temp2=temp1;
temp1=temp1->next;
}
printf("deleted element is:%d\n",temp1->data);
temp2->next=NULL;
}

}
void deleteAtSpec(int pos)
{
int i;
struct node *temp1,*temp2;
temp1 = head;
if(head==NULL)
{
printf("list is empty!! delection not possible..\n");
}
else
{
for(i=0;i<pos;++i)
{
temp2=temp1;
temp1=temp1->next;
}
if(temp1==NULL)
{
printf("Delection not possible..\n");
}
else
{
printf("Deleted element is : %d at location :%d\n",temp1->data,pos);
temp2->next=temp1->next;
}
}

}
void travese()
{

struct node *temp;


temp = head;
if(head==NULL)
{
printf("list is empty!!!\n");
}
else
{
while(temp->next!=NULL)
{
printf("%d--->",temp->data);
temp=temp->next;
}
printf("%d-->NULL",temp->data);
printf("\n");
}

}
void main()
{
int choose,ele,pos;
while(1)
{
printf("************* Linked list operations*************\n");
printf(" 1.insert beg \n 2. insert end \n 3. insert at specific \n 4. Delete beg \n 5. Delete End \n 6.
delete at specific \n 7. travese \n 8.exit \n");
printf("Choose one (1-7)");
scanf("%d",&choose);
switch(choose)
{
case 1: printf("enter an element : ");
scanf("%d",&ele);
insertBeg(ele);
break;
case 2: printf("enter an element : ");
scanf("%d",&ele);
insertEnd(ele);
break;
case 3: printf("enter an element : ");
scanf("%d",&ele);
printf("Enter a specific location: ");
scanf("%d",&pos);
insertAtSpec(ele,pos);
break;
case 4: deleteBeg();
break;
case 5: deleteEnd();
break;
case 6:printf("Enter a specific location:");
scanf("%d",&pos);
deleteAtSpec(pos);
break;
case 7:travese();
break;
case 8:exit(0);
break;
default: printf("Invalid choose\n");
}
}
}

You might also like