c_cpp dsahapersonal_algo_ds

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp dsahapersonal_algo_ds相关的知识,希望对你有一定的参考价值。

#include <stdio.h>
#include <stdlib.h>

/*

	Basic operations on a single linklist (Without using tail pointer)
	

*/


/*	Creating the structure of each node 	*/
struct linkList
{
	int data;
	struct linkList * next;
}*head;	//	The head pointer (Global)


//	Custom functions (Not related to linklist)
char * myToString(int val);






//	Linklist operations (Implemented in seperate functions)

void insertAtBegining(int key)
{
	struct linkList *n;	
	n = (struct linkList *)malloc(sizeof(struct linkList));	

	n->data = key;
	 if(head==NULL)
	 	n->next=NULL;
	 else
	 	n->next = head;

	 head=n;
}


void displayList()
{
	struct linkList *temp;
	temp = head;
	printf("Current list : ");
	while(temp!=NULL)
	{
		printf("%d -> ",temp->data );
		temp=temp->next;
	}
	printf(" null\n");
}


char* delAtBegining()
{
	char* s;
	int val;

	if(head==NULL)	
		return "No element to delete\n";

	val =  head->data;
	head = head->next;

	return myToString(val);
}


void insertAtEnd(int val)
{
	struct linkList *temp;
	temp=head;

	if(head!=NULL)
	{
		while((temp->next)!=NULL)
		{
			temp=temp->next;

		}
		temp->next = malloc(sizeof(struct linkList));
		temp=temp->next;
		temp->data = val;
	}
	else
	{
		insertAtBegining(val);	//	when head==NULL, no node exists
	}

}

char* delAtEnd()
{

	struct linkList *temp,*prev=NULL;
	int val;

	temp = head;
	if(head!=NULL)
	{
		while((temp->next)!=NULL)
		{	
			prev=temp;
			temp=temp->next;
		}

		val = temp->data;

		if(prev==NULL)
			head=NULL;
		else
			prev->next=NULL;

		return myToString(val);
	}
	else
	{
		return "No node to delete";
	}
}


void insertAfter(int afterWhich,int insertWhat)
{
	struct linkList *temp,*temp2;

	temp=head;

	while(temp!=NULL)
	{
		if(temp->data==afterWhich)
		{
			temp2=temp->next;

			struct linkList *n = malloc(sizeof(struct linkList));
			n->data=insertWhat;
			n->next=temp2;

			temp->next=n;
		}

		temp=temp->next;
	}
}




int main()
{
	head=NULL; 
	int ch;
	
					
	while(1)
	{
		displayList();
		printf("1 : Insert at begining\n");
		printf("2 : Display all\n");
		printf("3 : Delete at begining\n");
		printf("4 : Insert at end\n");
		printf("5 : Delete at end\n");
		printf("6 : To insert after a specific node\n");
		printf("\nEnter choice : ");
		scanf("%d",&ch);

		switch(ch)
		{
			case 1:	printf("Enter data : ");
					int key;
					scanf("%d",&key);
					insertAtBegining(key);
					break;
			case 2:	displayList();
					break;
			case 3:	printf("Deleted element = %s\n",delAtBegining() );
					break;
			case 4: printf("Enter data : ");
					scanf("%d",&key);
					insertAtEnd(key);
					break;
			case 5: printf("Deleted element = %s \n",delAtEnd() );
					break;
			case 6:	if(head==NULL)
						printf("No node exists\n");
					else
					{
						printf("Insert after : ");
						int afterWhich,insertWhat;
						scanf("%d",&afterWhich);
						printf("Value to insert : ");
						scanf("%d",&insertWhat);
						insertAfter(afterWhich,insertWhat);
					}
					break;


		}
	}

	return 0;
}

char * myToString(int val)
{
	char *s;
	int len = snprintf(NULL,0,"%d",val);

	s = malloc(len+1);
	snprintf(s,len+1,"%d",val);

	return s;
}
#include <stdio.h>
#include <stdlib.h>


/*		Ordered array and binary search		*/


