Linked list:
Create a linked list and delete elements as a user inputs(queue):
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void end(struct node **head,int value)
{
struct node *new_node=(struct node*)malloc(sizeof(struct node));
struct node*last=*head;
new_node->data=value;
new_node -> next = NULL;
if(*head==NULL)
{
*head = new_node;
return;
}
while(last->next!=NULL)
last=last->next;
last->next=new_node;
return;
}
void delete(struct node **head)
{
struct node *temp=*head;
*head=(*head)->next;
free(temp);
}
void display(struct node *head)
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
int main()
{
struct node *head=NULL;
int value,i,n,b;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&value);
end(&head,value);
}
display(head);
scanf("%d",&b);
for(i=0;i<b;i++)
{
delete(&head);
}
display(head);
}
Create a linked list and delete elements as a user inputs(stack):
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void push(struct node **head,int value)
{
struct node *new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=value;
new_node->next=*head;
*head=new_node;
}
void display(struct node *head)
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
printf("\n");
}
void delete(struct node **head)
{
struct node*temp=*head;
*head=(*head)->next;
free(temp);
}
int main()
{
struct node *head=NULL;
int value,i,n,m;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&value);
push(&head,value);
}
display(head);
scanf("%d",&m);
for(i=0;i<m;i++)
{
delete(&head);
}
display(head);
}
Implementation of a queue in linked
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *link;
};
void display(struct Node *head)
{
struct Node *temp = head;
while (temp != 0)
{
printf("%d->", temp->data);
temp = temp -> link;
}
printf("NULL\n");
}
int enqueue(struct Node **head, int value)
{ struct Node *new_node=(struct Node*)malloc(sizeof(struct Node));
struct Node*last=*head;
new_node->data=value;
new_node -> link = NULL;
if(*head==NULL)
{
*head = new_node;
return;
}
while(last->link!=NULL)
last=last->link;
last->link=new_node;
return;
}
int dequeue(struct Node **head, int* data)
{
struct Node* temp=*head;
if(*head==NULL)
return 0;
*head=(*head)->link;
free(temp);
return 1;
}
void main()
{
int i, enqueue_count, dequeue_count, tmp;
struct Node *head = NULL;
scanf("%d", &enqueue_count);
for(i = 0; i < enqueue_count; i++)
{
scanf("%d", &tmp);
if(0 == enqueue(&head, tmp))
{
printf("Enqueue failed\n");
}
}
scanf("%d", &dequeue_count);
for(i = 0; i < dequeue_count; i++)
{
int val;
if(0 == dequeue(&head, &val))
{
printf("Dequeue failed\n");
}
}
display(head);
}
Implement stack using linked list:
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *next;
};
int push(struct Node **head, int value);
int pop(struct Node **head, int* value);
void display(struct Node *head);
int main()
{
struct Node *head = NULL;
int push_count, pop_count, val, tmp, i;
scanf("%d", &push_count);
for(i = 1; i <= push_count; i++)
{
scanf("%d", &tmp);
if(0 == push(&head, tmp))
{
printf("Push failed\n");
}
}
scanf("%d", &pop_count);
for(i = 1; i <= pop_count; i++)
{
if(0 == pop(&head, &val))
{
printf("Pop failed\n");
}
}
display(head);
return 0;
}
// Returns 1 is Success
// 0 if failure
int push(struct Node **head, int value)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data =value;
new_node->next = *head;
*head = new_node;
return 1;
}
// Returns 1 if success and 0 if failure
// The popped data is stored at *value
int pop(struct Node **head, int* value)
{
struct Node* temp=*head;
if(*head==NULL)
return 0;
*head=(*head)->next;
free(temp);
return 1;
}
void display(struct Node *head)
{
if(head == NULL)
{
printf("Stack is Empty!!!\n");
return;
}
struct Node *curr_node = head;
while(curr_node != NULL)
{
printf("%d->",curr_node->data);
curr_node = curr_node -> next;
}
printf("NULL");
return;
}
To Print Nth element from the end of a linked list:
//C program to find n'th node from end
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
int printNthFromLast(struct Node* head, int n)
{
int len = 0, i;
struct Node *temp = head;
while (temp != NULL)
{
temp = temp->next;
len++;
}
if (len < n)
return 0;
temp = head;
for (i = 1; i < len-n+1; i++)
temp = temp->next;
printf ("%d", temp->data);
return 1;
}
void insert(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
int n;
// create linked list
insert(&head, 30);
insert(&head, 25);
insert(&head, 20);
insert(&head, 15);
insert(&head, 10);
scanf("%d",&n);
if(0==(printNthFromLast(head, n)))
printf("No node found\n");
return 0;
}
//C program to find n'th node from end
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
int printNthFromLast(struct Node* head, int n)
{
int len = 0, i;
struct Node *temp = head;
while (temp != NULL)
{
temp = temp->next;
len++;
}
if (len < n)
return 0;
temp = head;
for (i = 1; i < len-n+1; i++)
temp = temp->next;
printf ("%d", temp->data);
return 1;
}
void insert(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
struct Node* head = NULL;
int n;
// create linked list
insert(&head, 30);
insert(&head, 25);
insert(&head, 20);
insert(&head, 15);
insert(&head, 10);
scanf("%d",&n);
if(0==(printNthFromLast(head, n)))
printf("No node found\n");
return 0;
}
Delete the second last:
#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
void print_list(struct Node *list)
{
while(list)
{
printf("%d->", list->data);
list = list->next;
}
printf("NULL");
}
void insertAtBeginning(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
struct Node* find_third_from_last(struct Node* head)
{
struct Node *slow,*fast;
fast=head->next->next;
slow=head;
while(fast->next!=NULL)
{
fast=fast->next;
slow=slow->next;
}
return slow;}
void delete_2nd_last(struct Node **head)
{
struct Node *node;
node= *head;
if(node==NULL||node->next==NUL L)
{
return;
}
if(node->next->next==NULL)
{
*head=(*head)->next;
free(node);
return;
}
node=find_third_from_last(*hea d);
struct Node *second_last=node->next;
node->next=node->next->next;
free(second_last);
}
int main()
{
struct Node* head = NULL;
int tmp;
while(1)
{
scanf("%d",&tmp);
if(tmp == -1)
{
break;
}
insertAtBeginning(&head, tmp);
}
delete_2nd_last(&head);
print_list(head);
return 0;
}
#include<stdio.h>
Reversal:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
void create(struct node **);
void reverse(struct node **);
void release(struct node **);
void display(struct node *);
int main()
{
struct node *p = NULL;
int n;
create(&p);
printf("Displaying the nodes in the list:\n");
display(p);
reverse(&p);
printf("Displaying the reversed list:\n");
display(p);
release(&p);
return 0;
}
void reverse(struct node **head)
{
struct node *p, *q, *r;
p = q = r = *head;
p = p->next->next;
q = q->next;
r->next = NULL;
q->next = r;
while (p != NULL)
{
r = q;
q = p;
p = p->next;
q->next = r;
}
*head = q;
}
void create(struct node **head)
{
int c, ch;
struct node *temp, *rear;
do
{
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
// printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
// printf("\n");
}
void display(struct node *p)
{
while (p != NULL)
{
printf("%d\t", p->num);
p = p->next;
}
printf("\n");
}
void release(struct node **head)
{
struct node *temp = *head;
*head = (*head)->next;
while ((*head) != NULL)
{
free(temp);
temp = *head;
(*head) = (*head)->next;
}
}
Delete the the element from end:
/*
* C program to create a linked list and display the elements in the list
*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node
{
int num;
struct node *link;
};
typedef struct node NODE;
void display(NODE *head)
{
NODE *temp = head;
while (temp != 0)
{
printf("%d=>", temp->num);
temp = temp -> link;
}
printf("NULL\n");
}
struct node *insertAtEnd(struct node *head, int data)
{
NODE *node = head;
if(node == NULL)
{
node = (NODE *) malloc(sizeof(NODE));
node->num = data;
node->link = NULL;
// Return the new head
return node;
}
while(node->link != NULL)
{
node = node->link;
}
NODE *last_node;
last_node = (NODE *) malloc(sizeof(NODE));
last_node->num = data;
last_node->link = NULL;
node->link = last_node;
return head;
}
// Returns new head after deletion
NODE* deleteAtEnd(NODE *head)
{
if(head == NULL)
{
return NULL;
}
if(head->link == NULL)
{
free(head);
return NULL;
}
NODE *node = head;
while(node->link->link != NULL)
{
node = node->link;
}
NODE *tmp = node->link;
node->link = NULL;
free(tmp);
return head;
}
void main()
{
int i, insert_count, delete_count, tmp;
NODE *head = NULL;
scanf("%d",&insert_count);
for(i = 0; i < insert_count; i++)
{
scanf("%d", &tmp);
head = insertAtEnd(head, tmp);
}
scanf("%d",&delete_count);
for(i = 0; i < delete_count; i++)
{
head = deleteAtEnd(head);
}
display(head);
}
* C program to create a linked list and display the elements in the list
*/
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node
{
int num;
struct node *link;
};
typedef struct node NODE;
void display(NODE *head)
{
NODE *temp = head;
while (temp != 0)
{
printf("%d=>", temp->num);
temp = temp -> link;
}
printf("NULL\n");
}
struct node *insertAtEnd(struct node *head, int data)
{
NODE *node = head;
if(node == NULL)
{
node = (NODE *) malloc(sizeof(NODE));
node->num = data;
node->link = NULL;
// Return the new head
return node;
}
while(node->link != NULL)
{
node = node->link;
}
NODE *last_node;
last_node = (NODE *) malloc(sizeof(NODE));
last_node->num = data;
last_node->link = NULL;
node->link = last_node;
return head;
}
// Returns new head after deletion
NODE* deleteAtEnd(NODE *head)
{
if(head == NULL)
{
return NULL;
}
if(head->link == NULL)
{
free(head);
return NULL;
}
NODE *node = head;
while(node->link->link != NULL)
{
node = node->link;
}
NODE *tmp = node->link;
node->link = NULL;
free(tmp);
return head;
}
void main()
{
int i, insert_count, delete_count, tmp;
NODE *head = NULL;
scanf("%d",&insert_count);
for(i = 0; i < insert_count; i++)
{
scanf("%d", &tmp);
head = insertAtEnd(head, tmp);
}
scanf("%d",&delete_count);
for(i = 0; i < delete_count; i++)
{
head = deleteAtEnd(head);
}
display(head);
}
Second largest element in linked list:
#include<stdio.h>
#include<stdlib.h>
/* Link list node */
struct Node
{
int data;
struct Node* next;
};
void print_list(struct Node *list)
{
while(list)
{
printf("%d->", list->data);
list = list->next;
}
printf("NULL");
}
void insertAtBeginning(struct Node** head_ref, int new_data)
{
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
// Returns error code. 0 if success and 1 if failure
int find_sec_last_elem(struct Node *head, int *sec_last_value)
{
if(head==NULL ||head->next==NULL)
{
return 1;
}
else
{
while(head->next->next!=NULL)
{
head=head->next;
}
}
*sec_last_value=head->data;
return 0;
}
int main()
{
struct Node* head = NULL;
int tmp;
while(1)
{
scanf("%d",&tmp);
if(tmp == -1)
{
break;
}
insertAtBeginning(&head, tmp);
}
int sec_last_elem_data;
int error = find_sec_last_elem(head, &sec_last_elem_data);
if(error == 0)
{
printf("%d", sec_last_elem_data);
}
else
{
printf("Failed to find second last element");
}
return 0;
}
Another example:
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
struct node
{
int data;
struct node *ptr;
};
void display(struct node *head)
{
struct node *temp = head;
while (temp != 0)
{
printf("%d=>", temp->data);
temp = temp -> ptr;
}
printf("NULL\n");
}
void end(struct node **head,int value)
{
struct node *newnode=(struct node*)malloc(sizeof(struct node));
struct node *temp=*head;
newnode->data=value;
newnode->ptr = NULL;
if(*head==NULL)
{
*head = newnode;
return;
}
while(temp->ptr!=NULL)
{temp=temp->ptr;}
temp->ptr=newnode;
return;
}
void main()
{
int n, i,a;
scanf("%d",&n);
struct node *head=NULL;
for(i=0;i<n;i++)
{
scanf("%d",&a);
end(&head,a);
}
display(head);
}
Create and display a linked list in compile time (end):
#include <stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void end(struct node **head,int value)
{
struct node *new_node=(struct node*)malloc(sizeof(struct node));
struct node*last=*head;
new_node->data=value;
new_node -> next = NULL;
if(*head==NULL)
{
*head = new_node;
return;
}
while(last->next!=NULL)
last=last->next;
last->next=new_node;
return;
}
void display(struct node *head)
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
int main()
{
struct node *head = NULL;
end(&head, 5);
end(&head, 1);
end(&head, 3);
display(head);
return 0;
}
Create a linked list and display it in compile time(start) :
#include <stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void push(struct node **head,int value)
{
struct node *new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=value;
new_node -> next = *head;
*head = new_node;
}
void display(struct node *head)
{
struct node *temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
int main()
{
struct node *head = NULL;
push(&head, 5);
push(&head, 1);
push(&head, 3);
display(head);
return 0;
}
Create linked list and display it in run time(start):
#include <stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
void push(struct node **head,int value)
{
struct node *newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=value;
newnode->next=*head;
*head=newnode;
}
void display(struct node *head)
{
struct node *temp=head;
while(temp != NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
int main()
{ struct node *head=NULL;
int i,n,a;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%d",&a);
push(&head,a);}
display(head);
return 0;
}
Comments
Post a Comment