Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

List Implementation and Manipulation in C, Slides of Computer Engineering and Programming

An overview of lists, their data structures, implementations, and various operations such as creating, inserting, deleting, and converting strings to lists. It includes examples of list implementations using different versions of the listnode structure and dynamically created lists using malloc. The document also covers functions for empty lists, returning the first element, producing the tail of a list, and inserting and deleting elements.

Typology: Slides

2012/2013

Uploaded on 04/25/2013

obesix
obesix 🇺🇸

4.2

(18)

239 documents

1 / 47

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Programming Techniques
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f

Partial preview of the text

Download List Implementation and Manipulation in C and more Slides Computer Engineering and Programming in PDF only on Docsity!

Computer Programming Techniques

  1. List Data Structures and Operations

  2. List Implementations

  3. Dynamically Created Lists

  4. Converting a String to a List

  5. List Functions

Overview

Version 1:

#define N 1000 /* the size of the list */

typedef char LIST[N];

LIST lt; /* same as char lt[N] */

2. List implementations

struct listnode {

char data;

struct listnode *nextptr;

typedef struct listnode

LISTNODE;

LISTNODE elem;

Version 2 (Sec. 12.2)

elem

data nextptr

a.nextptr = &b;

b.nextptr = &c;

printf("%c", a.nextptr->data);

/* 'c' printed */

printf("%c", a.nextptr->nextptr->data);

/* 'e' printed */

a b c

‘a’ ‘c’ ‘e’ NULL

/* list implementation as before */

typedef LISTNODE *LNP;

LNP head = NULL;

head = malloc(sizeof(LISTNODE));

head->data = 'n';

head->nextptr = NULL;

3. Dynamically Created

Lists

Function prototype in stdlib.h:

void *malloc(size_t size);

head

‘n’ NULL

head->nextptr->nextptr =

malloc(sizeof(LISTNODE));

head->nextptr->nextptr->data = 'w';

head->nextptr->nextptr->nextptr = NULL;

Add a third element

head

‘n’ ‘e’ ‘w’ NULL

#include <stdio.h>

#include <stdlib.h>

/* list type implementation */

LNP string_to_list(char []);

int main()

LNP h = NULL;

h = string_to_list("AB");

return 0;

/* implementation of string_to_list() */

4. Converting a String to a

List

head =

malloc(sizeof(LISTNODE));

head->data = s[0];

tail = head;

string_to_list("AB")

head

‘A’

tail

tail->nextptr = malloc(sizeof(LISTNODE));

head

‘A’

tail

s[2] = '\0'

/* so end of list is assigned NULL */

head

‘A’

tail

‘B’ NULL

5 .1. Empty Lists

5 .2. Return the First Element of a List

5 .3. Produce the Tail of a List

5 .4. Put an Element on the Front of a List

5 .5. Insertion

5. List Functions

continued

 Make an empty list:

LNP h1;

h1 = NULL;

 Test for emptiness:

int isempty(LNP sptr)

return (sptr == NULL);

5 .1. Empty Lists

char first(LNP cptr)

if (isempty(cptr))

return '\0'

else

return cptr->data;

5 .2. Return the First Element

of a List