#include <stdio.h>
int main()
{
int arr[10] = {1, 2, 3, 4, 5}, i, k = 3, itm = 10, j, n = 5;
// k=newelement index, j=tmp,n=current size
printf("Befor Insertion\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
j = n;
for (j; j >= k; j--)
{
arr[j + 1] = arr[j];
}
arr[k] = itm;
n = n + 1;
printf("\nAfter Insertion\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
#include <stdio.h>
int main()
{
int arr[10] = {1, 2, 3, 4, 5}, i, k = 3, j, n = 5;
// k= kth element to be delete, j= tmp,n=current size;
printf("Befor Deletion\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
j = k - 1; // index starts at 0 not 1
for (j; j < n; j++)
{
arr[j] = arr[j + 1];
}
n = n - 1;
printf("\nAfter Deletion\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
#include <stdio.h>
int main()
{
int arr[] = {10, 3, 30, 52, 33, 44, 25}, i, j = -1, f, n = 5;
// f find place of of element, n=size, j=tmp
f = 30; // find place of 30
for (i = 0; i < n; i++)
{
if (arr[i] == f)
{
j = i;
break;
}
}
if (j == -1)
{
printf("Element Not Found");
}
else
{
printf("Element %d Found at place: %d ", f, j + 1);
}
}
#include <stdio.h>
int main()
{
int arr[] = {10, 3, 30, 52, 33, 44, 25}, i, k, itm = 50, n = 5;
// kth element to be update with Itm,n=size, j=tmp
k = 3; // kth place elemntof 30
printf("Before Updation\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
arr[k - 1] = itm; // Update happens here.
printf("\nAfter Updation\n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}
}
#include <stdio.h>
int main()
{
// Assume 4x5 sparse matrix
int sparseMatrix[4][5] =
{
{0, 0, 3, 0, 4},
{0, 0, 5, 7, 0},
{0, 0, 0, 0, 0},
{0, 2, 6, 0, 0}};
int size = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
printf("%d ", sparseMatrix[i][j]);
if (sparseMatrix[i][j] != 0)
size++;
}
printf("\n");
}
int compactMatrix[3][size];
// Making of new compact matrix
int k = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 5; j++)
{
if (sparseMatrix[i][j] != 0)
{
compactMatrix[0][k] = i;
compactMatrix[1][k] = j;
compactMatrix[2][k] = sparseMatrix[i][j];
k++;
}
}
}
// Display compact matrix
printf("\n After Compacting\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < size; j++)
{
printf("%d ", compactMatrix[i][j]);
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int val;
struct Node *next;
};
int main()
{
struct Node *newNode, *head, *temp;
head = NULL;
int c = 1;
while (c == 1)
{
newNode = (struct Node *)malloc(sizeof(struct Node));
printf("\nEnter new Data: ");
scanf("%d", &newNode->val);
newNode->next = NULL;
if (head == NULL)
{
head = temp = newNode;
}
else
{
temp->next = newNode;
temp = newNode;
}
printf("\nPress 1 to continue, 0 to display List created: ");
scanf("%d", &c);
}
temp = head;
printf("list is");
while (temp != NULL)
{
printf(" -> %d", temp->val);
temp = temp->next;
}
return 0;
}
// Insert at End, at Beginning, at specidfic Place, see list, see length
// All code
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int val;
struct Node *nxt;
};
struct Node *head = NULL, *nw;
struct Node *createNode()
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
printf("\nEnter Data: ");
scanf("%d", &newNode->val);
newNode->nxt = NULL;
return newNode;
}
void seeList(struct Node *temp)
{
if (temp == NULL)
{
printf("\nList is Empty.");
}
else
{
printf("\nList is");
while (temp != NULL)
{
printf(" -> %d", temp->val);
temp = temp->nxt;
}
}
}
int sizeList(struct Node *temp)
{
int c = 0;
if (temp == NULL)
{
printf("\nList is Empty.");
return 0;
}
else
{
while (temp != NULL)
{
c++;
temp = temp->nxt;
}
}
return c;
}
void addBeginning(struct Node *h, struct Node *n)
{
struct Node *t;
if (h == NULL)
{
head = n;
}
else
{
n->nxt = h;
head = n;
}
}
void addPlace(struct Node *h, struct Node *n, int p)
{
struct Node *t, *t2;
int i = 1;
if (h == NULL)
{
head = n;
}
else if (p == 1)
{
n->nxt = h;
head = n;
return;
}
else
{
t2 = t = h;
while (i < p)
{
t2 = t;
t = t->nxt;
i = i + 1;
}
t2->nxt = n;
n->nxt = t;
}
}
void addEnd(struct Node *h, struct Node *n)
{
struct Node *t;
if (h == NULL)
{
head = n;
}
else
{
t = h;
while (t->nxt != NULL)
{
t = t->nxt;
}
t->nxt = n;
}
}
int main()
{
int c = 1, t, p;
while (c == 1)
{
printf("\n\nEnter\n1.To add End of link list\n2.To add at Beginning of list\n3.To add at specidfic Place in list\n");
printf("4.To see full list\n5.Length of list\n6.QUIT\nChoice: ");
scanf("%d", &t);
switch (t)
{
case 1:
{
nw = createNode();
addEnd(head, nw);
break;
}
case 2:
{
nw = createNode();
addBeginning(head, nw);
break;
}
case 3:
{
printf("\nEnter Position: ");
scanf("%d", &p);
if (p > 0 && p <= sizeList(head))
{
nw = createNode();
addPlace(head, nw, p);
}
else
{
printf("\nNot possible sorry");
}
break;
}
case 4:
{
seeList(head);
break;
}
case 5:
{
printf("Length of list = %d ", sizeList(head));
break;
}
case 6:
{
c = 0;
break;
}
default:
{
printf("\nEnter Correct Option.");
}
}
}
return 0;
}
// Delete element at End, at Beginning, at specidfic Place, see list, see length
// All code
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int val;
struct Node *nxt;
};
struct Node *head, *tmp;
struct Node *createNode()
{
struct Node *nwNode = (struct Node *)malloc(sizeof(struct Node));
printf("\nEnter data: ");
scanf("%d", &nwNode->val);
nwNode->nxt = NULL;
return nwNode;
}
void addAtEnd(struct Node *h, struct Node *n)
{
if (h == NULL)
{
head = n;
}
else
{
tmp = head;
while (tmp->nxt != NULL)
{
tmp = tmp->nxt;
}
tmp->nxt = n;
}
}
void delAtBegn(struct Node *h)
{
if (h->nxt == NULL)
{
head = NULL;
}
else
{
tmp = h->nxt;
head = tmp;
}
}
void delAtEnd(struct Node *h)
{
struct Node *tmp2;
if (h->nxt == NULL)
{
head = NULL;
}
else
{
tmp = head;
while (tmp->nxt != NULL)
{
tmp2 = tmp;
tmp = tmp->nxt;
}
tmp2->nxt = NULL;
}
}
void delAtPlc(struct Node *h, int pla)
{
struct Node *tmp2;
int c = 1;
if (pla == 1)
{
delAtBegn(h);
}
else
{
tmp = head;
while (c < pla)
{
c = c + 1;
tmp2 = tmp;
tmp = tmp->nxt;
}
tmp2->nxt = tmp->nxt;
}
}
void seeList(struct Node *h)
{
if (h == NULL)
{
printf("Empty list");
}
else
{
tmp = head;
printf("\nList is");
while (tmp != NULL)
{
printf(" -> %d", tmp->val);
tmp = tmp->nxt;
}
}
}
int countList(struct Node *h)
{
int c = 1;
if (h == NULL)
{
printf("Empty list");
return 0;
}
else
{
tmp = head;
while (tmp != NULL)
{
c = c + 1;
tmp = tmp->nxt;
}
return c;
}
}
int main()
{
int c, f = 1, place;
while (f == 1)
{
printf("\nenter your choice:\n1.Delete at End\n2.Delete at Beginnig.\n3.Delete at specific place.\n4.To add at End\n5. To see full list");
printf("\n6.to QUIT\nchoice: ");
scanf("%d", &c);
switch (c)
{
case 1:
{
if (head != NULL)
{
delAtEnd(head);
}
else
{
printf("\nList is empty");
}
break;
}
case 2:
{
if (head != NULL)
{
delAtBegn(head);
}
else
{
printf("\nList is empty");
}
break;
}
case 3:
{
printf("\nPlace of element to delete: ");
scanf("%d", &place);
if (place > 0 && place <= countList(head))
{
delAtPlc(head, place);
}
else
{
printf("\n Sorry not possible");
}
break;
}
case 4:
{
tmp = createNode();
addAtEnd(head, tmp);
break;
}
case 5:
{
seeList(head);
break;
}
case 6:
{
f = 0;
break;
}
default:
{
printf("\n Enter correct option");
}
}
}
return 0;
}
// Create linked list and reverse.
// All code
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int val;
struct Node *nxt;
};
struct Node *head, *tmp;
struct Node *createNode()
{
struct Node *nwnode = (struct Node *)malloc(sizeof(struct Node));
printf("\nEnter Val: ");
scanf("%d", &nwnode->val);
nwnode->nxt = NULL;
return nwnode;
}
void addAtEnd(struct Node *h, struct Node *n)
{
if (h == NULL)
{
head = n;
}
else
{
tmp = h;
while (tmp->nxt != NULL)
{
tmp = tmp->nxt;
}
tmp->nxt = n;
}
}
void revList(struct Node *h)
{
struct Node *cur, *pre = NULL, *next;
next = h;
cur = h;
while (next != NULL)
{
next = next->nxt;
cur->nxt = pre;
pre = cur;
cur = next;
}
head = pre;
}
void seeList(struct Node *h)
{
if (h == NULL)
{
printf("Empty list");
}
else
{
tmp = head;
printf("\nList is");
while (tmp != NULL)
{
printf(" -> %d", tmp->val);
tmp = tmp->nxt;
}
}
}
int main()
{
int f = 1, c;
while (f == 1)
{
printf("\nEnter Choice\n1.Enter element to list\n2. Reverse List\n3.View List\n4.QUIT\nChoice: ");
scanf("%d", &c);
switch (c)
{
case 1:
{
addAtEnd(head, createNode());
break;
}
case 2:
{
revList(head);
break;
}
case 3:
{
seeList(head);
break;
}
case 4:
{
f = 0;
break;
}
default:
printf("\nEnter Correct option");
}
}
}