基本数据结构实现(Data Structures and Algorithm Analysis 2rd Edition)

链表操作:

#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <algorithm>
#include <vector>
using   namespace   std;

typedef struct student
{
	int data;
	struct student* next;
}node;

node* Create()
{
	printf("Enter key value:\n");
	int x;
	node* L = (node*)malloc(sizeof(node));
	L->next = NULL;
	node* pos = L;
	while(scanf("%d",&x)){
		pos->next = (node*)malloc(sizeof(node));
		pos->next->data = x;
		pos->next->next = NULL;
		pos = pos->next;
	}
	printf("Queue established!\n");
	return L;
}
void PrintQueue(node* L)
{
	node* pos = L;
	while(pos->next != NULL){
		printf("%d	",pos->next->data);	
		pos = pos->next;
	}
	printf("\n");	
}

node* FindPrevious(node* L,int key)
{
	if(L->next==NULL){
		printf("Empty List!\n");
		exit(0);
	}
	node* pos = L;
	while(pos->next!=NULL && pos->next->data != key){
		pos = pos->next;
	}
	return pos;//²éÕҳɹ¦£¬·µ»ØÏàÓ¦½ÚµãµØÖ·£»·ñÔò·µ»Ø×îºóÒ»¸ö½ÚµãµØÖ·
}


int IsLast(node* p)
{
	if(p->next == NULL)	return 1;
	else return 0;
}

void Insert(node* p,int key)
{
	node* temp = p;
	temp = (node*)malloc(sizeof(node));
	temp->data = key;
	temp->next = p->next;
	p->next = temp;
}

void DeleteEle(node* L,int key)
{
	if(L==NULL){
		printf("Empty List!\n");
		exit(0);
	}
	node* p = FindPrevious(L,key);
	if(!IsLast(p)){
		node* temp = p->next;
		p->next = temp->next;
		free(temp);
	}
}

void DeleteList(node* L)
{
	node* p = L->next;
	L->next = NULL;
	while(p!=NULL)
	{
		node* temp = p->next;
		free(p);
		p=temp;
	}
}


void main(void)
{
	node* p = Create();
	PrintQueue(p);
	//DeleteEle(p,2);
	//printf("After Deletion:\n");	
	//Insert(p,4);
	//printf("After Insertion:\n");
	DeleteList(p);
	printf("After DeleteList:\n");
	PrintQueue(p);
}


 栈操作(链表):

#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <algorithm>
#include <vector>
using   namespace   std;

typedef struct student
{
	int data;
	struct student* next;
}node;

node* CreateStack()
{
	node* S;
	S = (node*)malloc(sizeof(node));  
	S->next = NULL;
	return S;
}

void Push(node* S,int key)
{
	node* temp = (node*)malloc(sizeof(node));
	temp->data = key;
	temp->next = S->next;
	S->next = temp;
}

node* Top(node* S)
{
	if(S->next==NULL){  
		printf("Empty Stack!\n");  
		exit(0);  
	}  
	return S->next;
}

void Pop(node* S)
{
	if(S->next==NULL){  
		printf("Empty Stack!\n");  
		exit(0);  
	}  
	node* temp = S->next;
	S->next = temp->next;
	free(temp);
}
void PrintStack(node* L)  
{  
    node* pos = L;  
    while(pos->next != NULL){  
        printf("%d  ",pos->next->data);     
        pos = pos->next;  
    }  
    printf("\n");     
}

void main(void)
{
	printf("Enter key value:\n");
	int key;
	node* S = CreateStack();
	while(scanf("%d",&key)){
		Push(S,key);
	}
	PrintStack(S);
	Pop(S);
	printf("After one Pop:\n");
	PrintStack(S);
	Push(S,key);	
	printf("After Push %d:\n",key);
	PrintStack(S);	
}

栈操作(数组):
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <algorithm>
#include <vector>
using   namespace   std;

typedef struct S
{
	int Capacity;
	int TopOfStack;
	int* Array;
}Stack;

Stack* CreateStack()
{
	Stack* S = (Stack*)malloc(sizeof(Stack)); 
	S->Capacity = 20;
	S->TopOfStack = -1;
	S->Array = (int*)malloc(sizeof(Stack)); 
	return S;
}

int IsFull(Stack* S)
{
	if (S->Capacity-1 == S->TopOfStack)
	{
		return 1;
	}
	else return 0; 
}

void Push(Stack* S,int key)
{
	if (IsFull(S))
	{
		printf("ERROR: Stack Is Full!\n");
		exit(0);
	} 
	else
	{
		S->Array[++S->TopOfStack] = key;
	}			
}

int IsEmpty(Stack* S)
{
	if(S->TopOfStack == -1)	return 1;
	else return 0;
}
void Pop(Stack* S)
{
	if (IsEmpty(S))
	{
		printf("ERROR: Stack Is Empty!\n");
		exit(0);
	} 
	else
	{
		--(S->TopOfStack);
	}
}

void PrintStack(Stack* S)
{
	if (IsEmpty(S))
	{
		printf("ERROR: Stack Is Empty!\n");
		exit(0);
	}else{
		int index = S->TopOfStack;
		while(index>=0){
			printf("%d ",S->Array[index--]);
		}
		printf("\n");
	} 
}

void main(void)
{
	printf("Enter key value:\n");
	int key;
	Stack* S = CreateStack();
	while(scanf("%d",&key)){
		Push(S,key);
	}
	PrintStack(S);
	Pop(S);
	printf("After one Pop:\n");
	PrintStack(S);
	Push(S,key);	
	printf("After Push %d:\n",key);
	PrintStack(S);	
}


循环链表:
#include <stdio.h>
#include <iostream.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <algorithm>
#include <vector>
using   namespace   std;

typedef struct student
{
	int data;
	struct student* next;
}node;

typedef struct Ring
{
	node* L;
	int NodeNum;
}ring;

ring* CreateRing()//返回入口地址
{
	printf("Enter key value:\n");
	int x;
	node* L = NULL;
	int NodeNum = 0;
	node* CurrPos = L;
	while(scanf("%d",&x)){
		if (CurrPos==NULL)
		{
			CurrPos = (node*)malloc(sizeof(node));		 	
			CurrPos->data = x;
			CurrPos->next = CurrPos;
			L = CurrPos;
			NodeNum++;
		} 
		else
		{
			node* temp = CurrPos->next;		
			CurrPos->next = (node*)malloc(sizeof(node));		
			CurrPos->next->data = x;
			CurrPos->next->next = temp;
			CurrPos = CurrPos->next;
			NodeNum++;
		}
	}
	ring* QRing = (ring*)malloc(sizeof(ring));
	QRing->L = L;
	QRing->NodeNum = NodeNum;
	printf("Ring established!\n");
	return QRing;
}
void PrintRing(ring* QRing)
{
	node* pos = QRing->L;
	do{
		printf("%d	",pos->data);	
		pos = pos->next;
	}while(pos!= QRing->L);
	printf("\n");
}

void Delete_dist(ring* QRing,int dist)
{
	node* L = QRing->L;
	int n = QRing->NodeNum;
	node* curr = L;
	while(n){
		node* victim = curr;
		for(int m = 0; m<dist-1; m++){		
			victim = victim->next;
		}
		node* previc = victim;
		victim = victim->next;
		previc->next = victim->next;
		printf("%d\t",victim->data);
		free(victim);
		curr = curr->next;
		n--;
	}
	printf("\n");	
}


void main(void)
{
	ring* p = CreateRing();
	PrintRing(p);
	Delete_dist(p,3);
	PrintRing(p);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值