數(shù)據(jù)結(jié)構(gòu)題目:鏈表
題目:求相鄰k個(gè)節(jié)點(diǎn)數(shù)值之和最大的第一節(jié)點(diǎn)
輸入數(shù)據(jù)(設(shè)為整型)建立單鏈表,并求相鄰k個(gè)節(jié)點(diǎn)data值之和為最大的第一節(jié)點(diǎn)虎眨。例如輸入k = 2彰触,數(shù)據(jù)為2 6 4 7 3 #(#為結(jié)束符)实牡,建立下面鏈表,運(yùn)行結(jié)果輸出(序號(hào)3惨撇,data值4):
c:
#include "stdio.h"
#include "malloc.h"
int sum;
int maxsum=0;
int count;
int k,k1;
int a1,a2;
typedef struct node
{
int data,time;
struct node *next;
}list,*List;
List Creatlist()
{
List H,p,r;
int a;
H=(List)malloc(sizeof(list));
r=H;
printf("請(qǐng)輸入整形數(shù)據(jù),以空格隔開府寒,#結(jié)束魁衙。\n");
while((scanf("%d",&a))&&(a!='#'))
{
p=(List)malloc(sizeof(list));
p->data=a;
p->time=count;
count++;
r->next=p;
r=p;
}
r->next=NULL;
return H;
}
List Adjmax(List H)
{
List p,p1,q;
p=H->next;
if((p==NULL)||(count-1<k))
{
printf("輸入錯(cuò)誤!\n");
return 0;
}
while (p)
{
k1 = k;
sum = 0;
p1 = p;
while((k1--)&&(p1))
{
sum += p1->data;
q = p1->next;
p1 = q;
}
if(sum>maxsum)
{
a1=p->time;
a2=p->data;
maxsum=sum;
}
p=p->next;
}
printf("order:%d\ndata:%d\n",a1,a2);
return 0;
}
int main()
{
char ch;
printf("求相鄰k個(gè)節(jié)點(diǎn)數(shù)值之和最大的第一節(jié)點(diǎn)\nReady?Y/N ");
while(scanf("%c",&ch))
{
if(ch=='Y' || ch=='y')
{
count = 1;
printf("請(qǐng)輸入k值: ");
scanf("%d",&k);
if(k==1 || k==2 || k==3 || k==4 || k==5 || k==6 || k==7 || k==8 || k==9)
Adjmax(Creatlist());
else
printf("輸入不合法\n");
printf("Continue?Y/N ");
fflush(stdin);
}
else if(ch=='N' || ch=='n') break;
else
printf("輸入不合法\n");
}
return 0;
}
java:
import java.util.Scanner;
/**
*@author movis
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int k;
String data;
String n;
do {
System.out.println("請(qǐng)輸入k值:");
data = in.next();
while(!(data.equals("1")||data.equals("2")||data.equals("3")||data.equals("4")
||data.equals("5")||data.equals("6")||data.equals("7")||data.equals("8")
||data.equals("9"))) {
System.out.println("輸入k值不合法株搔,請(qǐng)重新輸入k值");
data = in.next();
}
k = Integer.parseInt(data);
System.out.println("請(qǐng)輸入數(shù)據(jù):");
LinkedList list = new LinkedList();
data = in.next();
while(!data.equals("#")) {
list.addNode(Integer.parseInt(data));
data = in.next();
}
Adjmax(list, k);
System.out.println("是否繼續(xù)剖淀?請(qǐng)輸入Y或N");
n = in.next();
while(!n.equals("Y") && !n.equals("N")) {
System.out.println("輸入值錯(cuò)誤,請(qǐng)重新輸入Y或N");
n = in.next();
}
}while(n.equals("Y"));
in.close();
}
public static void Adjmax(LinkedList list, int k) {
int sum;
int index;
int data;
int maxsum = 0;
int finalindex = 0;
int finaldata = 0;
if(k > list.length()) {
System.out.println("輸入k值大于數(shù)據(jù)個(gè)數(shù)纤房,錯(cuò)誤纵隔!");
return;
}
for(int i=0; i<=list.length()-k; i++) {
sum = 0;
index = 1;
LinkedList.Node temp = list.head;
data = temp.data;
for(int n=i; n>0; n--) {
temp = temp.next;
data = temp.data;
index++;
}
for(int j=k; j>0; j--) {
sum += temp.data;
temp = temp.next;
}
if(sum > maxsum) {
maxsum = sum;
finalindex = index;
finaldata = data;
}
}
System.out.println("序號(hào)為:"+finalindex+" data值為:"+finaldata);
}
}
/**
*@author movis
*/
public class LinkedList {
Node head = null;
public class Node {
int data;
Node next = null;
Node(int data) {
this.data = data;
}
}
public void addNode(int d) {
Node newNode = new Node(d);
if(head == null) {
head = newNode;
return;
}
Node temp = head;
while(temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
public void printList() {
Node tmp = head;
while (tmp != null) {
System.out.println(tmp.data);
tmp = tmp.next;
}
}
public int length() {
int length = 0;
Node tmp = head;
while (tmp != null) {
length++;
tmp = tmp.next;
}
return length;
}
}