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

Linked List in C

The document discusses code implementations for various operations on linked lists in C including: 1. Creating a linked list and inserting nodes at the beginning, middle, and end. 2. Functions are provided to create a linked list, insert nodes in different locations, and print the list. 3. Examples of output are shown for creating a list and inserting nodes at various indexes. 4. Deletion of the first node is also demonstrated with example code and output.

Uploaded by

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

Linked List in C

The document discusses code implementations for various operations on linked lists in C including: 1. Creating a linked list and inserting nodes at the beginning, middle, and end. 2. Functions are provided to create a linked list, insert nodes in different locations, and print the list. 3. Examples of output are shown for creating a list and inserting nodes at various indexes. 4. Deletion of the first node is also demonstrated with example code and output.

Uploaded by

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

1.

Create Linked list

Code :

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

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

void creat(int n)
{
struct node *temp, *sec;
int data, i;

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

printf("Enter the data in 1node:");


scanf("%d", &data);

head->data = data;
head->next = NULL;

temp = head;

for (i = 2; i <= n; i++)


{
sec = (struct node *)malloc(sizeof(struct node));
printf("Enter the data in %dnode:", i);
scanf("%d", &data);
sec->data = data;
sec->next = NULL;

temp->next = sec;
temp = temp->next;
}
}

void print()
{
struct node *temp = head;
printf("head = %u ", &temp->next);
while (temp != NULL)
{
if (temp->next == NULL)
printf(" --> | %d | NULL | ", temp->data);
else
printf(" --> | %d | %u | ", temp->data, temp->next);

temp = temp->next;
}
}

int main()
{
int n;
printf("How many node you want to create enter that number :");
scanf("%d", &n);
creat(n);
print();
}

Output :

/tmp/NYqWvMdcNg.o
How many node you want to create enter that number :5
Enter the data in 1node:1
Enter the data in 2node:2
Enter the data in 3node:3
Enter the data in 4node:4
Enter the data in 5node:5
head = 30460616 --> | 1 | 30460640 | --> | 2 | 30460672 | --> | 3
| 30460704 | --> | 4 | 30460736 | --> | 5 | NULL |

2. Insert at first in linked list

Code :

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

struct node
{
int data;
struct node *next;
} *head;
void creat(int n)
{
struct node *temp, *sec;
int data, i;

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

printf("Enter the data in 1node:");


scanf("%d", &data);

head->data = data;
head->next = NULL;

temp = head;

for (i = 2; i <= n; i++)


{
sec = (struct node *)malloc(sizeof(struct node));
printf("Enter the data in %dnode:", i);
scanf("%d", &data);
sec->data = data;
sec->next = NULL;

temp->next = sec;
temp = temp->next;
}
}

void insertatfirst(int data)


{
struct node *first;
first = (struct node *)malloc(sizeof(struct node));
first->data = data;
first->next = head;
head = first;
}

void print()
{
struct node *temp = head;
printf("head = %u ", &temp->next);
while (temp != NULL)
{
if (temp->next == NULL)
printf(" --> | %d | NULL | ", temp->data);
else
printf(" --> | %d | %u | ", temp->data, temp->next);

temp = temp->next;
}
}

int main()
{
int n, data;
printf("How many node you want to create enter that number :");
scanf("%d", &n);
creat(n);
print();
printf("\nEnter the data you want in first node:");
scanf("%d", &data);
insertatfirst(data);
print();
}

OutPut :

/tmp/NYqWvMdcNg.o
How many node you want to create enter that number :5
Enter the data in 1node:1
Enter the data in 2node:2
Enter the data in 3node:3
Enter the data in 4node:4
Enter the data in 5node:5
head = 13757128 --> | 1 | 13757152 | --> | 2 | 13757184 | --> | 3
| 13757216 | --> | 4 | 13757248 | --> | 5 | NULL |
Enter the data you want in first node:0
head = 13757288 --> | 0 | 13757120 | --> | 1 | 13757152 | --> | 2
| 13757184 | --> | 3 | 13757216 | --> | 4 | 13757248 | --> | 5 |
NULL |

3. Insert data on index :

