answersLogoWhite

0


Best Answer

#include<stdio.h>

#include<stdlib.h>

typedef struct Node

{

int data;

struct Node *next;

}node;

void insert(node *pointer, int data)

{

/* Iterate through the list till we encounter the last node.*/

while(pointer->next!=NULL)

{

pointer = pointer -> next;

}

/* Allocate memory for the new node and put data in it.*/

pointer->next = (node *)malloc(sizeof(node));

pointer = pointer->next;

pointer->data = data;

pointer->next = NULL;

}

int find(node *pointer, int key)

{

pointer = pointer -> next; //First node is dummy node.

/* Iterate through the entire linked list and search for the key. */

while(pointer!=NULL)

{

if(pointer->data == key) //key is found.

{

return 1;

}

pointer = pointer -> next;//Search in the next node.

}

/*Key is not found */

return 0;

}

void delete(node *pointer, int data)

{

/* Go to the node for which the node next to it has to be deleted */

while(pointer->next!=NULL && (pointer->next)->data != data)

{

pointer = pointer -> next;

}

if(pointer->next==NULL)

{

printf("Element %d is not present in the list\n",data);

return;

}

/* Now pointer points to a node and the node next to it has to be removed */

node *temp;

temp = pointer -> next;

/*temp points to the node which has to be removed*/

pointer->next = temp->next;

/*We removed the node which is next to the pointer (which is also temp) */

free(temp);

/* Beacuse we deleted the node, we no longer require the memory used for it .

free() will deallocate the memory.

*/

return;

}

void print(node *pointer)

{

if(pointer==NULL)

{

return;

}

printf("%d ",pointer->data);

print(pointer->next);

}

int main()

{

/* start always points to the first node of the linked list.

temp is used to point to the last node of the linked list.*/

node *start,*temp;

start = (node *)malloc(sizeof(node));

temp = start;

temp -> next = NULL;

/* Here in this code, we take the first node as a dummy node.

The first node does not contain data, but it used because to avoid handling special cases

in insert and delete functions.

*/

printf("1. Insert\n");

printf("2. Delete\n");

printf("3. Print\n");

printf("4. Find\n");

while(1)

{

int query;

scanf("%d",&query);

if(query==1)

{

int data;

scanf("%d",&data);

insert(start,data);

}

else if(query==2)

{

int data;

scanf("%d",&data);

delete(start,data);

}

else if(query==3)

{

printf("The list is ");

print(start->next);

printf("\n");

}

else if(query==4)

{

int data;

scanf("%d",&data);

int status = find(start,data);

if(status)

{

printf("Element Found\n");

}

else

{

printf("Element Not Found\n");

}

}

}

}

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c program to insert an element in the beginning at the end at the specified position in a linear linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write an applet program to insert a text at a specified position?

bbbdb


What type of information should be specified in the block comment at the very beginning of the program?

What type of information should be specific in the block comment at the very beginning of a program? Provide a list of information.


Simple c program for selection sort?

The Selection Sort definition is rather simple : find the largest number (element) in a list and move it to it's position in sorted form.You can perform selection sort like, smallest elements are in the beginning and largest element at the end.Now how this element arrange to it's exact position,We can do this by swapping elements at highest index and the process is continue till all the elements are sorted.


How is the program of pso written in in matlab...For eg i want to obtain optimal position for svc for voltage stability..how will b program..also how to create that program from algorithm..?

The program of PSO in Matlab to obtain optimal position for SVC voltage stability will need written in the standard format beginning with the stability grid.


When coordinates going into a program are specified relative to the program zero point it is called?

rapid


Define program relocation?

program relocation is the process which modifies the object program so that it can be loaded at an address different from the location originally specified.


Program to insert an element in array at beginning?

Answer: Use the unshift() Method You can use the unshift() method to easily add new elements or values at the beginning of an array in JavaScript. This method is a counterpart of the push() method, which adds the elements at the end of an array. However, both method returns the new length of the array To learn more about data science please visit- Learnbay.co


Who is the AF Program Element Monitor for AF AT program?

Af/a7sx


Who is the program element monitor (PEM) for the AF AT program?

Af/a7sx


Who is the af program element monitor for the af at program?

Af/a7sx


What are the operations in singly linked lists?

The main operations on singly linked list are: 1:-Insertion 2:-Deletion 3:-Searching 4:-Display Insertion can be done in done ways: 1)Insertion at beginning 2)Insertion at end 3)Inserting at a specified position Deletion can be performed in 3 ways: 1)Deletion from beginning 2)Deletion from end 3)Deletion from a specified position.


What program searches documents for specified keywords?

search engine