Notes
Notes - notes.io |
#include<stdio.h>
#include<conio.h>
#define MAX 5
typedef struct stag
{
int top; int ele[MAX];
} STACK;
void initialize(STACK *sp)
{
sp->top = -1;
}
void push(STACK *sp, int x)
{
if(sp->top == MAX-1) printf("nStack Overflow"); else sp->ele[++sp->top] = x;
}
int pop(STACK *sp)
{ int x; if(sp->top == -1) printf("nStack Underflow"); else x = sp->ele[sp->top--]; return x;
}
int isfull(STACK *sp)
{
if(sp->top == MAX-1) return 1; else return 0;
}
int isempty(STACK *sp)
{
if(sp->top == -1) return 1; else return 0;
}
int main()
{
STACK s;
int x; initialize(&s); push(&s, 70); push(&s, 80);
while(!isempty(&s))
{
x = pop(&s); printf("n The popped element is %d", x);
}
return 0;
}
_______________________________________________________________________________________________________________________________________________________________
SELECTION SORT
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{ min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
if(min_idx != i)
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("n");
}
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: n");
printArray(arr, n);
return 0;
}
___________________________________________________________________________________________________________________________________________________________________________
SINGLY LINKED LIST
#include <stdio.h>
#include <stdlib.h>
struct Node
{ int data;
struct Node *next;
};
void push(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;
}
void insertAfter(struct Node* prev_node, int new_data)
{
if (prev_node == NULL)
{
printf("the given previous node cannot be NULL"); return;
}
struct Node* new_node =(struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = prev_node->next;
prev_node->next = new_node;
}
void append(struct Node** head_ref, int new_data)
{
/* 1. allocate node */
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
struct Node *last = *head_ref;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
{
*head_ref = new_node;
return;
}
while (last->next != NULL)
last = last->next;
last->next = new_node;
return;
}
void printList(struct Node *node)
{
while (node != NULL)
{
printf(" %d ", node->data);
node = node->next;
}
}
int main()
{
struct Node* head = NULL; append(&head, 6);
push(&head, 7);
push(&head, 1);
append(&head, 4);
insertAfter(head->next, 8);
printf("n Created Linked list is: "); printList(head);
return 0; }
Deletion
#include <stdio.h>
#include <stdlib.h>
struct node {
int data; struct node *next;
}*head;
void createList(int n); void deleteFirstNode();
void displayList();
int main() { int n, choice;
printf("Enter the total number of nodes: "); scanf("%d", &n);
createList(n);
printf("nData in the list n"); displayList();
printf("nPress 1 to delete first node: ");
scanf("%d", &choice); if(choice == 1)
deleteFirstNode();
printf("nData in the list n"); displayList();
return 0; }
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
head = (struct node *)malloc(sizeof(struct node));
if(head == NULL)
{
printf("Unable to allocate memory.");
} else
{
printf("Enter the data of node 1: "); scanf("%d", &data);
head->data = data;
head->next = NULL;
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory."); break;
}
else
{
printf("Enter the data of node %d: ", i); scanf("%d", &data);
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
printf("SINGLY LINKED LIST CREATED SUCCESSFULLYn");
}
}
void deleteFirstNode()
{
struct node *toDelete;
if(head == NULL)
{
printf("List is already empty.");
} else
{
toDelete = head;
head = head->next;
printf("nData deleted = %dn", toDelete->data);
free(toDelete);
printf("SUCCESSFULLY DELETED FIRST NODE FROM LISTn");
}
}
void displayList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
} else
{ temp = head;
while(temp != NULL)
{
printf("Data = %dn", temp->data); temp = temp->next;
}
}
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team