Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef int ElementType;
- typedef struct Node* NodeType;
- struct Node {
- ElementType element;
- NodeType next;
- };
- typedef NodeType Position;
- typedef Position List;
- void MakeNullList(List *Header) {
- (*Header)=(NodeType)malloc(sizeof(struct Node));
- (*Header)->next= NULL;
- }
- int EmptyList(List L) {
- return (L->next==NULL);
- }
- void InsertList(ElementType X,Position P, List *L) {
- Position T;
- T=(NodeType)malloc(sizeof(struct Node));
- T->element=X;
- T->next=P->next;
- P->next=T;
- }
- void InsertList1(ElementType X, int P1, List *L) {
- Position P;
- P = *L;
- while((P->next != NULL) && (P1 > 1)) {
- P = P->next;
- P1--;
- }
- InsertList(X, P, L);
- }
- ElementType Retrieve(Position P, List L) {
- if (P->next!=NULL)
- return P->next->element;
- }
- Position First(List L) {
- return L;
- }
- Position EndList(List L) {
- Position P;
- P=First(L);
- while (P->next!=NULL)
- P=P->next;
- return P;
- }
- Position Next(Position P, List L) {
- return P->next;
- }
- void PrintList(List L) {
- Position P;
- P = First(L);
- while (P != EndList(L)) {
- printf("%d ",Retrieve(P,L));
- P = Next(P, L);
- }
- printf("\n");
- }
- void PrintRevList(List L) {
- if(L->next != NULL) {
- Position P = L->next;
- if(P->next == NULL)
- printf("%d ",P->element);
- else {
- PrintRevList(L->next);
- printf("%d ",P->element);
- }
- }
- }
- void ReadList(List *L) {
- int i,N;
- ElementType X;
- MakeNullList(L);
- printf("So phan tu danh sach N= ");
- scanf("%d",&N);
- for(i=1;i<=N;i++) {
- printf("Phan tu thu %d: ",i);
- scanf("%d",&X);
- InsertList(X,EndList(*L),L);
- }
- }
- int main() {
- List L;
- ElementType X;
- Position P;
- ReadList(&L);
- printf("Danh sach vua nhap: ");
- PrintList(L);
- printf("Danh sach nghich dao: ");
- PrintRevList(L);
- printf("\n");
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement