しつもん

このエントリーをはてなブックマークに追加
1レモン
cで、Linked Listをつかって入力した数字を小さい順に並びかえるプログラムを誰か教えて下さい。。
2非決定性名無しさん:03/08/25 20:13
std::list<int> lst;
lst.push_back(5);
lst.push_back(8);
lst.push_back(3);
lst.push_back(7);
lst.push_back(2);

lst.sort();

for(std::list<int>::iterator it=lst.begin(); it!=lst.end(); ++it)
 std::cout << *it << std::endl;
3あぼーん:あぼーん
あぼーん
4非決定性名無しさん:03/09/08 22:06
>>2
それはCでなくてC++
5非決定性名無しさん:03/09/08 22:16
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define FORWARD 1
#define BACK 2

typedef struct tfield{
struct tfield *back; /* 前のリストに戻るポインタ */
int num; /* シーケンス番号 */
struct tfield *next; /* 次のリストに進むポインタ */
} TF;

char *DelNewLine(char *string); /* 改行文字を捨てます */
void PrintList(void *, int); /* 作成したリストを表示します */
void FreeAllocatedMemory(void *); /* 領域を開放します */
void AscSort(TF *); /* 昇順にソートします */

void main(void){

TF *head, *sorthead, *tail, *p, *q;
char buf[20];
int i;
int flg = 0;
6非決定性名無しさん:03/09/08 22:16

/* 最後尾から先頭に戻っていくリスト */
tail = NULL;
while(printf("数字を入力してください >"), fgets(buf, 20, stdin) != NULL){
p = (TF *)malloc(sizeof(TF));
p->num = atoi(buf);
p->back = tail;
tail = p;
}

/* 先頭から最後尾へ進むリスト */
p = tail;
head = NULL;
while(p != NULL){
p->next = head;
head = p;
p = p->back;
}

/* 昇順ソート */
sorthead = head;
AscSort(sorthead);
7非決定性名無しさん:03/09/08 22:18

/* 出力 */
while(!flg){
printf("\n順方向リスト\n");
PrintList(head, FORWARD);

printf("\n逆方向リスト\n");
PrintList(tail, BACK);
printf("終了しますか? (y/n)>");
if(getchar() == 'y' )
flg = 1;
else
fflush(stdin);
}

/* 領域の開放 */
FreeAllocatedMemory(head);

}
/* 改行文字を捨てる */
char *DelNewLine(char *string){

string[strlen(string) - 1] = '\0';

return string;

}
8非決定性名無しさん:03/09/08 22:18
/* 昇順にソートする */
void AscSort(TF *p){

TF *q;
int work;

for(; p->next != NULL; p = p->next){
printf("p->num :%d\n", p->num);
for(q = p->next; q != NULL; q = q->next){
printf("q->num :%d ", q->num);
if(p->num > q->num){
work = p->num;
p->num = q->num;
q->num = work;
}
}
putchar('\n');
}

}
9非決定性名無しさん:03/09/08 22:19

/* 表示する */
void PrintList(void *pointer, int direction){

TF *p;

p = (TF *)pointer;

while(p != NULL){
printf("番号 : %d\n", p->num);
if(direction == FORWARD)
p = p->next;
else if(direction == BACK)
p = p->back;
}

}
10非決定性名無しさん:03/09/08 22:20

/* 領域の開放 */
void FreeAllocatedMemory(void *headpointer){

TF *p;
TF *q;

p = (TF *)headpointer;

while(p != NULL){
q = p->next;
free(p);
p = q;
}

}
11非決定性名無しさん:03/10/24 20:58
int main(int argc, char *argv[])
{
char *buf;

strcpy(buf, "もうね、アホかと、バカかと。お前は本当は(ry");
puts(buf);
return 0;
}

12非決定性名無しさん
↑線形リストと違うやんけ!!