Code :

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

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

void creat(int n)
{
struct node *temp, *sec;
int data, i;

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

printf("Enter the data in 1node:");


scanf("%d", &data);

head->data = data;
head->next = NULL;

temp = head;

for (i = 2; i <= n; i++)


{
sec = (struct node *)malloc(sizeof(struct node));
printf("Enter the data in %dnode:", i);
scanf("%d", &data);
sec->data = data;
sec->next = NULL;

temp->next = sec;
temp = temp->next;
}
}

void insertatbetween(int data, int index)


{
struct node *between = (struct node *)malloc(sizeof(struct node));
struct node *prv;
int i;
prv = head;
if (between == NULL)
{
printf("\nempty");
exit(0);
}
else
{
between->data = data;
between->next = NULL;
for (i = 2; i <= index - 1; i++)
{
prv = prv->next;
}
if (prv != NULL)
{
between->next = prv->next;
prv->next = between;
}
}
}

void print()
{
struct node *temp = head;
printf("head = %u ", &temp->next);
while (temp != NULL)
{
if (temp->next == NULL)
printf(" --> | %d | NULL | ", temp->data);
else
printf(" --> | %d | %u | ", temp->data, temp->next);

temp = temp->next;
}
}

int main()
{
int n, data, index;
printf("How many node you want to create enter that number :");
scanf("%d", &n);
creat(n);
print();
printf("\nEnter the data you want to add:");
scanf("%d", &data);
printf("\nEnter the index no where you want to add:");
scanf("%d", &index);
insertatbetween(data, index);
print();
}

OutPut :
/tmp/NYqWvMdcNg.o
How many node you want to create enter that number :4
Enter the data in 1node:1
Enter the data in 2node:2
Enter the data in 3node:4
Enter the data in 4node:5
head = 8350408 --> | 1 | 8350432 | --> | 2 | 8350464 | --> | 4 |
8350496 | --> | 5 | NULL |
Enter the data you want to add:3
Enter the index no where you want to add:3
head = 8350408 --> | 1 | 8350432 | --> | 2 | 8350528 | --> | 3 |
8350464 | --> | 4 | 8350496 | --> | 5 | NULL |

4. Insert at end of node:

Code :

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

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

void creat(int n)
{
struct node *temp, *sec;
int data, i;

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

printf("Enter the data in 1node:");


scanf("%d", &data);

head->data = data;
head->next = NULL;

temp = head;

for (i = 2; i <= n; i++)


{
sec = (struct node *)malloc(sizeof(struct node));
printf("Enter the data in %dnode:", i);
scanf("%d", &data);
sec->data = data;
sec->next = NULL;

temp->next = sec;
temp = temp->next;
}
}

void insertatend(int data)


{
struct node *end = (struct node *)malloc(sizeof(struct node));
struct node *p;
p = head;
if (end == NULL)
{
printf("\nempty");
exit(0);
}
else
{
end->data = data;
end->next = NULL;
while (p->next != NULL)
{
p = p->next;
}
p->next = end;
end->next = NULL;
}
}

void print()
{
struct node *temp = head;
printf("head = %u ", &temp->next);
while (temp != NULL)
{
if (temp->next == NULL)
printf(" --> | %d | NULL | ", temp->data);
else
printf(" --> | %d | %u | ", temp->data, temp->next);

temp = temp->next;
}
}

int main()
{
int n, data;
printf("How many node you want to create enter that number :");
scanf("%d", &n);
creat(n);
print();
printf("\nEnter the data you want in end node:");
scanf("%d", &data);
insertatend(data);
print();
}

OutPut :

/tmp/NYqWvMdcNg.o
How many node you want to create enter that number :4
Enter the data in 1node:1
Enter the data in 2node:2
Enter the data in 3node:3
Enter the data in 4node:4
head = 29362888 --> | 1 | 29362912 | --> | 2 | 29362944 | --> | 3
| 29362976 | --> | 4 | NULL |
Enter the data you want in end node:5
head = 29362888 --> | 1 | 29362912 | --> | 2 | 29362944 | --> | 3
| 29362976 | --> | 4 | 29363008 | --> | 5 | NULL |

