The simple creation of the list--c programming __c

Source: Internet
Author: User
Tags function prototype strcmp

Topic: Create a fixed-length one-way list

Program Analysis: Linked list is the dynamic allocation of storage space chain-type storage structure,

It includes a "head pointer" variable, where the No. 0 node is called the entire list of the head node, the head node to hold an address, the address points to an element, the head node generally do not hold the specific data, but the first node to store the address.

Each element in a list is called a "node", and each node consists of two parts: the data field that holds the data element and the pointer field that stores the immediate successor storage location. The pointer field stores the next node in the list, which is a pointer. Multiple nodes are linked into a linked list.

The pointer field of the last node is set to null (NULL) as the ending flag of the linked list, indicating that it has no successor nodes.

Use a struct variable as a node in a linked list, because a struct variable member can be a numeric type, a character type, an array type, or a pointer type, so that you can use pointer type members to hold the address of the next node so that other types of members hold data information.

When creating a list to dynamically allocate space for the linked list, the C language library function provides several functions to realize the dynamic storage unit.

The malloc () function realizes the dynamic opening storage unit:

The malloc function prototype is: void *malloc (unsigned int size);

The effect is to allocate a contiguous space of size in the dynamic storage area of memory, and the function return value is a pointer to the starting address of the assignment field (type void). Returns a space pointer (NULL) if the allocation space fails (for example, insufficient memory space)

The code is as follows:

#include <stdio.h>

#include <malloc.h>

struct Lnode

{

int data;

struct Lnode *next;

};

/* Above only defines a struct type, does not actually allocate the memory space

Allocate memory space only when variables are defined.

struct Lnode *creat (int n)

{

int i;

struct Lnode *head,*p1,*p2;

/*head is used to mark linked lists, p1 are always used to point to newly allocated memory space.

P2 always point to the tail node, and through the P2 to link into the newly allocated node.

int A;

Head=null;

for (i=1;i<=n;i++)

{

p1= (struct Lnode *) malloc (sizeof (struct lnode));

/* Dynamically allocate memory space and convert data to (struct Lnode) type * *

printf ("Please enter number%d in the list:", I);

scanf ("%d", &a);

p1->data=a;

if (head==null)/* Specifies the head pointer of the linked list.

{

HEAD=P1;

P2=P1;

}

Else

{

p2->next=p1;

P2=P1;

}

The successor pointer to the p2->next=null;/* tail node is null (empty) * *.

}

Return head;/* the head pointer of the linked list.

}

void Main ()

{

int n;

struct Lnode *q;

printf ("Please enter the length of the list:/n");

scanf ("%d", &n);

Q=creat (n);/* The head pointer of the linked list to mark the entire list.

printf ("Data in/N-linked list:/n");

while (q)/* until node q is null end loop * *

{

printf ("%d", q->data);/* Output node value * *

q=q->next;/* point to the next node * *

}

}

Topic: Create a two-way list

Program Analysis: Two-way linked list node has two pointer fields, one point to its direct successor, the other point to its direct precursor. The precursor of the first node is null (the head node is the No. 0 node), and the successor of the last node is null.

The code is as follows:

#include <stdio.h>

#include <string.h>

#include <malloc.h>

typedef struct NODE

{

Char name[20];

struct node *prior,*next;

}stud;

/* Double linked list structure definition * *

Stud *creat (int n)/* Create a double linked list function * *

{

Stud *p,*h,*s;

int i;

H= (Stud *) malloc (sizeof (stud));/* Dynamically allocating memory to the head node * *

H->name[0]= '/0 '/* for the content of the head node empty/

The precursor and successor of the h->prior=null;/* head node are null*/.

h->next=null;

p=h;/* assigns a header node to the p,p always points to the last node in the list.

for (i=0;i<n;i++)

{

s= (Stud *) malloc (sizeof (stud));/* Dynamic Allocated memory * *

p->next=s;/* the subsequent point of the P node to s*/

printf ("Please enter the name of%d classmate:", i+1);

scanf ("%s", s->name);

s->prior=p;/* let the precursor of S node point to P node.

s->next=null;/* let the subsequent point of the S node null,s always point to the newly assigned node.

P=s;/*p always point to the last node of the list.

}

The successor point of the last node of the p->next=null;/* double linked list null*/

return (h);

}

Stud *search (stud *h,char *x)/* Lookup * *

{

Stud *p;/* for locating nodes in a double linked list * *

Char *y;

p=h->next;

while (p)

{

y=p->name;

if (strcmp (y,x) ==0)/*strcmp function compares the contents of two strings are equal.

return (P);/* If found, returns the current node * *

Else

p=p->next;/* is not equal to continue looking down * *

}

printf ("Information not Found/n");

}

void del (stud *p)/* Delete * *

{

if (p->next!=null)/* Determines whether the node to be deleted is the last node, and the last node is followed by null*/

The successor of p->next->prior=p->prior;/*p node gives the precursor of P node.

Successor of the P->PRIOR->NEXT=P->NEXT;/*P node precursor given p node

Free (p); /* Free the memory space of the P node.

}

void Dip (stud *p)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.