gpt4 book ai didi

c++ - 如何在C++中创建链表列表?

转载 作者:行者123 更新时间:2023-11-30 20:40:28 27 4
gpt4 key购买 nike

我正在尝试用 C++ 创建一个链表数组。

我想创建一个可以添加/删除新学生姓名和 ID 的链接列表。这是节点的结构:

struct node
{
char name[50];
char id[50];
struct node *next;
}*start=NULL,*prev;
typedef struct node node;

如何做到这一点?

代码:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>

struct node
{
char name[50];
char type[50];
struct node *next;
}*start=NULL,*prev;
typedef struct node node;

void menu();
void insertion();
void finder();
void table();
void deletion();

int main()
{
//node *table[57];

while(1)
{
menu();
}
return 0;
}

void menu()
{
int choice;

printf("\nSymbol Table Menu\n1. Insert.\n2. Find.\n3. Show Table.\n4. Delete.\n");

scanf("%d",&choice);
//system("cls");
if(choice==1)insertion();
else if(choice==2)finder();
else if(choice==3)table();
else if(choice==4)deletion();

}
int Hashing(node *temp)
{
int h,len,i,asc,sum=0;
printf("\nHashing\n");
len=strlen(temp->name);
for(i=0; i<len; i++)
{
asc=temp->name[i];
sum=sum+asc;
}
h=sum%57;
return h;
}
void insertion()
{
int index;
node *temp;

if (start == NULL)
{
start =(node*)malloc(sizeof(node));
temp=start;
printf("Insert Name: ");
scanf("%s",start->name);
index=Hashing(temp);
printf("Index: %d\n",index);
printf("\nInsert Type: ");
scanf("%s",start->type);
start->next=NULL;
prev=start;
}
else
{
temp=(node*)malloc(sizeof(node));
printf("Insert Name: ");
scanf("%s",temp->name);
index=Hashing(temp);
printf("Index: %d\n",index);
printf("\nInsert Type: ");
scanf("%s",temp->type);
temp->next= NULL;
prev->next=temp;
prev=temp;
}
}
void finder()
{
node *temp;
char key1[50];
char key2[50];
printf("\nInsert Search Key: ");
temp=start;
scanf("%s%s",key1,key2);

while(temp!=NULL)
{
if((strcmp(temp->name,key1)==0) && (strcmp(temp->type,key2)==0))
{
printf("FOUND\n");
}
temp=temp->next;
}
}
void table()
{
printf("\nFull Table\n\n");

node *temp;
printf("Linked list = ");
temp=start;

if(temp==NULL) printf("No list\n");

while(temp!=NULL)
{
printf("%s,%s -> ",temp->name,temp->type);
temp=temp->next;
if(temp==NULL)printf("NULL\n");
}
}
void deletion()
{
node *temp,*temp1;
char del1[50];
char del2[50];
temp=start;
printf("Delete Name: ");
scanf("%s",del1);
printf("\nDelete Type: ");
scanf("%s",del2);

if((strcmp(temp->name,del1)==0) && (strcmp(temp->type,del2)==0))
{
start=start->next;
free(temp);
}
else
{
while ((temp->next->next!=NULL)&&(strcmp(temp->next->name,del1)!=0))
{
temp=temp->next;
}
if ((temp->next==NULL) &&(strcmp(temp->name,del1)!=0))
printf("\nNot Found\n");
else
{
temp1=temp->next;
temp->next=temp1->next;
free(temp1);
}
}
printf("DELETED\n");
}

最佳答案

创建一个节点数组,并分别初始化每个节点。

node *array = malloc(NUMBER_OF_NODES*sizeof(node *));
int i;
for(i=0; i< NUMBER_OF_NODES; i++)
{
/* this part is optional - you might want to leave some of the cells uninitialized */
array[i] = malloc(sizeof(node)); /* notice that we need to allocate space for a node - not (node *)!
}

关于c++ - 如何在C++中创建链表列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22390592/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com