5. Delete first node :

Code :

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

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

void creat(int n)
{
struct node *temp, *sec;
int data, i;

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

printf("Enter the data in 1node:");


scanf("%d", &data);

head->data = data;
head->next = NULL;

temp = head;

for (i = 2; i <= n; i++)


{
sec = (struct node *)malloc(sizeof(struct node));
printf("Enter the data in %dnode:", i);
scanf("%d", &data);
sec->data = data;
sec->next = NULL;

temp->next = sec;
temp = temp->next;
}
}
void deletatfirst()
{
struct node *p = head;
head = head->next;
free(p);
}

void print()
{
struct node *temp = head;
printf("head = %u ", &temp->next);
while (temp != NULL)
{
if (temp->next == NULL)
printf(" --> | %d | NULL | ", temp->data);
else
printf(" --> | %d | %u | ", temp->data, temp->next);

temp = temp->next;
}
}
int main()
{
int n, data;
printf("How many node you want to create enter that number :");
scanf("%d", &n);
creat(n);
print();
deletatfirst(head);
printf("\n After deleting first Node \n");
print();
}

OutPut :

/tmp/NYqWvMdcNg.o
How many node you want to create enter that number :5
Enter the data in 1node:1
Enter the data in 2node:2
Enter the data in 3node:3
Enter the data in 4node:4
Enter the data in 5node:5
head = 36068040 --> | 1 | 36068064 | --> | 2 | 36068096 | --> | 3
| 36068128 | --> | 4 | 36068160 | --> | 5 | NULL |
After deleting first Node
head = 36068072 --> | 2 | 36068096 | --> | 3 | 36068128 | --> | 4
| 36068160 | --> | 5 | NULL |

6. Delete a index value :

Code :

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

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

void create(int n)
{
struct node *temp, *sec;
int data, i;

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

printf("Enter the data in 1st node:");


scanf("%d", &data);

head->data = data;
head->next = NULL;

temp = head;

for (i = 2; i <= n; i++)


{
sec = (struct node *)malloc(sizeof(struct node));
printf("Enter the data in %dth node:", i);
scanf("%d", &data);
sec->data = data;
sec->next = NULL;

temp->next = sec;
temp = temp->next;
}
}

void deleteAtIndex(int index)


{
if (index == 1)
{
struct node *temp = head;
head = head->next;
free(temp);
return;
}

struct node *prev = head;


struct node *current = head->next;
int i;

for (i = 2; i < index && current != NULL; i++)


{
prev = current;
current = current->next;
}
if (current == NULL)
{
printf("Index out of range!\n");
return;
}

prev->next = current->next;
free(current);
}

void print()
{
struct node *temp = head;
printf("head = %u ", &temp->next);
while (temp != NULL)
{
if (temp->next == NULL)
printf(" --> | %d | NULL | ", temp->data);
else
printf(" --> | %d | %u | ", temp->data, temp->next);

temp = temp->next;
}
}

int main()
{
int n, data, index;
printf("How many nodes do you want to create? Enter that number: ");
scanf("%d", &n);
create(n);
print();
printf("\nEnter the index (position) of the node to delete: ");
scanf("%d", &index);

if (index < 1)
{
printf("Invalid index. Index must be >= 1.\n");
}
else
{
deleteAtIndex(index);
printf("\nAfter deleting node at index %d:\n", index);
print();
}
return 0;
}

OutPut :

/tmp/NYqWvMdcNg.o
How many nodes do you want to create? Enter that number: 5
Enter the data in 1st node:1
Enter the data in 2th node:2
Enter the data in 3th node:3
Enter the data in 4th node:4
Enter the data in 5th node:5
head = 5270216 --> | 1 | 5270240 | --> | 2 | 5270272 | --> | 3 |
5270304 | --> | 4 | 5270336 | --> | 5 | NULL |
Enter the index (position) of the node to delete: 3
After deleting node at index 3:
head = 5270216 --> | 1 | 5270240 | --> | 2 | 5270304 | --> | 4 |
5270336 | --> | 5 | NULL |

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy