Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //WAP a Program to create an array.
- #include<stdio.h>
- #include<conio.h>
- main()
- {
- int a[5];
- int i;
- printf("Enter the elements of array:");
- for(i=0; i<5; i++)
- scanf("\n%d", &a[i]);
- //Display Array list
- printf("The inserted array elemets are:");
- for(i=0; i<5; i++)
- printf("\n%d", a[i]);
- getch();
- }
- ---------------------------------------
- //Insertion in array
- #include<stdio.h>
- #include<conio.h>
- void main()
- {
- int a[100], i, n, v;
- char ch;
- printf("Enter the size of array: ");
- scanf("\n%d", &n);
- printf("\nEnter the Elements of Array: \n");
- for(i=0; i<n; i++)
- scanf("\n%d", &a[i]);
- printf("\nThe Elements of Array are:");
- for(i=0; i<n; i++)
- printf("\n%d", a[i]);
- back:
- printf("\nDo you want to add more? If Yes type y or Y and Press Enter.");
- fflush(stdin);
- scanf("%c", &ch);
- if (ch=='y' || ch=='Y')
- {
- printf("Enter Data: ");
- scanf("%d", &v);
- n++;
- a[n-1]=v;
- printf("\nThe Elements of Array are:");
- for(i=0; i<n; i++)
- {
- printf("\n%d", a[i]);
- }
- goto back;
- }
- getch();
- }
- ---------------------------------------
- //Insertion at nth position in array
- #include<stdio.h>
- #include<conio.h>
- void main()
- {
- int a[100], i, n, v, p;
- char ch;
- printf("Enter the size of array: ");
- scanf("\n%d", &n);
- printf("\nEnter the Elements of Array: \n");
- for(i=0; i<n; i++)
- scanf("\n%d", &a[i]);
- printf("\nThe Elements of Array are:");
- for(i=0; i<n; i++)
- printf("\n%d", a[i]);
- printf("\nDo you want to add more? If Yes type y or Y and Press Enter.");
- fflush(stdin);
- scanf("%c", &ch);
- if (ch=='y' || ch=='Y')
- {
- printf("Enter the location where you wish to insert an element\n");
- scanf("%d", &p);
- printf("Enter the value to insert\n");
- scanf("%d", &v);
- for (i=n-1; i>= p-1; i--)
- a[i+1] = a[i];
- a[p-1]=v;
- printf("Resultant array is\n");
- for (i= 0; i<=n; i++)
- printf("%d\n", a[i]);
- }
- getch();
- }
- ---------------------------------------
- //Deletion in array at nth position
- #include<stdio.h>
- #include<conio.h>
- void main()
- {
- int a[100], i, n, v, p;
- printf("Enter the size of array: ");
- scanf("\n%d", &n);
- printf("\nEnter the Elements of Array: \n");
- for(i=0; i<n; i++)
- scanf("\n%d", &a[i]);
- printf("\nThe Elements of Array are:");
- for(i=0; i<n; i++)
- printf("\n%d", a[i]);
- printf("\nEnter the location where you wish to delete element");
- scanf("%d", &p);
- if (p>=n+1)
- printf("Deletion not possible.\n");
- else
- {
- for(i=p-1; i<n-1; i++)
- a[i] = a[i+1];
- for(i=0; i<n-1; i++)
- printf("%d\n", a[i]);
- }
- getch();
- }
- ---------------------------------------
- //Reverse of an array.
- #include<stdio.h>
- #include<conio.h>
- void main()
- {
- int a[100], i, n;
- printf("Enter the size of array: ");
- scanf("\n%d", &n);
- printf("\nEnter the Elements of Array: \n");
- for(i=0; i<n; i++)
- scanf("\n%d", &a[i]);
- //Display Reverse Array list
- printf("The Reverse array elemets are:");
- for(i=n-1; i>=0; i--)
- {
- printf("\n%d", a[i]);
- }
- getch();
- }
- ---------------------------------------
- //Merging of two arrays
- #include<stdio.h>
- #include<conio.h>
- int main()
- {
- int a1[25], a2[25], a3[50], i, m, n, temp;
- printf("Enter the size of Array1: ");
- scanf("\n%d", &m);
- printf("\nEnter the Array1 Elements: \n");
- for(i=0; i<m; i++)
- {
- scanf("%d", &a1[i]);
- }
- printf("\nEnter the size of Array2: ");
- scanf("\n%d", &n);
- printf("\nEnter the Array2 Elements: \n");
- for(i=0; i<n; i++)
- {
- scanf("%d", &a2[i]);
- }
- printf("\nThe Elements of Array1:");
- for(i=0; i<m; i++)
- {
- printf("\n%d", a1[i]);
- }
- printf("\nThe Elements of Array2:");
- for(i=0; i<n; i++)
- {
- printf("\n%d", a2[i]);
- }
- int size = m+n;
- a3[size];
- back:
- printf("\nPress 1 if you want to merge Array 1 with Array 2\n");
- printf("Press 2 if you want to merge Array 2 with Array 1\n");
- fflush(stdin);
- scanf("\n%d", &temp);
- if(temp==1)
- {
- for(i=0; i<m; i++)
- a3[i]=a1[i];
- for(i=m; i<size; i++)
- a3[i]=a2[i-m];
- printf("\nArray1 + Array2 is: \n");
- for(i=0; i<size; i++)
- printf("\n%d", a3[i]);
- return 0;
- }
- else if(temp==2)
- {
- for(i=0; i<n; i++)
- a3[i]=a2[i];
- for(i=n; i<size; i++)
- a3[i]=a1[i-n];
- printf("\nArray2 + Array1 is: \n");
- for(i=0; i<size; i++)
- printf("\n%d", a3[i]);
- return 0;
- }
- else
- {
- printf("Entered wrong choice\n");
- goto back;
- }
- }
- ---------------------------------------
- #include <stdio.h>
- #include <malloc.h>
- #include <stdlib.h>
- struct node {
- int value;
- struct node *next;
- };
- void insert();
- void display();
- typedef struct node DATA_NODE;
- DATA_NODE *head_node, *first_node, *temp_node = 0, *prev_node, next_node;
- int data;
- int main()
- {
- int option = 0;
- printf("Singly Linked List Example - All Operations\n");
- while (option < 3)
- {
- printf("\nOptions\n");
- printf("1 : Insert into Linked List \n");
- printf("2 : Display from Linked List \n");
- printf("Others : Exit()\n");
- printf("Enter your option:");
- scanf("%d", &option);
- switch (option)
- {
- case 1:
- insert();
- break;
- case 2:
- display();
- break;
- default:
- break;
- }
- }
- return 0;
- }
- void insert()
- {
- printf("\nEnter Element for Insert Linked List : \n");
- scanf("%d", &data);
- temp_node = (DATA_NODE *) malloc(sizeof (DATA_NODE));
- temp_node->value = data;
- if (first_node == 0)
- {
- first_node = temp_node;
- } else {
- head_node->next = temp_node;
- }
- temp_node->next = 0;
- head_node = temp_node;
- }
- void display() {
- int count = 0;
- temp_node = first_node;
- printf("\nDisplay Linked List : \n");
- while (temp_node != 0) {
- printf("# %d # ", temp_node->value);
- count++;
- temp_node = temp_node -> next;
- }
- printf("\nNo Of Items In Linked List : %d\n", count);
- }
- ---------------------------------------
- #include<stdio.h>
- #include<conio.h>
- void main()
- {
- int a[50], top, pos, i, n, x;
- char ch;
- printf("Enter the size: ");
- scanf("%d", &n);
- printf("Enter the Elements: \n");
- top=n;
- for(i=0; i<top; i++)
- {
- scanf("%d", &a[i]);
- }
- //The Elements in the Stack
- printf("Elements in the Stack: \n");
- for(i=top-1; i>=0; i--)
- {
- printf("%d \n", a[i]);
- }
- back:
- fflush(stdin);
- printf("Press d to delete an Element or i to insert an Element.");
- scanf("%c", &ch);
- if(ch=='d' || ch=='D')
- {
- printf("Enter the position you want to delete an element:\n");
- scanf("%d", &pos);
- if (pos-1==top-1);
- top--;
- printf("The updated elements in stack are:\n");
- for(i=top-1; i>=0; i--);
- }
- }
- ---------------------------------------
- #include<stdio.h>
- main(){
- int top,n,pos,i,x;
- int a[100];
- char ch;
- printf("Enter the size:\n");
- scanf("%d",&n);
- printf("Enter %d elements:\n",n);
- top=n;
- for(i=0;i<top;i++){
- scanf("%d",&a[i]);
- }
- printf("The elements in stack are:\n");
- for(i=top-1;i>=0;i--){
- printf("%d\n",a[i]);
- }
- back:
- fflush(stdin);
- printf("Press d if you want to delete an element or i to insert an element:\n");
- scanf("%c",&ch);
- if(ch=='d' || ch=='D'){
- printf("Enter the position you want to delete an element:\n");
- scanf("%d",&pos);
- if(pos-1==top-1){
- top--;
- printf("The updated elements in stack are:\n");
- for(i=top-1;i>=0;i--){
- printf("%d\n",a[i]);
- }
- goto back;
- }
- else{
- printf("Cann't remove the element at this position.\n");
- goto back;
- }
- }
- else if(ch=='i' || ch=='I'){
- printf("Enter the element you want to add:\n");
- scanf("%d",&x);
- if(top>=n){
- printf("Overflow\n");
- goto back;
- }
- else{
- top++;
- a[top-1]=x;
- printf("The updated elements in stack are:\n");
- for(i=top-1;i>=0;i--){
- printf("%d\n",a[i]);
- }
- goto back;
- }
- }
- else{
- return 0;
- }
- }
- ---------------------------------------
- ---------------------------------------
Add Comment
Please, Sign In to add comment