[英]An array of linked list in C
我想創建一個鏈表數組,其中每個數組元素都是鏈表的一個節點。 基本上,我想通過數組元素索引一個鏈表。 假設,我們有一個數組 a[20],其中每個元素代表鏈表的一個節點。 下面給出的圖片:-
我創建了一個鏈接列表,它將在其中輸入並打印列表。 但是,我需要幫助用數組索引它。 這是我的鏈表。
#include <stdio.h>
#include <stdlib.h>
int a[20];
typedef struct Node {
int data;
struct Node *next;
} Node;
void insert_beg_of_list(Node *current, int data);
void print_list(Node *current);
void insert_beg_of_list(Node *current, int data) {
//keep track of first node
Node *head = current;
while(current->next != head) {
current = current->next;
}
current->next = (Node*)malloc(sizeof(Node));
current = current->next;
current->data = data;
current->next = head;
}
void print_list(Node *current) {
Node *head = current;
current = current->next;
while(current != head){
printf(" %d ", current->data);
current = current->next;
}
}
int main() {
Node *head = (Node *)malloc(sizeof(Node));
head->next = head;
int data = 0 ;
int usr_input = 0;
int i;
int m;
scanf("%d", &usr_input);
for (i=0; i<usr_input; i++) {
scanf("%d", &data);
insert_beg_of_list(head, data);
}
printf("The list is ");
print_list(head);
printf("\n\n");
return 0;
}
舉個例子 :-
它現在做什么:-
Input :- Number of elements = 5
Input :- Elements = 1 2 3 4 5
Output :- 1 2 3 4 5
我期待什么:-
Input :-
Number of elements for a[0] = 4
Input for a[0] = 1 2 3 4
Number of elements for a[1] = 4
Input for a[1] = 2 3 5 4
Expected Output :-
a[0] = 1 2 3 4
a[1] = 2 3 5 4
這是帶有 Array 元素的修改后的代碼。 我將數據存儲在current [0]
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node *next;
} Node;
Node *current[20];
void insert_beg_of_list(Node *current[0], int data);
void print_list(Node *current[0]);
void insert_beg_of_list(Node *current[0], int data) {
//keep track of first node
Node *head = current[0];
while(current[0]->next != head) {
current[0] = current[0]->next;
}
current[0]->next = (Node*)malloc(sizeof(Node));
current[0] = current[0]->next;
current[0]->data = data;
current[0]->next = head;
}
void print_list(Node *current[0]) {
Node *head = current[0];
current[0] = current[0]->next;
while(current[0] != head){
printf(" %d ", current[0]->data);
current[0] = current[0]->next;
}
}
int main() {
Node *head = (Node *)malloc(sizeof(Node));
head->next = head;
int data = 0 ;
int usr_input = 0;
int i;
int m;
int j;
scanf("%d", &usr_input);
for (i=0; i<usr_input; i++) {
scanf("%d", &data);
insert_beg_of_list(head, data);
}
printf("The list is ");
print_list(head);
printf("\n\n");
return 0;
}
它顯示分段錯誤。
如果要在數組的每個索引中存儲一個新的鏈表,則不能使用相同的head
節點( main
函數中第一行的head
節點)。 它應該為數組中的每個索引分配一個新的頭節點,然后將其余數據推送到它之后。
編輯順便說一句:您的代碼與您附加的圖像不一致:在我的回答中,我假設您想要圖像中的數據結構。 另一方面,在你的代碼中,根據你只調用一次的函數print_list
(而不是數組中的每個索引),並且根據你只有一個頭節點的事實,也許你想要別的東西(只有一個通用列表?),然后就不是很清楚你的意思。
我認為你想要一個鏈表數組,顯然每個鏈接都應該有單獨的頭節點。 如果我認為正確,那么您可以看到下面給出的代碼。 在這里發帖之前會進行檢查。
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *next;
}*head[5];
void create(int count);
void print(int count);
int main()
{
int i,n;
n=5; /*n is the total number of nodes */
for(i=0;i<n;i++)
{
head[i]=NULL;
create(i);
print(i);
printf("\n\n");
}
return 0;
}
void create(int count)
{
int n2=5; /*n2 is the number of nodes in a single linked list*/
int j;
struct node *temp;
for(j=0;j<5;j++)
{
if(head[count]==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->data=j+5+count;
temp->next=NULL;
head[count]=temp;
}
else
{
temp->next=(struct node*)malloc(sizeof(struct node));
temp=temp->next;
temp->data=j+5+count;
temp->next=NULL;
}
}
}
void print(int count)
{
struct node *temp;
temp=head[count];
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
嘿,也許這個用 C 編寫的簡單代碼可能對你有所幫助:
#include "stdio.h"
#include "stdlib.h"
struct node {
int data;
struct node *next;
}*head;
struct node *temp;
int main(){
int size,si;
printf("\nEnter the size of array : ");
scanf("%d",&size);
struct node *A[size];
for(int i=0;i<size;i++){
printf("\nEnter the size of linked list in array[%d] : ",i);
scanf("%d",&si);
head = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the data:");
scanf("%d",&head -> data);
head -> next = NULL;
temp = head;
for(int j=1;j<si;j++){
struct node *new = (struct node *)malloc(sizeof(struct node));
printf("\nEnter the data:");
scanf("%d",&new -> data);
new -> next = NULL;
temp -> next = new;
temp = new;
}
A[i] = head;
}
for(int i=0;i<size;i++){
printf("\nArr[%d] => ",i);
struct node *p;
p = A[i];
while(p!=NULL){
printf("%d\t",p->data);
p = p -> next;
}
printf("\n");
}
return 0;
}
: 輸出 :
Enter the size of array : 3
Enter the size of linked list in array[0] : 3
Enter the data:1
Enter the data:2
Enter the data:3
Enter the size of linked list in array[1] : 5
Enter the data:1
Enter the data:2
Enter the data:3
Enter the data:4
Enter the data:5
Enter the size of linked list in array[2] : 4
Enter the data:9
Enter the data:8
Enter the data:7
Enter the data:6
Arr[0] => 1 2 3
Arr[1] => 1 2 3 4 5
Arr[2] => 9 8 7 6
Program ended with exit code: 0
說明:在這個程序中,我創建了一個用戶定義大小的動態數組,然后創建了一個用戶定義大小的鏈表,並在每個數組塊中分配了每個鏈表的頭部。 我已經展示了直接實現,所以一切都在 main 函數中,但這也可以通過創建單獨的函數來完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.