Linked List in C
Linked List in C
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->data = data;
head->next = NULL;
temp = head;
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 |
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->data = data;
head->next = NULL;
temp = head;
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, 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 |
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->data = data;
head->next = NULL;
temp = head;
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, 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 |
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->data = data;
head->next = NULL;
temp = head;
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, 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 |
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->data = data;
head->next = NULL;
temp = head;
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 |
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->data = data;
head->next = NULL;
temp = head;
temp->next = sec;
temp = temp->next;
}
}
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 |