void insertByLinearSearch(int [],int);

void displayCurrent(int[],int);

void insertByBinarySearch(int[],int);

void binarySearch(int[],int,int);

int main()
{
	int n;
	printf("Size of array = ");
	scanf("%d",&n);

	int arr[n];


	int i;
	for(i=0;i<n;i++)
	{
		insertByBinarySearch(arr,i);
		displayCurrent(arr,i);
	}	



	int valueToSearch;
	scanf("%d",&valueToSearch);
	binarySearch(arr,valueToSearch,n);

	return 0;
}


void insertByLinearSearch(int arr[],int n)	//	"i" of main = "n" of this function
{
	int i;
	int valueToInsert;
	printf("Next value : ");
	scanf("%d",&valueToInsert);

	for(i=0;i<n;i++)
	{
		if(arr[i]>=valueToInsert)
		{
			n++;
			for(int j=n;j>i;j--)	// Code to shift the rest elements at right
			{
				arr[j]=arr[j-1];
			}
			break;
		}
	}
	arr[i]=valueToInsert;
}


void binarySearch(int arr[],int valueToSearch,int size)
{
	int lower = 0;
	int upper = size-1;
	int middle;


	while(lower<=upper)
	{
		middle=(lower+upper)/2;

		if(arr[middle]==valueToSearch)
		{
			printf("%d found at index %d\n",valueToSearch,middle );
			return;
		}
		else if(valueToSearch>arr[middle])
			lower=middle+1;
		else
			upper=middle-1;
	}
	printf("%d not found\n", valueToSearch);
}


void insertByBinarySearch(int arr[],int n)
{

	int valueToInsert;
	scanf("%d",&valueToInsert);


	if(n==0)
	{
		arr[0]=valueToInsert;
		return;
	}
	int lower = 0;
	int upper = n-1;
	int middle;

	while(lower<=upper)
	{
		middle=(lower+upper)/2;

		if(arr[middle]==valueToInsert)
			break;
		else if(valueToInsert>arr[middle])
			lower = middle+1;
		else
			upper = middle-1;
	}

	int i;
	if(arr[middle]<valueToInsert)
		middle++;
	for(i=++n;i>middle;i--)
		arr[i]=arr[i-1];

	arr[middle]=valueToInsert;

}


void displayCurrent(int arr[],int n)
{
	int i=0;
	printf("Current array : ");
	for(i=0;i<=n;i++)
		printf("%d ",arr[i]);
	printf("\n");
}
#include <bits/stdc++.h>

#define SIZE 10

int main()
{
	int arr[SIZE];
	int i,key,flag,j,n;
	n=SIZE;

	//	1) Normal array Insertion

	for(i=0;i<SIZE;i++)
		scanf("%d",&arr[i]);









	//	2) Normal array Traversal

	for(i=0;i<SIZE;i++)
		printf("%d ",arr[i] );










	//	3) Normal array Search and update

	printf("Enter element to search : ");
	scanf("%d",&key);

	flag=0;

	for(i=0;i<SIZE;i++)
	{
		if(arr[i]==key)
		{
			flag=1;
			printf("%d found at %d\n",key,i);
		}
	}

	if(flag==0)
		printf("%d not found\n",key );














	//	4) Normal array search and deletion

	printf("Enter element to delete : ");
	scanf("%d",&key);

	for(i=0;i<n;i++)
	{
			if(arr[i]==key)
			{
				j=i;
				/*	Code to shift rest elements	*/
				for(;j<n-1;j++)
				{
					arr[j]=arr[j+1];
				}
				n--;
			}
	}


	printf("After deletion : ");
	for(i=0;i<n;i++)
		printf("%d ", arr[i]);









	

	return 0;
}

以上是关于c_cpp dsahapersonal_algo_ds的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp -_-公元前

c_cpp uv__next_timeout.c

c_cpp uv__handle_unref.c

c_cpp uv__handle_ref.c

c_cpp uv__handle_stop.c

c_cpp uv__handle_start.c