开始复习数据结构
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <malloc.h>
struct Node
{
int date;
struct Node *next;
};
void Init(Node *l) //初始化链表
{
l->next = NULL;
l->date = 0;
}
void Creat(Node *l, int n) //用户输入初始化链表
{
int i, count = 0;
Node *p = NULL, *q = NULL;
p = l;
for (i = 0; i < n; i++)
{
q = (Node *)malloc(sizeof(Node));
scanf("%d", &q->date);
p->next = q;
p = q;
count++;
}
p->next = NULL;
l->date = count;
}
void Creat2(Node *l, int A[], int n) //用数组的数据初始化链表
{
int i;
Node *p = NULL, *q = NULL;
p = l;
for (i = 0; i < n; i++)
{
q = (Node *)malloc(sizeof(Node));
q->date = A[i];
p->next = q;
p = q;
}
p->next = NULL;
l->date = n;
}
void Insert(Node *l, int d, int k) //在位置k前插入数据d
{
int i;
Node *s = NULL, *p = NULL, *q = NULL;
p = (Node *)malloc(sizeof(Node));
p->date = d;
s = l->next;;
for (i = 0; i < k; i++)
{
q = s;
s = q->next;
}
q->next = p;
p->next = s;
l->date++;
}
void Delete(Node *l, int k) //删除k位置的数据
{
int i;
Node *s = NULL, *p = NULL;
s = l->next;
for (i = 0; i < k; i++)
{
p = s;
s = p->next;
}
p->next = s->next;
free(s);
l->date--;
}
void Print(Node *l) //打印链表
{
int i;
Node *s = NULL, *p = NULL;
s = l->next;
printf("Output:");
for (i = 0; i < l->date; i++)
{
printf("%d ", s->date);
p = s;
s = p->next;
}
printf("\n");
}
void Find(Node *l, int d) //在链表l中寻到数据d并输出该数据的位置
{
int i, count = 0;
Node *s = NULL, *p = NULL;
s = l->next;
for (i = 0; i < l->date; i++)
{
if (s->date == d)
{
count++;
printf("已找到,位置%d\n", i + 1);
break;
}
p = s;
s = p->next;
}
if (!count)
printf("未找到\n");
}
int main()
{
Node l, m;
int n;
int A[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int len = sizeof(A) / sizeof(int);
printf("请输入存入数据的个数:");
scanf("%d", &n);
Init(&l);
Creat(&l, n);
Print(&l);
Delete(&l, 2);
Print(&l);
Insert(&l, 3, 2);
Print(&l);
Find(&l, 4);
printf("--------------\n");
Creat2(&m, A, len);
Print(&m);
return 0;
}
直接翻译成java语言
package test;
import java.util.Scanner;
public class Node{
int date;
Node next;
Scanner sc = new Scanner(System.in);
Node(){
this.date = 0;
this.next = null;
}
public void Creat(int n){
int i, count = 0;
Node p = null, q = null;
p = this;
for (i = 0; i < n; i++){
q = new Node();
q.date = sc.nextInt();
p.next = q;
p = q;
count++;
}
p.next = null;
this.date = count;
}
public void Insert(int d, int k){
int i;
Node s = null, p = null, q = null;
p = new Node();
p.date = d;
s = this.next;
for (i = 0; i < k; i++){
q = s;
s = q.next;
}
q.next = p;
p.next = s;
this.date++;
}
public void Delete(int k){
int i;
Node s = null, p = null;
s = this.next;
for (i = 0; i < k; i++){
p = s;
s = p.next;
}
p.next = s.next;
this.date--;
}
public void Print(){
int i;
Node s = null, p = null;
s = this.next;
System.out.print("Output:");
for (i = 0; i < this.date; i++){
System.out.printf("%d ", s.date);
p = s;
s = p.next;
}
System.out.println();
}
public void Find(int d){
int i, count = 0;
Node s = null, p = null;
s = this.next;
for (i = 0; i < this.date; i++){
if (s.date == d){
count++;
System.out.printf("已找到,位置%d\n", i + 1);
break;
}
p = s;
s = p.next;
}
if (count == 0)
System.out.println("未找到");
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Node l = new Node();
int n;
System.out.print("请输入存入数据的个数:");
n = sc.nextInt();
l.Creat(n);
l.Print();
l.Delete(2);
l.Print();
l.Insert(3, 2);
l.Print();
l.Find(6);
}
}