顺序表和单链表的基本操作

大一,快考试了,复习一下。


单链表

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<malloc.h>
#include<windows.h>
typedef struct Node
{
	struct Node *next;
	int Date;
}SLNode;
SLNode *SList_Creat(int n);
int SList_Length(SLNode *head);
int SList_Insert(SLNode *head, int i, int n);
void SList_Print(SLNode *head);
int SList_Delete(SLNode *head, int i);
int main()
{
	SLNode *L = SList_Creat(5);
	SList_Insert(L, 3, 8);
	SList_Print(L);
	SList_Delete(L, 3);
	SList_Print(L);
	system("pause");
	return 0;
}

SLNode *SList_Creat(int n)  //创建 n个结点的链表
{
	SLNode *p,*s,*head;
	int k;
	head=(SLNode *)malloc(sizeof(SLNode));
	head->next = NULL;
	head->Date = 0;
	s = head;
	for (int i = 0; i < n; i++)
	{
		p = (SLNode *)malloc(sizeof(SLNode));
		scanf("%d", &k);
		p->Date = k;
		s->next = p;
		s = p;
		p->next = NULL;
	}
	return head;
}

int SList_Length(SLNode *head)  //获取链表长度
{
	int count = 0;
	SLNode *s=head;
	while (s->next != NULL)
	{
		count++;
		s = s->next;
	}
	return count;
}

int SList_Insert(SLNode *head,int i,int n)  // 在i位置前插入结点 其数据为n
{
	SLNode *s=NULL, *p=NULL;
	int j=0;
	s = head;
	while (s->next != NULL&&j < i)
	{
		s = s->next;
		j++;
	}
	if (j != i)
	{
		printf("Error!\n");
		return 0;
	}
	if ((p = (SLNode *)malloc(sizeof(SLNode))) == NULL)
		return 0;
	else
	{
		p->Date = n;
		p->next = s->next;
		s->next = p;
		return 1;
	}
}

int SList_Delete(SLNode *head,int i)  //删除位置i的结点
{
	SLNode *p = head,*s=NULL;
	int j = 0;
	while (p->next != NULL&&j < i)
	{
		p = p->next;
		j++;	
	}
	if (j != i)
	{
		printf("Error!\n");
		return 0;
	}
	else
	{
		s = p->next;
		p->next = p->next->next;
		free(s);
		return 1;
	}
}

void SList_Print(SLNode *head)   //输出整个链表
{
	SLNode *p = head;
	for (int i = 0; i<SList_Length(head); i++)
	{	
		p = p->next;
		printf("%-2d",p->Date);
	}
	printf("\n");
}


顺序表


#define _CRT_SECURE_NO_WARNINGS    // by leo
#include<stdio.h>    
#include<windows.h>
#define MaxSize 100
typedef struct List 
{
	int Date[MaxSize];
	int Len;
}SeqList;
void List_Initiate(SeqList *L);
int List_Insert(SeqList *L, int i, int n);
int List_Search(SeqList *L, int n);
int List_Delete(SeqList *L, int i);
void List_Pirnt(SeqList *L);
int main1()
{
	SeqList L;
	int k,n,i,j;
	printf("请输入存入数据的个数:");
	scanf("%d", &k);
	List_Initiate(&L);
	printf("请输入存入的数据:\n");
	for (int i = 0; i < k; i++)
	{
		scanf("%d", &n);
		List_Insert(&L, i, n);
	}
	printf("请输入插入的位置和数据\n");
	scanf("%d %d", &i, &j);
	List_Insert(&L, i, j);
	List_Pirnt(&L);
	system("pause");
	return 0;	
}

void List_Initiate(SeqList *L)
{
	L->Len = 0;
	printf("初始化成功!\n");
}

int List_Insert(SeqList *L, int i, int n)  // insert a date before i
{
	int k;
	if (L->Len >= MaxSize)
	{
		printf("The List is overflow");
		return 0;
	}
	if (i<0 || i>MaxSize)
	{
		printf("Error\n");
		return 0;
	}
	else
	{
		for (k = L->Len; k > i; k--)
			L->Date[k] = L->Date[k - 1];
		L->Date[i] = n;
		L->Len++;
		return 1;
	}

}

int List_Search(SeqList *L, int n) // find n in this List
{
	for (int i = 0; i < L->Len; i++)
		if (L->Date[i] == n)
			return i;
		return 0;
}

int List_Delete(SeqList *L, int i)  //delete i in this List
{
	if (i<0 || i>MaxSize)
	{
		printf("Error\n");
		return 0;
	}
	for (int k = i; k < L->Len; k++)
		L->Date[k] = L->Date[k + 1];
	L->Len--;
	return 1;
}

void List_Pirnt(SeqList *L)
{
	for (int i = 0; i < L->Len; i++)
		printf("%-2d", L->Date[i]);
	printf("\n");
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值