我真的不知道如何讓雙向鏈表工作。我以前使用平行數組來排序用戶名和年齡與一個bubblesort。 目標是從用戶讀取名稱和年齡並將值存儲到節點中。打印向前和向後排列的值,我認爲使用strcmp對於這個程序會更容易,因爲對我來說bubblesort非常困難。
我需要基於目前爲止的代碼提供建議。什麼是最好的方式來使這個程序的工作。如何使用雙向鏈表向前,向後排序並輸出到文件
#include <stdio.h>
#include <stdlib.h>
//create structure of person that can hold a name and an age that can be used in a doubly linked list
int main(int argc, char *argv[])
{
struct person //structure of person
{
char name [41];
int age;
struct person *next;//doubly linked list
struct person *prev;
};
typedef struct person people;//using a new type of our structure
people first;//declade first of type people
first.age = 12;//puts 12 in the age portion of the variable first of the structure person which can hold someones age
strcpy (first.name, "sue");
people *p; //creates a pointer to a storage location
p = &first; //that storage location now has the contents of the storage location of first.
(*p).age = 13; //this is the same as p->age=13; or arrow. This dereferences the pointer. arrow for pointer, (*x)
//dereferencing through structure
//make two pointers to manage a doubly linked list
//make two pointers of type people named head and tail
people *head,*tail;
*head = NULL;
*tail = NULL;
//start with printing rout9ine
printit(head);//shows nothing
//linked list
void printit(people *h);
{
people *t;
t=h;
while (t != NULL)
{
//print the information here, he didnt give me code so i need to remember this on my own
t = t->next;//continues the list goes through pointers to the first node then destroyes t by making it
//the next. You can also use the h.
}
}
//sending address, pass by reference
buildit(&head,&tail);
//head is a pointer of type people, want the pointer not the value
//so you need a pointer of a pointer, it acccepts the address of head and tail which
//is the address of the addres of the start and end of the linked list
void buildit (people **h, people **t)
{
people *current;
current = malloc (sizeof(people)); //creates space for a people structure, current points to it.
//assume doubly linked list
//t refers to the address of tail, if you want the contents of the address of tail you need *t.
(*t) -> next = current;
current -> prev = *t;
*t = current;
}
//structure in iostream, called FILE
FILE *out;
out = fopen(/*name of the file*/ stringname, access);
//fopen opens a file, you can open stdin, com 1, com 2, opens keyboard or ports or files.
//access has two values, r and w. r = read w = write. input reads from output writes to.
out = fopen("output.txt","w"); //opens output.txt
char filename [81];
gets (filename); //allows a user to input the file they want to open. array of chars, reads entire string spaces and all
out = fopen(filename,"w");
if (out != NULL) //checks if file is open or note, if null no file opened up, it checks
{
fprintf (out, "formatstring"; parameterlist); //file print f
fscanf //adds file handle to parameters, thats it.
fgets //same, file gets
}
//changing a file needs pass by reference, not value.
ptintit(person*, FILE**);
void printit(person*h, FILE**o)
{
fprintf (*o,",,,,,",...);
}
system("PAUSE");
return 0;
}
您是否需要使用雙向鏈表?如果不是這樣,使用數組將是更好的選擇(如peterh建議的),因爲元素將被放置在連續的存儲器位置,並且每個元素訪問將更有效率,因爲可以避免大量不必要的指針解引用。 – jithinpt
另外,您能否更清楚地陳述您面臨的問題?你得到一個不正確的輸出?或者你想要更好的表現? – jithinpt
不,它必須是一個雙向鏈表。我試圖用指針將我的頭圍繞在地址的地址上。 – IdislikeCalot