Following program gives Implementation of Doubly Linked List with its various operations:
/************************************************************** Author: Aniruddha S N* */#include<iostream>usingnamespacestd;// structure nodestructnode{intinfo;structnode*next;structnode*prev;};//function declarationsvoiddisplay_list();voidInsert_Element_List(intele);voidDeleteElementAtPos(intpos);voiddisplay_Prev_Next(intpos);voiddelete_Entire_list();//global variable firstnode*first=NULL;//main functionintmain(){intele=0;intch=0;intpos=0;//Menu to display Doubly linked list operationswhile(1){cout<<"**************DoublyLinkedListOperationsMenu*************\n";cout<<"1---->Insert an element in list\n";cout<<"2---->displayList\n";cout<<"3---->DeleteAnElement at specified position in list\n";cout<<"4---->Display Previous and Next ele of the mentioned node\n";cout<<"5---->exit\n";cout<<"***************************************************\n";cout<<"\n\n";cout<<"Enter your choice:\n";cin>>ch;//using switch case call respective functions for respective choicesswitch(ch){case1:cout<<"Enter the element to be inserted into list\n";cin>>ele;Insert_Element_List(ele);break;case2:display_list();break;case3:cout<<"Enter the position to delete elements\n";cin>>pos;DeleteElementAtPos(pos);break;case4:cout<<"Enter the Node Position to display previous and next ele\n";cin>>pos;display_Prev_Next(pos);break;default:delete_Entire_list();exit(0);}}return0;}//end of main//function to insert an element in singly linked listvoidInsert_Element_List(intele){node*temp;if(first==NULL){first=newnode();first->info=ele;first->next=NULL;first->prev=NULL;}else{node*temp1;temp1=newnode();temp=first;//find the end of listwhile(temp->next!=NULL)temp=temp->next;temp1->info=ele;temp1->next=NULL;//attach the new node at the end of listtemp->next=temp1;temp1->prev=temp;}}//function to display the elements in the listvoiddisplay_list(){if(first==NULL)cout<<"list doesnt have any elements";elseif(first->next==NULL){cout<<"List Contents:\n";cout<<first->info<<"\n";}else{node*temp;temp=first;cout<<"List Contents:\n";while(temp!=NULL){cout<<temp->info<<"\n";temp=temp->next;}}}//function to delete element at specified positionvoidDeleteElementAtPos(intpos){intflag=0;node*prev;node*temp;if(first==NULL){cout<<"List Empty\n";}elseif(pos>1&&first->next==NULL){cout<<"No element at specified position in list\n";}elseif(pos==1){temp=first;prev=temp;temp=temp->next;temp->prev=NULL;//re initiate firstfirst=temp;prev->next=NULL;prev->prev=NULL;deleteprev;prev=NULL;}// for all other positionselse{temp=first;flag=1;while(flag<pos&&temp->next!=NULL){prev=temp;temp=temp->next;flag++;}//check whether the specified position existsif(flag<pos){cout<<"No element at specified position in list\n";}//delete the node at the specified positionelse{prev->next=temp->next;temp->next->prev=prev;temp->next=NULL;temp->prev=NULL;deletetemp;temp=NULL;}}//end of else for all other positions}voiddisplay_Prev_Next(intpos){if(first==NULL)cout<<"List empty\n";elseif(first->next==NULL)cout<<"Only one element in List\n";//for other valid caseselse{node*temp=first;node*prev;intflag=1;while(flag<pos&&temp->next!=NULL){prev=temp;flag++;temp=temp->next;}//check whether its an invalid node positionif(flag<pos){cout<<"No element at specified position in the list\n";}//if the position is validelse{cout<<"Previous Node:"<<prev->info<<"\n";cout<<"Current Node:"<<temp->info<<"\n";if(temp->next==NULL)cout<<"Next Node:End of List\n";elsecout<<"Next Node:"<<temp->next->info<<"\n";}}//end of else other valid cases}//function to delete entire list and prevent memory leakage when exit is calledvoiddelete_Entire_list(){node*temp;node*curr;temp=first;if(first==NULL)return;while(temp->next!=NULL){curr=temp;temp=temp->next;deletecurr;}deletetemp;}
/************************************************************** Author: Aniruddha S N* */#include<iostream>usingnamespacestd;// structure nodestructnode{intinfo;structnode*next;};//function declarationsvoiddisplay_list();voidInsert_Element_List(intele);voidDeleteElementAtPos(intpos);//global variable firstnode*first=NULL;//main functionintmain(){intele=0;intch=0;intpos=0;//Menu to display single linked list operationswhile(1){cout<<"**************LinkedListOperationsMenu*************\n";cout<<"1---->Insert an element in list\n";cout<<"2---->displayList\n";cout<<"3---->DeleteAnElement at specified position in list\n";cout<<"4---->exit\n";cout<<"***************************************************\n";cout<<"\n\n";cout<<"Enter your choice:\n";cin>>ch;//using switch case call respective functions for respective choicesswitch(ch){case1:cout<<"Enter the element to be inserted into list\n";cin>>ele;Insert_Element_List(ele);break;case2:display_list();break;case3:cout<<"Enter the position to delete elements\n";cin>>pos;DeleteElementAtPos(pos);break;default:exit(0);}}return0;}//end of main//function to insert an element in singly linked listvoidInsert_Element_List(intele){node*temp;if(first==NULL){first=newnode();first->info=ele;first->next=NULL;}else{node*temp1;temp1=newnode();temp=first;//find the end of listwhile(temp->next!=NULL)temp=temp->next;temp1->info=ele;temp1->next=NULL;//attach the new node at the end of listtemp->next=temp1;}}//function to display the elements in the listvoiddisplay_list(){if(first==NULL)cout<<"list doesnt have any elements";elseif(first->next==NULL){cout<<"List Contents:\n";cout<<first->info<<"\n";}else{node*temp;temp=first;cout<<"List Contents:\n";while(temp!=NULL){cout<<temp->info<<"\n";temp=temp->next;}}}//function to delete element at specified positionvoidDeleteElementAtPos(intpos){intflag=0;node*prev;node*temp;if(first==NULL){cout<<"List Empty\n";}elseif(pos>1&&first->next==NULL){cout<<"No element at specified position in list\n";}elseif(pos==1){temp=first;prev=temp;temp=temp->next;//re initiate firstfirst=temp;deleteprev;prev=NULL;}// for all other positionselse{temp=first;flag=1;while(flag<pos&&temp->next!=NULL){prev=temp;temp=temp->next;flag++;}//check whether the specified position existsif(flag<pos){cout<<"No element at specified position in list\n";}//delete the node at the specified positionelse{prev->next=temp->next;deletetemp;temp=NULL;}}//end of else for all other positions}
This Program gives stack implementation using arrays, max stack size input
is considered as 100 during implementation, modify it suitably.
#include<stdio.h>#include<conio.h>//Max stack size can be 99//function declarationvoidpush(int*top,int*stackarray,intsize,intele);voidpop(int*top,int*stackarray);voiddisplay_stack(intstackarray[],inttop);//main functionvoidmain(){intstackarr[100];inttop=-1;intsize=0;intele=0;intch=0;printf("enter the stack size less than 100\n");scanf("%d",&size);//this menu will be displayed till user chooses to exitwhile(1){printf("***************stackOperationsMenu*****************\n");printf("1--------->push an element int stack\n");printf("2--------->pop an element into stack\n");printf("3--------->display stack contents\n");printf("otherNum------>exit the program\n");printf("Enter your choice\n");scanf("%d", &ch);switch(ch){case1:printf("Enter the element to be pushed to stack\n");scanf("%d", &ele);push( &top,stackarr,size,ele);break;case2:pop( &top,stackarr);break;case3:display_stack(stackarr,top);break;default:return;}//end of switch}//end of while}// end of main//function definitionsvoidpush(int*top,int*stackarray,intsize,intele){if(*top==size-1){printf("stack is full\nPlease pop some elements and try to insert again\n");}else{*top=*top+1;stackarray[*top]=ele;}}voidpop(int*top,int*stackarray){if(*top==-1)printf(" stack is empty no element to pop\n");else{printf("Popped element is %d",stackarray[*top--]);}}voiddisplay_stack(intstackarray[],inttop){inti=0;printf("Stack contents:\n");for(inti=0;i&lt;=top;i++){printf("%d\n",stackarray[i]);}
#include<iostream>#include<fstream>#include<string>usingnamespace std;int main(){
string strfile="filenamewithabsolutepath.txt";
ifstream ifptr;
ifptr.open(strfile);if(!ifptr.is_open()){cout<<"Unable to open file\n";}else{//read the file till the endwhile(!ifptr.eof()){
string line;
getline(ifptr,line);cout<<"line:"<<line<<"\n";}//end of while}//end of elsereturn0;}//end of main
#include<iostream>usingnamespace std;int main(){char* original_string="abcdefg";char* reverse_string;char* temp;int length_original=0;int length_reverse=0;
temp=original_string;//find the length of stringwhile(*temp!=NULL){
temp++;
length_original++;}//assign memory to reverse_string
reverse_string=newchar[length_original];while(length_original>0){
reverse_string[length_reverse++]=*--temp;
length_original--;}//store null as last character
reverse_string[length_reverse]='\0';cout<<"Original string:"<<original_string;cout<<"\nReverse string:"<<reverse_string;return0;}
An array is a collection of homogeneous variables, all the variables in the array should be of same datatype.
declaration of an array:an array is declared like a normal variable but it is followed by a pair of square braces.
#include<stdio.h>voidmain(){//the array size is specified in square bracesinta[10];//we can assign values during declaration itself using flower braces.floatf[5]={0,1,2,3,4};}
Sample program:
//This program demonstrates //1.How to store values in an array//2.How to access values stored in array#include<stdio.h>voidmain(){//Array declarationintarray[10];inti=0;//storing values in the arrayprintf("enter the values of 10 nos:\n");for(i=0;i<10;i++){scanf("%d",&array[i]);}//accessing the values stored in arrayfor(i=0;i<10;i++){printf("array location:%d has value:%d\n",i,array[i]);}}
Two dimensional arrays:
A two dimensional array can be considered as a matrix like storage structure, elements are stored in rows and columns.
general format:
variablename[m][n];
m is the number of rows
n is the number of columns.
ex:
int a[2][4]={1,2,3,4,5,6,7,8};
logical storage
[ 1 2 3 4
5 6 7 8]
Note:
The matrix like storage structure is only logical assumption, but the actual storage of variables in memory need not be so.
Sample program:
//This program demonstrates //1.How to store values in a 2Darray//2.How to access values stored in a 2D array#include<stdio.h>void main(){//Array declarationint array[2][2];int i=0;int j=0;//storing values in the arrayprintf("enter the values of 4 nos:\n");for( i=0;i<2;i++)//row traversal{for( j=0;j<2;j++)//column traversal{scanf("%d",&array[i][j]);}}//accessing the values stored in arrayfor( i=0;i<2;i++)//row traversal{for(j=0;j<2;j++)//column traversal{printf("array location:%d has value:%d\n",i,array[i][j]);}}}
enter the values of 4 nos:
1 2 3 4
array location:0 has value:1
array location:0 has value:2
array location:1 has value:3
array location:1 has value:4
Three Dimensional Arrays:
Three dimensional arrays are like multiple copies of the same matrix.
Standard format:
variablename[no of matrix][no of rows][no of columns]
ex: int a[2][3][3]; two matrices with 3 rows and 3 columns.
sample program:
//This program demonstrates //1.How to store values in a 3Darray//2.How to access values stored in a 3D array#include<stdio.h>void main(){//Array declarationint array[2][2][2];int i=0;int j=0;int k=0;//storing values in the arrayprintf("enter the values of 8 nos:\n");for(k=0;k<2;k++)//traverse the matrix copy{for( i=0;i<2;i++)//row traversal{for( j=0;j<2;j++)//column traversal{scanf("%d",&array[k][i][j]);}}}//accessing the values stored in arrayfor( k=0;k<2;k++)//traverse the matrix copy{printf("Matrix no:%d\n",k);for( i=0;i<2;i++)//row traversal{for(j=0;j<2;j++)//column traversal{printf("array location:%d has value:%d\n",i,array[k][i][j]);}//end of j loop}//end of i loop}//end of k loop}
enter the values of 8 nos:
1 2 3 4 5 6 7 8
Matrix no:0
array location:0 has value:1
array location:0 has value:2
array location:1 has value:3
array location:1 has value:4
Matrix no:1
array location:0 has value:5
array location:0 has value:6
array location:1 has value:7
array location:1 has value:8
By using three dimensional array, the elements were stored in two 2*2 matrices in the above program.